0% found this document useful (0 votes)
33 views5 pages

Ce146 JT Lab 2

Uploaded by

dhyeyshah009
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)
33 views5 pages

Ce146 JT Lab 2

Uploaded by

dhyeyshah009
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/ 5

Lab 2

Topics: Inheritance, Polymorphism(method overriding), static


keyword
Q1. Write a Java program that checks for prime number using the object oriented
approach.
[Hint: create a class NumberClass with a member value and method isPrimeNumber()]
Code :
import java.util.Scanner;
public class NumberClass {
int value;
public NumberClass(int value) {
super();
this.value = value;
}
public boolean isPrimeNumber (int n) {
if(n == 0 || n == 1) {
return false;
}
int i = 2;
while(i <= n/2) {
if(n % i == 0) {
return false;
}
i++;
}
return true;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Number : ");
int n = sc.nextInt();
NumberClass ob = new NumberClass(n);
boolean b = ob.isPrimeNumber(n);
if(b) {
System.out.println("Number is Prime");
} else {
System.out.println("Number is not Prime");
}
}
}
Output :
Q2. Create two classes:
Person
- name : String
- age : int
+ Person()
+ Person(name : String, age : int)
+ getName() : String
+ getAge() : int
+ setName(name : String) : void
+ setAge(age : int) : void
+ toString() : String

Student
- rollno : int
- marks : double[]
+ Student()
+ Student(rollno : int)
+ Student(rollno : int, marks : double[])
+ Student(rollno : int, name : String, age : int, marks : double[])
+ getRollno() : int
+ getMarks() : double[]
+ setRollno(rollno: int) : void
+ setMarks(marks : double[]) : void
+ toString() : String
+ displayDetails() : void

Add the following to Student class:


● a static variable count( to count the number of objects)
● a static block to initialize count variable to zero
● a static method String getCount() that returns the number of student objects created

● Write a TestStudent class containing the main() method.


● Store the details of 3 students by creating an array of objects of Student class and
display the student who has highest average amongst the three students as follows using
displayDetails() method for that object:

e.g.
RollNo = 100
Name = ABC
Age = 20
Marks=78 86 88 67 92

● Create one more object of the Student class and then call the getCount() to display
the number of Student objects created.
Code :
Person.java
public class Person {
private String name;
private int age;
public Person() {
this.name = "";
this.age = 0;
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "Name: " + name + ", Age: " + age;
}
}

Student.java
public class Student extends Person {
private int rollno;
private double[] marks;
public static int count = 0;
static {
count = 0;
}
public Student() {
super();
this.rollno = 0;
this.marks = new double[0];
count++;
}
public Student(int rollno) {
super();
this.rollno = rollno;
this.marks = new double[0];
count++;
}
public Student(int rollno, double[] marks) {
super();
this.rollno = rollno;
this.marks = marks;
count++;
}
public Student(int rollno, String name, int age, double[] marks) {
super(name, age);
this.rollno = rollno;
this.marks = marks;
count++;
}
public int getRollno() {
return rollno;
}
public double[] getMarks() {
return marks;
}
public void setRollno(int rollno) {
this.rollno = rollno;
}
public void setMarks(double[] marks) {
this.marks = marks;
}
public double getAverage() {
double sum = 0;
for (double mark : marks) {
sum += mark;
}
return marks.length > 0 ? sum / marks.length : 0;
}
@Override
public String toString() {
String marksStr = "";
for (double mark : marks) {
marksStr += mark + " ";
}
return "RollNo: " + rollno + "\nName: " + getName() + "\nAge: " + getAge() + "\nMarks: " + marksStr.trim();
}
public void displayDetails() {
System.out.println(toString());
}
public static String getCount() {
return "\nNumber of Student objects created: " + count;
}
}

TestStudent.java
public class TestStudent {
public static void main(String[] args) {
Student[] students = new Student[3];
students[0] = new Student(101, "Dhyey", 18, new double[]{98, 86, 88, 67, 92});
students[1] = new Student(102, "Ansh", 19, new double[]{85, 90, 76, 80, 88});
students[2] = new Student(103, "Rutvish", 19, new double[]{72, 80, 68, 85, 95});

Student topStudent = students[0];


for (int i = 1; i < students.length; i++) {
if (students[i].getAverage() > topStudent.getAverage()) {
topStudent = students[i];
}
}
System.out.println("Student with highest average marks:");
topStudent.displayDetails();
Student student4 = new Student(104, "Dev", 18, new double[]{85, 95, 90, 91, 88});

System.out.println(Student.getCount());
}
}

Output :

You might also like