Aditya Gupta 500124035
Aditya Gupta 500124035
Sap Id 500124035
Roll NO 227132119
Experiment 4
Title: Class, Objects, Methods, and Constructors
1. Write a JAVA program to implement class mechanism. – Create a class, define data members,
methods (setData(args) ,getData()) and invoke them inside main method.
Algorithm :
a. Prompt the user to enter their name and age.
b. Read the name and age from the user.
c. Create an object of the ClassMechanism class.
d. Set the name and age of the object using the setData method.
e. Print the name and age of the object using the getData method.
}
}
Output :
2. Write software to prepare the results of the student for school examination. The Roll no of the
students and their marks in subjects of Physics, Chemistry and Maths are supplied through the
Scanner class. The result of the subject is prepared and displayed on the output screen by finding the
students who fail and pass the exam. If the marks are below 40% in subject, then the student has
failed in the subject. If the student passes and the aggregate score is above 75% then he scores
Distinction. If the student passes and aggregate marks are 60%,50%, and 40% then he scores First,
Second, and Third Division. Mention all the cases in output.
Algorithm :
a. Get the students marks in physics, chemistry and math.
b. Calculate the average marks.
c. Check if the student passed or failed in each subject.
d. Determine the student’s division based on their average marks.
e. Print the results.
Code :
import java.util.*;
public class School {
public void passFail(double marks) {
if (marks < 40) {
System.out.println("failed");
} else {
System.out.println("Passed");
}
}
public void Division(double marks) {
if (marks > 40 && marks < 50) {
System.out.println("Third division");
} else if (marks > 50 && marks < 60) {
System.out.println("Second division");
} else if (marks > 60 && marks < 75) {
System.out.println("First division");
} else {
System.out.println("Distinction");
}
}
Algorithm:
a. Define two methods with a same common name but different number of arguments.
b. Create an object of the class inside main function.
c. Call both of the methods with same common name with different values to the arguments to demonstrate
their behavior.
Code :
public class ComPoly {
public void student(int age, int marks, String name) {
System.out.println("\nname of student : " + name);
System.out.println("Age : " + age);
System.out.println("marks : " + marks);
}
}
}
Output :
4. Write a JAVA program to implement type promotion in method overloading.
Algorithm :
a. Define a method called sum that takes two parameters and calculate their sum and store it in a variable
called sum and print the sum.
b. Inside the main method create an object of the class.
c. Call the method three times with different arguments (i.e. one time integer value second time decimal value
and last time character value).
Code :
public class typePromotion {
public void sum(double a, double b) {
double sum = a + b;
System.out.println("Sum : " + sum);
}
}
}
Output :
5. Write a JAVA program to implement constructor overloading.
Algorithm :
a. Create a class which will have two constructors one is default constructor and other is parameterized
constructor with a single parameter.
b. Inside main method create two objects first one without any parameters and second one with a parameter
passed in it which will call the parameterized constructor.
Code :
public class cOverloading {
cOverloading() {
System.out.println("\nconstructor 1");
}
cOverloading(int a) {
System.out.println("Age = " + a);
}
Output :