0% found this document useful (0 votes)
3 views

Chapter 2

Uploaded by

ankitdb04
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Chapter 2

Uploaded by

ankitdb04
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

About main()method

A simple Java program


Let us consider a simple program to find the square root of an integer.
import java.lang.*; //Math class is defined in this package

class Calculator{
double i;
double x = Math.sqrt(i);
}

class Example{
public static void main(String args[]){
Calculator a = new Calculator();
a.i = 20;
System.out.println("Square root of "+a.i+" is "+a.x);
}
}
Analysis of the program
Let us examine each statement step-by-step.
Import
import java.lang.*;
Statements
class Calculator{
double i;
Declaration double x = Math.sqrt(i);
of class }
class Example{
public static void main(String args[]){
Calculator a = new Calculator();
a.i = 20;
Declaration of System.out.println("Square root of "+a.i+" is "+a.x);
main class }
}
Significance of main class
Java program starts its execution from a method belongs to a class
only.

The main() method is the starting point of the execution of the main thread.

If there are multiple classes, then ambiguity is resolved by incorporating a


main() method into only one special class called main class.

The name of the Java program should be named after this class so that Java
interpreter unanimously choose that class to start its execution.
Understanding basic Java syntax

Java main() method


public Keyword

public

• It is an access specifier, which allows the programmer to control


the visibility of class members.

• public member may be accessed by code outside the class in


which it is declared.

• main() must be declared as public, since it must be called by


code outside of its class when the program is started.
Java main() method
Note: By default a member is public.
Other access specifiers will be discussed later.
static keyword

static

• The keyword static allows main() to be called without


having to instantiate a particular instance of the class.

• This is necessary since main() is called by Java interpreter


before any objects are made.

Note: There are more information about static which will be


Java main() method discussed shortly.
void keyword

void

• As per the Java programming language paradigm, each method


should return a value; if it does not return anything, then the
return type should be void.

• The keyword void simply tells the compiler that main()


does not return any value after its execution.

Java main() method


main() method

main

• main is the name of a method in a class.

• This method is searched by JVM as a starting point for an


application with a particular signature only.

Java main() method


Arguments in main()
String args[]

• Here, String is a class defied in java.lang API.

• args[] is an array to store objects of class String.

• Here, you could write anything, say String x[] instead of


String args[]. args[] is a common practice that every
programmer use. It is a customary.

• Java sees everything as String objects.


Java main() method
• It will help to read an input and then store into the array
args[] as String objects.
Output from Java program
Statement 12 includes the following code

System.out.println("Square root of "+a.i+" is "+a.x);

System is a final class from the java.lang package.

out is a class variable of type PrintStream declared in the System class.

println is a method of the PrintStream class.

a.i and a.x represents the names of variables to be printed.

+ is a concatenation operator, it is used to concatenate the string values.


print versus println methods
Consider the following lines to be printed as output
Debasis
Samanta

This can be done using both println() and print() functions


System.out.print(“Debasis”);
System.out.println(“Debasis”);
System.out.print(“\n”);
System.out.println(“Samanta”);
System.out.print(“Samanta”);

• The println(“…") method prints the string "..." and moves the cursor to a new line.
• The print("...") method instead prints just the string "...", but does not move the cursor to a
new line. Hence, subsequent printing instructions will print on the same line.

Note: The println() method can also be used without parameters, to position the cursor on the next
line.
Java Runtime Data Input
Practice another Java program
Let us run this Java program:

public class UserArgument{


public static void main(String args[]){
System.out.print("Hi ");
System.out.print(args[0]);
System.out.print(", How are you?");
}
}
Numeric input to program
Let us run this Java program:
import java.lang.*;

class Calculator{
double i;
double x = Math.sqrt(i);
}
class Example{
public static void main(String args[]){
Calculator a = new Calculator();
a.i = Integer.parseInt(args[0]);
System.out.println("Square root of "+a.i+" is "+a.x);
}
}
Input to Java program with Scanner Class

Scanner is one of the predefined class which is


used for reading the data dynamically from the
keyboard.
Example program for Scanner : Maximum
import java.util.Scanner;

public class MaximumCalculator {


public static void main(String args[]) {
Scanner scnr = new Scanner(System.in);
// Calculating the maximum two numbers in Java
System.out.println("Please enter two numbers to find maximum of two");
int a = scnr.nextInt();
int b = scnr.nextInt();
if (a > b) {
System.out.printf("Between %d and %d, maximum is %d \n", a, b, a);
}
else {
System.out.printf("Between %d and %d, maximum number is %d \n", a, b, b);
}
}
}
Example program with Scanner and array

import java.util.*;
class SimpleArrayList{
public static void main(String args[]){
int sum = 0;
float avg = 0;
ArrayList <Integer> l = new ArrayList<Integer>();
System.out.println("Enter the input ");
Scanner input = new Scanner(System.in);
while (input.hasNextInt()) {
l.add(input.nextInt());
}
for (int i = 0; i < l.size(); i++) {
sum = sum+l.get(i);
}
avg = sum/(l.size());
System.out.println("Average : " + avg);
Note:
}
} Press Ctrl+Z to stop scanning.
Input with DataInputStream : Calculator Program
import java.io.*;

class InterestCalculator{
public static void main(String args[ ] ) {
Float principalAmount = new Float(0);
Float rateOfInterest = new Float(0);
int numberOfYears = 0;
DataInputStream in = new DataInputStream(System.in);
String tempString;
System.out.println("Enter Principal Amount: ");
System.out.flush();
tempString = in.readLine();
principalAmount = Float.valueOf(tempString);
System.out.println("Enter Rate of Interest: ");
System.out.flush();
tempString = in.readLine();
rateOfInterest = Float.valueOf(tempString);
System.out.println("Enter Number of Years: ");
System.out.flush();
tempString = in.readLine();
numberOfYears = Integer.parseInt(tempString);
// Input is over: calculate the interest
float interestTotal = principalAmount*rateOfInterest*numberOfYears;
System.out.println("Total Interest = " + interestTotal);
}
}
Questions to think…

• Which type of binding that a Java program


can support?
• How recursive program in Java can be written?

You might also like