Chapter 2
Chapter 2
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.
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
public
static
void
main
• 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:
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
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…