0% found this document useful (0 votes)
90 views7 pages

Aditya Gupta 500124035

The document contains code snippets and algorithms for 5 Java programming exercises: 1. Implementing a class with methods to set and get data member values. 2. Creating a School class to calculate student exam results, identifying subjects passed/failed and awarding divisions. 3. Demonstrating compile-time polymorphism by overloading methods with different arguments. 4. Implementing method overloading using type promotion to add values of different data types. 5. Overloading constructors, creating objects with and without parameters.

Uploaded by

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

Aditya Gupta 500124035

The document contains code snippets and algorithms for 5 Java programming exercises: 1. Implementing a class with methods to set and get data member values. 2. Creating a School class to calculate student exam results, identifying subjects passed/failed and awarding divisions. 3. Demonstrating compile-time polymorphism by overloading methods with different arguments. 4. Implementing method overloading using type promotion to add values of different data types. 5. Overloading constructors, creating objects with and without parameters.

Uploaded by

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

Name Aditya Gupta

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.

Code : import java.util.*;


class ClassMechanism{
String name;
int age;
public void setData(String n,int a) {
name = n;
age = a;
}
public void getData() {
System.out.println("\nName : "+ name + "\nAge : "+ age);
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter name : ");
String name = sc.nextLine();
System.out.println("Enter age : ");
int age = sc.nextInt();
ClassMechanism obj = new ClassMechanism();
obj.setData(name, age);
obj.getData();

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

public static void main(String args[]) {


Scanner sc = new Scanner(System.in);
System.out.println("Enter roll number of student : ");
int roll = sc.nextInt();
System.out.println("Enter physics marks : ");
double physics = sc.nextDouble();
System.out.println("Enter chemistry marks : ");
double chemistry = sc.nextDouble();
System.out.println("Enter math marks : ");
double maths = sc.nextDouble();
double avgMarks = (physics + maths + chemistry) / 3;
School obj = new School();
System.out.print("Result in physics : ");
obj.passFail(physics);
System.out.print("Result in maths : ");
obj.passFail(maths);
System.out.print("Result in chemistry : ");
obj.passFail(chemistry);
obj.Division(avgMarks);
}
}
Output :
3. Write a JAVA program to implement compile time polymorphism.

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

public void student(String name, int marks) {


System.out.println("\nName : " + name);
System.out.println("Marks : " + marks);
}

public static void main(String args[]) {


String name = "Anshul";
int age = 24;
int marks = 100;
ComPoly obj = new ComPoly();
obj.student(name, marks);
obj.student(age, marks, name);

}
}

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

public static void main(String args[]) {


typePromotion obj = new typePromotion();
obj.sum(155, 255);
obj.sum(155.6, 699.65);
obj.sum('a', 'S');

}
}

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

public static void main(String args[]) {


cOverloading obj = new cOverloading();
System.out.println("hello world");
int a = 24;
cOverloading obj1 = new cOverloading(a);
}
}

Output :

You might also like