You are on page 1of 4

SCHOOL OF COMPUTER SCIENCE AND ENGINEERING

Fall Semester 2020-21

Name: Jaanaavi Wasade


Registration Number:18BCE0743
Course Code: CSE1007
Course Name: Java Programming
Slot: L25 +L26
Faculty: Priya G
PROBLEMS BASED ON
CLASSES AND OBJECTS

1. Design a class named Rectangle to represent a rectangle. The class contains:


Two double data fields named width and height that specify the width and
height of the rectangle. The default values are 1 for both width and height.

(i)A default constructor that creates a default rectangle.


(ii)A constructor that creates a rectangle with the specified width and height.
(iii)A method named getArea() that returns the area of this rectangle.
(iv)A method named getPerimeter() that returns the perimeter. Implement the
class.

Write a test program that creates two Rectangle objects— one with width 5 and
height 50 and the other with width 2.5 and height 45.7. Display the width,
height, area, and perimeter of each rectangle in this order.

import java.util.*;
import java.lang.*;
public class Rectangle {
double height;
double width;
public Rectangle(double w, double h){
height = h;
width = w;
}
public Rectangle(){
height = 1;
width = 1;
}
public double getArea(){
return height*width;
}
public double getPerimeter(){
return 2*(height + width);
}
public double getWidth(){
return width;
}
public double getHeight(){
return height;
}
public static void main(String[] args){
Rectangle b1 = new Rectangle(5, 50);
Rectangle b2 = new Rectangle(2.5, 45.7);
System.out.println("Width: " + b1.getWidth());
System.out.println("Height: " + b1.getHeight());
System.out.println("Area: " + b1.getArea() + "\n");
System.out.println("Perimeter: " + b1.getPerimeter());
System.out.println("Width: " + b2.getWidth());
System.out.println("Height: " + b2.getHeight());
System.out.println("Area: " + b2.getArea());
System.out.println("Perimeter: " + b2.getPerimeter());
}

Q2. Write a Java program to create a class called Student having data members Regno,
Name, Course being studied and current CGPA. Include constructor to initialize objects.
Create array of objects with at least 10 students and find 9- pointers.

import java.util.*;
import java.lang.*;
public class Student {
Scanner s=new Scanner(System.in);
String Regno;
String name;
String course;
double cgpa;

public void display(){


if(cgpa>=9){
System.out.print(" 9 pointer"+"\n");

}
else{
System.out.print("Not a 9 pointer"+"\n");
}
}
public Student(){
Regno=s.nextLine();
name=s.nextLine();
course=s.nextLine();
cgpa=s.nextDouble();
}
public static void main(String[] args){

Student a[]=new Student[10];


for (int i=0;i<10;i++)
{
a[i]=new Student();

}
for (int i=0;i<10;i++){
System.out.print("Student "+(i+1)+":");

a[i].display();

}
Q3. Write a Java program that displays that displays the time in different formats in the form of
HH,MM,SS using constructor Overloading.

You might also like