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 [Link].*;
import [Link].*;
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);
[Link]("Width: " + [Link]());
[Link]("Height: " + [Link]());
[Link]("Area: " + [Link]() + "\n");
[Link]("Perimeter: " + [Link]());
[Link]("Width: " + [Link]());
[Link]("Height: " + [Link]());
[Link]("Area: " + [Link]());
[Link]("Perimeter: " + [Link]());
}
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 [Link].*;
import [Link].*;
public class Student {
Scanner s=new Scanner([Link]);
String Regno;
String name;
String course;
double cgpa;
public void display(){
if(cgpa>=9){
[Link](" 9 pointer"+"\n");
}
else{
[Link]("Not a 9 pointer"+"\n");
}
}
public Student(){
Regno=[Link]();
name=[Link]();
course=[Link]();
cgpa=[Link]();
}
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++){
[Link]("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.