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

Ce133 JT Lab 3

The document contains code to create classes Person and Student, with Student extending Person. It defines methods and variables like count, average, and displayDetails() for the Student class. The main method creates Student objects, calculates their average marks, displays details of the student with highest average, and calls getCount() to output the total number of Student objects created.

Uploaded by

Robert khan
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)
40 views5 pages

Ce133 JT Lab 3

The document contains code to create classes Person and Student, with Student extending Person. It defines methods and variables like count, average, and displayDetails() for the Student class. The main method creates Student objects, calculates their average marks, displays details of the student with highest average, and calls getCount() to output the total number of Student objects created.

Uploaded by

Robert khan
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/ 5

1.Write a Java program that checks for prime number using the object oriented approach.

import java.util.*;

class primeNumber{
int value;

primeNumber(int value){
this.value = value;
}
boolean isPrime() {
for(int i = 2 ; i<value ; i++) {
if(value % i == 0) {
return false;
}
}
return true;
}
}

public class question1 {


public static void main (String [] args) {

Scanner sc = new Scanner(System.in);


int n = sc.nextInt();

primeNumber number = new primeNumber(n);


if(number.isPrime()) {
System.out.println("it is prime number");
}
else {
System.out.println("it is not a prime number");
}
}
}

Output:
2.Create two classes:
class Person
Derive a class Student from class Person.

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.

import java.util.*;

class person{
String name;
int age;
person(){
System.out.println("this the default constructer for person");
}
person(String name , int age){
this.name = name;
this.age = age;
}
String getName() {
return name;
}
int age() {
return age;
}
void setName(String name) {
this.name = name;
}
void setAge(int age) {
this.age = age;
}
public String toString() {
String s = "Name: "+name + "Age: " + age;
return s;
}

}
class student extends person{
int rollno;
double marks[]= new double [5];
static int count;
static {
count = 0;
}
student() {
System.out.println("this a default constructer of student");
count++;
}
student(int rollno){
this.rollno = rollno;
count++;
}
student(int rollno , double marks[]){
this.rollno = rollno;
this.marks = marks;
count++;
}
student(int rollno, String name , int age , double marks[]){
super(name,age);
this.rollno = rollno;
this.marks = marks;
count++;
}
int getRollNo() {
return rollno;
}
double[] getMarks() {
return marks;
}

void setRollNo(int rollno) {


this.rollno = rollno;
}
void setMarks(double marks[]) {
this.marks = marks;
}
public String toString() {
String mark = "";
for(int i = 0 ; i<5 ; i++) {
mark += marks[i] + " ";
}
String s = "Name: "+name + "\n" + "Age: " + age +"\n" + "rollno: "+rollno + "\n" +
"Marks: " + mark;
return s;
}
void displayDetails() {
String s = toString();
System.out.println(s);
}
static int getCount() {
return count;
}
double average() {
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += marks[i];
}
double average = sum / 5;
return average;
}
}

public class teststudent {


public static void main (String [] args) {
double marks1[] = {20,30,40,50,60};
double marks2[] = {30,40,50,60,70};
double marks3[] = {40,50,60,70,80};
student[] s = new student[3];
s[0] = new student ( 2, "vansh",20,marks1);
s[1] = new student ( 3, "lalu",20,marks2);
s[2] = new student ( 4, "farah",20,marks3);

double avg1 = s[0].average();


double avg2 = s[1].average();
double avg3 = s[2].average();
if (avg1> avg2 && avg1> avg3) {
s[0].displayDetails();
}
if(avg2>avg1 && avg2>avg3){
s[1].displayDetails();
}
else{
s[2].displayDetails();
}
double marks4[] = {50,60,70,80,90};
student s1 = new student(5,"shivam",20,marks4);
int i = student.getCount();
System.out.println("total number of object are made: " + i );
}
}

Output:

You might also like