Java Manual R23
Java Manual R23
Aim: The aim of the program is to demonstrate the use of differentdata types in the
java.
1. Algorithm:
import java.util.*;
class Datatype
{
public static void main(String args[])
int a=15;
byte b=100;
short c=126;
long d=12345;
boolean e=true;
System.out.println("byte number is:"+b);
System.out.println("int number is:"+a);
System.out.println("long number is:"+c);
System.out.println("short is:"+d);
System.out.println("Boolean is:"+e);
}
4. sample output:
1
2
byte numberis:100
2
3
1(b).OPERATORS
Aim: The aim of this program is to demonstrate the use ofvarious operators in
java
Algorithm:
import java.util.*;
class AO
{
public static void main(String args[]);
{
Scanner sc=new Scanner(System.in);
System.out.println(“enter a,b values:”);
int a=sc.nextInt();
int b=sc.nextInt();
int add=a+b;
int sub=a-b;
int mul=a*b;
int div=a%b;
System.out.println("the add of 2 numbers is:"+add);
System.out.println("the sub of 2 numbers is:"+sub);
System.out.println("the mul of 2 numbers is:"+mul);
System.out.println("the div of 2 numbers is:"+div);
}
3
4
Output:
Result: The above java program to demonstrate the use ofvarious operators in
java has been done successfully
4
5
Aim: the aim of the program is to find the classes and objects
Algorithm:
Program:
class Tester
{
public static void main(String[] args)
{
Tiger.roar(); // Call the roar method using the class name
}
}
class Tiger
{
public void eat()
{
System.out.println("The tiger is eating");
}
Output:
the tiger is roar
the tiger is eats
Result: hence the java program object and classes has been executedsuccessfully
6
7
2(b).CONSTRUCTOR
Program:
import java.util.*;
class Student
{
int sid;
String sname;
Student(int x, String y)
{
sid = x;
sname = y;
7
8
void display()
{
System.out.println("The student id is: " + sid);
System.out.println("The sname is: " + sname);
}
Output:
The output of the given code will be:
8
9
2(c).class,objects,methods
Algorithm:
• Start
• Class Definition
• Constructor
Create a constructor that takes two parameters (name and age) to initialize the attributes
of the class.
• Method: displayDetails()
Define a method displayDetails() that prints the student's name and age.
• Method: checkAge()
• Main Method
9
10
Program:
class Student
{
String name;
int age;
void displayDetails()
{
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
void checkAge()
{
if (age < 18)
{
System.out.println(name + " is a minor.");
} else
{
System.out.println(name + " is an adult.");
}
}
}
Output:
Name: Alice
10
11
Age: 17
Alice is a minor.
Name: Bob
Age: 20
Bob is an adult.
Result:.
11
12
Algorithm:
1. Define the Rectangle Class:
o Step 1.1: Create a class named Rectangle.
o Step 1.2: Declare two integer instance variables, length and width,
to store the dimensions of the rectangle.
2. Create the First Constructor:
o Step 2.1: Define a constructor that takes two integer parameters, x
and y.
o Step 2.2: Assign the value of x to the length variable.
o Step 2.3: Assign the value of y to the width variable.
3. Create the Second Constructor:
o Step 3.1: Define a second constructor that takes one integer
parameter, x.
o Step 3.2: Assign the value of x to both the length and width
variables (this is for creating a square).
4. Define the area() Method:
o Step 4.1: Create a method named area() within the Rectangle class.
o Step 4.2: Inside the area() method, calculate the area of the
rectangle by multiplying length by width.
o Step 4.3: Return the calculated area.
5. Define the Col Class:
o Step 5.1: Create another class named Col.
12
13
o Step 5.2: Inside the Col class, define the main method with the
signature public static void main(String args[]).
6. Create an Instance of Rectangle with Two Parameters:
o Step 6.1: Inside the main method, create an instance of Rectangle
using the first constructor, passing two integers, 10 and 20, as
arguments. This initializes a rectangle with a length of 10 and a
width of 20.
o Step 6.2: Call the area() method on this instance to calculate the
area.
o Step 6.3: Store the result in a variable ra.
7. Print the Area of the First Rectangle:
o Step 7.1: Print the area stored in the variable ra with a message like
"The area of the rectangle is: ".
8. Create an Instance of Rectangle with One Parameter:
o Step 8.1: Create another instance of Rectangle using the second
constructor, passing one integer 10 as the argument. This initializes
a square with both length and width equal to 10.
o Step 8.2: Call the area() method on this instance to calculate the
area.
o Step 8.3: Store the result in a variable ra1.
9. Print the Area of the Square:
o Step 9.1: Print the area stored in the variable ra1 with a message
like "The area of the rectangle is: ".
Program:
class Rectangle
{
int length, width;
Rectangle(int x, int y)
{
13
14
length = x;
width = y;
}
Rectangle(int x)
{
length = width = x;
}
int area()
{
int res = length * width;
return res;
}
}
class Col
{
public static void main(String args[])
{
Rectangle obj = new Rectangle(10, 20);
int ra = obj.area();
System.out.println("The area of the rectangle is: " + ra);
14
15
Output
The area of the rectangle is: 200
The area of the rectangle is: 100
15
16
Algorithm:
16
17
26. Step 10.2: The method calculates the sum and prints "The sum of 3 numbers is: 60".
27. Call the Third sum Method:
28. Step 11.1: Call the sum method with four arguments, 10, 20, 30, and 40.
29. Step 11.2: The method calculates the sum and prints "The sum of 4 numbers is: 100".
Program
import java.util.*;
class Demo
{
void sum(int x, int y)
{
int res1 = x + y;
System.out.println("The sum of 2 numbers is: " + res1);
}
class Mol
{
public static void main(String args[])
{
Demo obj = new Demo();
17
18
obj.sum(10, 20);
obj.sum(10, 20, 30);
obj.sum(10, 20, 30, 40);
}
}
Output
The sum of 2 numbers is: 30
The sum of 3 numbers is: 60
The sum of 4 numbers is: 100
18
19
3(c)Method overriding
Program:
import java.util.*;
class Rectangle
{
double area(double l, double b)
{
double res = l * b;
return res;
}
}
class Mor
{
19
20
Output:
The area of triangle is 10.0
20
21
1. Define Rectangle class with attributes for length and width and a method to calculate
the area.
2. Create Triangle class that extends Rectangle, adds a height attribute, and includes a
method to calculate the volume (or extended area).
3. In the Single class, define the main method.
4. Create an instance of Triangle and calculate the area of the rectangle and the extended
area (volume) of the triangle.
5. Print the calculated areas.
Program:
import java.util.*;
class Rectangle
{
int length, width;
Rectangle(int x, int y)
{
length = x;
width = y;
}
int area()
{
int res = length * width;
return res;
}
}
21
22
int tarea()
{
int res1 = area() * height;
return res1;
}
}
class Single
{
public static void main(String args[])
{
Triangle obj = new Triangle(10, 5, 20);
int ra = obj.area();
int ta = obj.tarea();
System.out.println("The area of rectangle is: " + ra);
System.out.println("The area of triangle is: " + ta);
}
}
Output:
The area of rectangle is: 50
The area of triangle is: 1000
22
23
Student(int x, String y)
{
23
24
sno = x;
sname = y;
}
void stu()
{
System.out.println("The sno is: " + sno);
System.out.println("The sname is: " + sname);
}
}
class Marks extends Student
{
int m1, m2, m3;
Marks(int x, String y, int a, int b, int c)
{
super(x, y);
m1 = a;
m2 = b;
m3 = c;
}
void stu_marks()
{
System.out.println("The sub1 marks is: " + m1);
System.out.println("The sub2 marks is: " + m2);
System.out.println("The sub3 marks is: " + m3);
}
}
class Total extends Marks
{
Total(int x, String y, int a, int b, int c)
{
super(x, y, a, b, c);
}
24
25
int total
{
int tot = m1 + m2 + m3;
return tot;
}
}
25
26
OUTPUT:
the sno is: 18
the sname is: yuvaraju
the sub1 marks is: 90
the sub2 marks is: 92
the sub3 marks is: 93
the student total marks is: 275
the student percentage is: 91.666...
26
27
Algorithm:
1. Class Rectangle: Has length and width, and calculates area with rarea().
2. Class Volume (Extends Rectangle): Inherits from Rectangle, calculates a modified area
in varea().
3. Class Triangle (Extends Rectangle): Inherits from Rectangle and adds height to
calculate the area in tarea().
4. Main Method: Creates objects of Triangle and Volume, then calculates and prints their
areas
Program:
import java.util.*;
class Rectangle
{
int length, width;
Rectangle(int x, int y)
{
length = x;
width = y;
}
int rarea()
{
int res = length * width;
return res;
}
}
27
28
{
Volume(int x, int y)
{
super(x, y);
}
int varea()
{
int res1 = 1 / 2 * rarea();
return res1;
}
}
int tarea()
{
int res3 = rarea() * height;
return res3;
}
}
class Hierarchical
{
public static void main(String args[])
28
29
{
Triangle obj1 = new Triangle(10, 20, 30);
int ta = obj1.tarea();
int ra = obj1.rarea();
System.out.println("The area of rectangle is: " + ra);
System.out.println("The area of triangle is: " + ta);
Output:
29
30
Algorithm:
1. Start
2. Define Parent Class (Person)
o Declare a String variable name.
o Define a constructor Person(String name) to initialize the name variable.
o Print "Person constructor called".
o Define a method displayInfo() to display the value of name.
3. Define Child Class (Employee) that Extends Person
o Declare an int variable employeeId.
o Define a constructor Employee(String name, int employeeId):
▪ Use super(name) to call the parent class (Person) constructor and initialize
name.
▪ Initialize the employeeId variable.
▪ Print "Employee constructor called".
o Override the displayInfo() method:
▪ Use super.displayInfo() to call the parent class method displayInfo() to
display name.
▪ Print employeeId.
4. Define Main Class (Main)
o In the main method:
▪ Create an object emp of class Employee, passing "John Doe" and 101 as
arguments to the constructor.
▪ Call emp.displayInfo() to display the details of the employee.
5. End
Program:
class Person
{
String name;
Person(String name)
{
this.name = name;
System.out.println("Person constructor called");
}
void displayInfo()
{
System.out.println("Name: " + name);
30
31
}
}
void displayInfo()
{
super.displayInfo();
System.out.println("Employee ID: " + employeeId);
}
}
Output:
31
32
5(b)Interfaces
Aim: Write a JAVA program to implement Interface. What kind of Inheritance can be achieved
Algorithm:
1. Define the Interface:
o Create an interface that specifies the methods which classes implementing the
interface must define.
2. Implement the Interface:
o Create one or more classes that implement the interface.
o Provide concrete implementations for all methods declared in the interface.
3. Use the Implementing Classes:
o In the main class, create objects of the classes that implement the interface.
o Call the methods defined in the interface using these objects.
Program:
interface Device
{
void turnOn();
void turnOff();
void displayInfo();
}
32
33
@Override
public void turnOff()
{
System.out.println("Laptop is turning off.");
}
@Override
public void displayInfo()
{
System.out.println("This is a Laptop.");
}
}
myPhone.turnOn();
myPhone.displayInfo();
myPhone.turnOff();
myLaptop.turnOn();
myLaptop.displayInfo();
myLaptop.turnOff();
}
}
Output:
Smartphone is turning on.
This is a Smartphone.
Smartphone is turning off.
Laptop is turning on.
This is a Laptop.
33
34
34
35
1. Initialize Variables:
2. Set the number n to the value you want to check (e.g., 153).
3. Store the original number in a temporary variable temp for later comparison.
4. Initialize sum to 0 to keep track of the sum of cubes of digits.
5. Extract Digits and Calculate Sum:
6. While Loop: Continue the loop as long as n is not 0.
7. Extract Digit: Compute the remainder of n divided by 10 (r = n % 10), which gives the last
digit of n.
8. Calculate Cube: Compute the cube of the digit (r * r * r) and add it to sum.
9. Update Number: Update n by removing the last digit (n = n / 10).
10. Compare and Print Result:
11. After exiting the loop, compare sum with temp.
12. If sum is equal to temp, print "the given number is Armstrong."
13. Otherwise, print "the given number is not Armstrong."
Program:
import java.util.*;
class Armstrong
{
public static void main(String args[])
{
int n=153;
int temp=n;
int r,sum=0;
while(n!=0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
{
System.out.println("the given number is Armstrong");
35
36
}
else
{
System.out.println("the given number is not Armstrong");
}
}
}
Output:
36
37
6(b)switch statement
Aim: write a java program to print the week days by using switch statement.
Algorithm:
1. Initialize: Create a Scanner object.
2. Prompt: Ask the user to enter a day number.
3. Read Input: Capture the input number.
4. Determine Day: Use switch to assign the correct day name or "Invalid day".
5. Print Result: Output the day name or error message.
Program:
import java.util.*;
class Days
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the day:");
int day=sc.nextInt();
String dayString;
switch(day)
{
case 1: dayString="Monday";
break;
case 2: dayString="Tuesday";
break;
case 3: dayString="Wednesday";
break;
case 4: dayString="Thrusday";
break;
case 5: dayString="Friday";
break;
case 6: dayString="Saturday";
break;
case 7: dayString="Sunday";
break;
default: dayString="Invalid day";
break;
}
System.out.println("The day is "+dayString);
}
}
Output:
Enter the day:5
Friday
37
38
Result:
Hence the above program is executed successfully
38