[go: up one dir, main page]

0% found this document useful (0 votes)
15 views38 pages

Java Manual R23

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views38 pages

Java Manual R23

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

1

1(a). DATA TYPES

Aim: The aim of the program is to demonstrate the use of differentdata types in the
java.

1. Algorithm:

1. Start the program


2. Give the class name as data type.
3. Initialize the values for the primitive datatypes.
4. Print the data.
5. Stop.
3.program:

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

int number is:15

long number is:126


short is:12345
boolean is: true

Result:The above java program to demonstrate the use ofvarious Datatypes in


java has been done successfully

2
3

1(b).OPERATORS

Aim: The aim of this program is to demonstrate the use ofvarious operators in
java

Algorithm:

1. Start the program


2. Initialize the variable
3. Give the operations according to our need such asint add=a+b;
4. Int sub=a-b;
5. Int mul=a*b;
6. Int div=a%b;
7. Print the output statement using println()
8. stop
Program:

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:

Enter a,b values:10,20


The add of 2 numbers is :30
The sub of 2 numbers is:10
The mul of 2 numbers is:200
The add of 2 numbers is:10

Result: The above java program to demonstrate the use ofvarious operators in
java has been done successfully

4
5

2(a).CLASSES & OBJECTS

Aim: the aim of the program is to find the classes and objects

Algorithm:

1. Start the program.


2. Define the class "Tester".
3. Inside the "main" method, call the "roar" method of the "Tiger" class usingthe
class name.

4. Create a new instance of the "Tiger" class called "t1".


5. Call the "eat" method of the "Tiger" class using the "t1" instance.
6. End the program.

Program:
class Tester
{
public static void main(String[] args)
{
Tiger.roar(); // Call the roar method using the class name

Tiger t1 = new Tiger();


t1.eat();

}
}

class Tiger

{
public void eat()
{
System.out.println("The tiger is eating");
}

public static void roar()


5
{
6

System.out.println("The tiger is roaring");


}
}

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

Aim: the aim of the program is to write a program on the constructor


Algorithm:
1. Start the program.
2. Create a class Student with instance variables sid (student id) of type int and
sname (student name) of type String.
3. Create a constructor for the Student class that takes two parameters - x (for sid) and y
(for sname).
4. Inside the constructor, assign the values of x and y to sid and sname
respectively.
5. Create a method called display() within the Student class.
6. Inside the display() method, print the value of sid and sname using
System.out.println() method.
7. Create the main() method.
8. Inside the main() method, create an object of the Student class called obj and
pass the values 10 and "arya" as arguments to the constructor.
9. Call the display() method using the obj object.
10. End the
program.

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);
}

public static void main(String args[])


{
Student obj = new Student(10, "arya");
obj.display();
}
}

Output:
The output of the given code will be:

The student id is: 10


The sname is: arya

Result: hence the java program has been executed successfully

8
9

2(c).class,objects,methods

Aim: Write a JAVA program to implement class mechanism. Create a class,


methods and invoke them inside main method.

Algorithm:
• Start

• Class Definition

Define a class Student with two attributes:


o name (String): Stores the name of the student.
o age (int): Stores the age of the student.

• 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()

Define a method checkAge() that:


o If age is less than 18, print that the student is a minor.
o Otherwise, print that the student is an adult.

• Main Method

In the main method:


o Create First Student Object
▪ Create an object student1 of class Student by passing the name and age (e.g.,
"Alice", 17).
▪ Invoke the displayDetails() method on student1.
▪ Invoke the checkAge() method on student1.
o Create Second Student Object
▪ Create another object student2 of class Student by passing the name and age
(e.g., "Bob", 20).
▪ Invoke the displayDetails() method on student2.
▪ Invoke the checkAge() method on student2.
End

9
10

Program:
class Student
{
String name;
int age;

Student(String name, int age)


{
this.name = name;
this.age = 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.");
}
}
}

public class Main


{
public static void main(String[] args)
{
Student student1 = new Student("Alice", 17);
student1.displayDetails();
student1.checkAge();

Student student2 = new Student("Bob", 20);


student2.displayDetails();
student2.checkAge();
}
}

Output:

Name: Alice

10
11

Age: 17
Alice is a minor.

Name: Bob
Age: 20
Bob is an adult.

Result:.

hence the java program has been executed successfully

11
12

3(a) Constructor overloading

Aim: Write a Java program To demonstrate the concept of Constructor overloading

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);

Rectangle obj1 = new Rectangle(10);


int ra1 = obj1.area();
System.out.println("The area of the rectangle is: " + ra1);
}
}

14
15

Output
The area of the rectangle is: 200
The area of the rectangle is: 100

Result: hence the java program has been executed successfully

15
16

3(b) Method overloading

Aim: Write a Java program To demonstrate the concept of Method overloading

Algorithm:

1. Define the Demo Class:


2. Step 2.1: Create a class named Demo.
3. Define the First sum Method:
4. Step 3.1: Inside the Demo class, define a method sum that takes two integer parameters, x
and y.
5. Step 3.2: Calculate the sum of x and y, and store the result in a variable res1.
6. Step 3.3: Print the result with the message "The sum of 2 numbers is: ".
7. Define the Second sum Method:
8. Step 4.1: Define another method sum that takes three integer parameters, x, y, and z.
9. Step 4.2: Calculate the sum of x, y, and z, and store the result in a variable res2.
10. Step 4.3: Print the result with the message "The sum of 3 numbers is: ".
11. Define the Third sum Method:
12. Step 5.1: Define another method sum that takes four integer parameters, x, y, z, and p.
13. Step 5.2: Calculate the sum of x, y, z, and p, and store the result in a variable res3.
14. Step 5.3: Print the result with the message "The sum of 4 numbers is: ".
15. Define the Mol Class:
16. Step 6.1: Create another class named Mol.
17. Define the main Method:
18. Step 7.1: Inside the Mol class, define the main method with the signature public static void
main(String args[]).

19. Create an Instance of Demo Class:


20. Step 8.1: In the main method, create an instance of the Demo class.
21. Call the First sum Method:
22. Step 9.1: Call the sum method with two arguments, 10 and 20.
23. Step 9.2: The method calculates the sum and prints "The sum of 2 numbers is: 30".
24. Call the Second sum Method:
25. Step 10.1: Call the sum method with three arguments, 10, 20, and 30.

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);
}

void sum(int x, int y, int z)


{
int res2 = x + y + z;
System.out.println("The sum of 3 numbers is: " + res2);
}

void sum(int x, int y, int z, int p)


{
int res3 = x + y + z + p;
System.out.println("The sum of 4 numbers is: " + res3);
}
}

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

Result:Hence the java program has been executed successfully

18
19

3(c)Method overriding

Aim: Write a Java program To demonstrate the concept of Method overriding


Algorithm:

1. Define Rectangle class with a method to calculate the area of a rectangle.


2. Create Triangle class that extends Rectangle and overrides the area method to calculate
the area of a triangle.
3. In the Mor class, define the main method.
4. Create an instance of Triangle and use it to calculate the area of a triangle.
5. Print the calculated area.

Program:
import java.util.*;

class Rectangle
{
double area(double l, double b)
{
double res = l * b;
return res;
}
}

class Triangle extends Rectangle


{
double area(double l, double b)
{
double res1 = 0.5 * l * b;
return res1;
}
}

class Mor
{

19
20

public static void main(String args[])


{
Triangle obj1 = new Triangle();
double ta = obj1.area(4, 5);
System.out.println("The area of triangle is " + ta);
}
}

Output:
The area of triangle is 10.0

Result:Hence the java program has been executed successfully

20
21

4(a) Single Inheritance

Aim: Write a Java program To demonstrate the concept of Single Inheritance


Algorithm:

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;
}
}

class Triangle extends Rectangle


{
int height;

21
22

Triangle(int x, int y, int z)


{
super(x, y);
height = z;
}

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

Result: Hence the java program has been executed successfully

22
23

4(b) Multilevel Inheritance


Aim: Write a Java program To demonstrate the concept of Multilevel Inheritance
Algorithm:
1. Declare A Class As Student
2. Contains student details such as sno (student number) and sname (student name).
3. The constructor initializes these attributes, and the stu() method prints the student's
details.
4. Marks Class (extends Student):
5. Inherits from the Student class and adds three integer marks (m1, m2, m3).
6. The constructor initializes the student details (using super(x, y)) and the marks.
7. The stu_marks() method prints the marks.
8. Total Class (extends Marks):
9. Adds functionality to calculate the total of the three marks via the total() method.
10. Percentage Class (extends Total):
11. Calculates the percentage by averaging the marks through the per() method.
12. Main Method (Multilevel2):
13. Creates an instance of the Percentage class, passing the student number, name, and
marks.
14. Calls the stu(), stu_marks(), total(), and per() methods to print the student details,
marks, total marks, and percentage.
Program:
import java.util.*;
class Student
{
int sno;
String sname;

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;
}
}

class Percentage extends Total


{
Percentage(int x, String y, int a, int b, int c)
{
super(x, y, a, b, c);
}
double per()
{
double avg = total() / 3.0;
return avg;
}
}
class Multilevel2
{
public static void main(String args[])
{
Percentage obj = new Percentage(18, "yuvaraju", 90, 92, 93);
obj.stu();
obj.stu_marks();
int tm = obj.total();
double pa = obj.per();
System.out.println("The student total marks is: " + tm);
System.out.println("The student percentage is: " + pa);
}
}

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...

Result: Hence the java program has been executed successfully.

26
27

4(c) Hierarchical Inheritance


Aim: Write a Java program To demonstrate the concept of Hierarchical Inheritance

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;
}
}

class Volume extends Rectangle

27
28

{
Volume(int x, int y)
{
super(x, y);
}

int varea()
{
int res1 = 1 / 2 * rarea();
return res1;
}
}

class Triangle extends Rectangle


{
int height;

Triangle(int x, int y, int z)


{
super(x, y);
height = z;
}

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);

Volume obj2 = new Volume(10, 20);


int va = obj2.varea();
System.out.println("The area of volume is: " + va);
}
}

Output:

1. The area of the rectangle is: 200

2. The area of the triangle is: 6000

3. The area of the volume is: 100.0

Result: Hence the java program has been executed successfully.

29
30

5(a) super() keyword

Aim: Write a JAVA program give example for “super” keyword.

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

}
}

class Employee extends Person


{
int employeeId;

Employee(String name, int employeeId)


{
super(name);
this.employeeId = employeeId;
System.out.println("Employee constructor called");
}

void displayInfo()
{
super.displayInfo();
System.out.println("Employee ID: " + employeeId);
}
}

public class Main


{
public static void main(String[] args)
{
Employee emp = new Employee("John Doe", 101);
emp.displayInfo();
}
}

Output:

Person constructor called


Employee constructor called
Name: John Doe
Employee ID: 101

Result: Hence the java program has been executed successfully.

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();
}

class Smartphone implements Device


{

public void turnOn()


{
System.out.println("Smartphone is turning on.");
}

public void turnOff()


{
System.out.println("Smartphone is turning off.");
}

public void displayInfo()


{
System.out.println("This is a Smartphone.");
}
}

32
33

class Laptop implements Device


{

public void turnOn()


{
System.out.println("Laptop is turning on.");
}

@Override
public void turnOff()

{
System.out.println("Laptop is turning off.");
}

@Override
public void displayInfo()
{
System.out.println("This is a Laptop.");
}
}

public class Main


{
public static void main(String[] args)
{
Device myPhone = new Smartphone();
Device myLaptop = new 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

Laptop is turning off.

Result: Hence the java program has been executed successfully.

34
35

6(a) Armstrong number


Aim: Write a java program check the given number is Armstrong or not
Algorithm:

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:

the given number is Armstrong

Result: Hence the java program has been executed successfully.

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

You might also like