Java - 2-v2
Java - 2-v2
Java - 2-v2
Simple Programs
public class EggBasket Simple Programs class Example2 {
{ public static void main(String[]
public static void main(String[] args) ◼ It is possible to declare two or more variables using Code
args) {
{ int myVar1; this declares
int numberOfBaskets, eggsPerBasket, the same declaration statement. Just separate their
a variable
totalEggs; names by commas. For example, myVar1 and
numberOfBaskets = 10; int myVar2; this declares
myVar2 could have been declared like this: another variable
eggsPerBasket = 6;
totalEggs = numberOfBaskets *
int myVar1, myVar2; myVar1 = 1024; this
eggsPerBasket; assigns 1024 to myVar1
System.out.println("If you have"); System.out.print("myVar2 contains System.out.println("myVar1
System.out.println(eggsPerBasket + " contains " + myVar1);
eggs per basket and");
myVar1 / 2: ");
myVar2 = myVar1 / 2;
System.out.println(numberOfBaskets + System.out.println(myVar2); System.out.print("myVar2
" baskets, then");
◼ The print( ) method is just like println( ), except that contains myVar1 / 2: ");
System.out.println("the total number
of eggs is " + totalEggs); print() does not output a new line after each call. System.out.println(myVar2);
} Output
}
}
◼ Both print( ) and println( ) can be used to output values of }
any of Java’s built-in types.
Keyboard Input Keyboard Input
import java.util.Scanner;
public class RectangleArea Enter the width of a rectangle in cm:
public class EggBasket
{ 10 {
public static void main(String[]
Enter the height of a rectangle in cm: public static void main(String[] args)
5
args) the area of the rectangle is:50
{
{ Process finished with exit code 0 int numberOfBaskets, eggsPerBasket,
totalEggs;
int width, height, area;
◼ We use the class Scanner to accept keyboard input. numberOfBaskets = 10;
Scanner keyboard = new
eggsPerBasket = 6;
Scanner(System.in); ◼ This line “ import java.util.Scanner;” import the totalEggs = numberOfBaskets *
System.out.println("Enter the definition of the scanner class from the package java. eggsPerBasket;
width of a rectangle: "); util. System.out.println("If you have");
width = keyboard.nextInt(); System.out.println(eggsPerBasket + "
System.out.println("Enter the ◼ The line “ Scanner keyboard = new eggs per basket and");
height of a rectangle: "); Scanner(System.in);” sets things up so that data System.out.println(numberOfBaskets +
height = keyboard.nextInt(); can be entered from the keyboard, this line must " baskets, then");
area = width * height; appear before getting any input from the keyboard. System.out.println("the total number
System.out.print("the area of of eggs is " + totalEggs);
the rectangle is: " + area ); ◼ This line “Scanner keyboard = new }
} Scanner(System.in);” the value of the expression }
} to the right (Keyboard input) and gives it to the
◼ If the value of distance is 25.99, the value of (int)distance is 25 not 26. It displays the number 55 because character '7' just happened to get
the number 55.
◼ The following command will display the character ‘A’
ASCII Table Named Constants
◼ A constants value cannot be changed, it need to have a fixed value (For example, 'A', 'B’, 5, 2.7)
◼ Constant can be initialize only one time using this syntax:
finalType Variable = Constant ;
▪ For example, we can give the name PI to the constant 3.14159 as follows:
public static final double PI = 3.14159;
▪ The convention for naming constants is to use all uppercase letters, with an underscore symbol (_)
between words. For example:
final int DAYS_PER_WEEK = 7;
final int MAX_STRIKES = 3;
final double INTEREST_RATE = 6.99;
final String MOTTO = "The customer is right!";
final char SCALE = 'K';
Named Constants
import java.util.Scanner;
public class CircleArea
{
public static void main(String[] args)
{
double r, area;
final double PI=3.14159; Thank You
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the radius if the
circle in cm.: ");
r = keyboard.nextInt();
Question are Welcome
area = r * PI;
System.out.print(“The area of the rectangle
is: " + area + "square cm.");
}
}
Enter the radius if the circle in cm.:
5
The area of the rectangle is: 15.70795 square cm.