Uploaded by Madhav Jain

upload

advertisement
Q1. 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
triangle. The default
values are 1 for both width and height.
a. A default constructor that creates a default rectangle.
b. A constructor that creates a rectangle with the specified width and height.
c. A method named getArea() that returns the area of this rectangle.
d. 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.
Code: import java.util.*;
class Rectangle {
double width;
double height;
Rectangle() {
width = height = 1;
}
Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
double getArea() {
return width * height;
}
double getPerimeter() {
return 2 * (width + height);
}
}
public class classes {
public static void main(String[] args) {
Rectangle r1 = new Rectangle();
System.out.println("Rectangle r1(default):-");
System.out.println("Width = " + r1.width + "\nHeight = " + r1.height);
System.out.println("Area = " + r1.getArea() + "\nHeight = " +
r1.getPerimeter());
double x, y, z, p;
try (Scanner sc = new Scanner(System.in)) {
System.out.print("Enter the width and height of rectangle r2: ");
x = sc.nextDouble();
y = sc.nextDouble();
Rectangle r2 = new Rectangle(x, y);
System.out.println("Rectangle r2:-");
System.out.println("Width = " + r2.width + "\nHeight = " +
r2.height);
System.out.println("Area = " + r2.getArea() + "\nHeight = " +
r2.getPerimeter());
System.out.print("Enter the width and height of rectangle r3: ");
z = sc.nextDouble();
p = sc.nextDouble();
}
Rectangle r3 = new Rectangle(z, p);
System.out.println("Rectangle r3:-");
System.out.println("Width = " + r3.width + "\nHeight = " + r3.height);
System.out.println("Area = " + r3.getArea() + "\nHeight = " +
r3.getPerimeter());
}
}
Output: -
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 9pointers.
Code: import java.util.*;
class Student {
public String regno, name, course;
public float cgpa;
Student(String regno, String name, String course, float cgpa) {
this.regno = regno;
this.name = name;
this.course = course;
this.cgpa = cgpa;
}
void display() {
System.out.println("Student details!");
System.out.println("Registration Number: " + regno);
System.out.println("Name: " + name);
System.out.println("Course: " + course);
System.out.println("CGPA: " + cgpa + "\n\n");
}
}
public class da2q2 {
public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in)) {
Student[] s = new Student[10];
String regno, name, course;
float cgpa;
for (int i = 0; i < 10; i++) {
System.out.println("\nEnter details of student " + (i + 1) +
": ");
System.out.println("Enter
regno = sc.next();
System.out.println("Enter
name = sc.next();
System.out.println("Enter
course = sc.next();
System.out.println("Enter
cgpa = sc.nextFloat();
regno: ");
name: ");
course: ");
cgpa: ");
s[i] = new Student(regno, name, course, cgpa);
}
System.out.println("9 pointer students: ");
for (int i = 0; i < 10; i++) {
if (s[i].cgpa >= 9) {
s[i].display();
}
;
}
}
}
}
Output: -
Q 3. Write a Java program that displays area of different Figures (Rectangle,
Square, Triangle) using the method overloading.
Code: import java.util.*;
public class shapes {
public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in)) {
int a, b;
float c, d;
char ch;
do {
System.out.print("****Keywords for different shapes****\n");
System.out.println("circle:- c \nrectangle:- r \nsquare:- s
\ntraingle:- t \n0-exit!");
System.out.println("Enter the shape you want area of: ");
ch = sc.next().charAt(0);
if (ch == 'r') {
System.out.println("Enter the length and width of
rectangle: - ");
a = sc.nextInt();
b = sc.nextInt();
System.out.println("Area of rectangle = " + area(a, b) +
"\n");
} else if (ch == 'c') {
System.out.println("Enter the radius of circle: - ");
d = sc.nextFloat();
System.out.println("Area of cirlce = " + area(d) + "\n");
} else if (ch == 's') {
System.out.println("Enter the side of sqaure: -");
a = sc.nextInt();
System.out.println("Area of square = " + area(a) + "\n");
} else if (ch == 't') {
System.out.println("Enter the base and height of the
triangle: -");
c = sc.nextFloat();
d = sc.nextFloat();
System.out.println("Area of triangle = " + area(c, d) +
"\n");
} else {
System.out.println("Keyword not recognized!!");
}
} while (ch != '0');
}
}
static int area(int n, int m) {
return (n * m);
}
static int area(int l) {
return (l * l);
}
static double area(float z) {
return (3.1415 * z * z);
}
static double area(float x, float y) {
return (0.5 * x * y);
}
}
Output: -
Download