[go: up one dir, main page]

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

Jav Assignment

Uploaded by

frontech990
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)
16 views5 pages

Jav Assignment

Uploaded by

frontech990
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

BHARATH CHOWDRY DS 09/07/2024

1.Create a class called shape declare necessary variables to calculate area and
volume of that shape using method over loading.

import java.util.Scanner;
class Shape {
double calculateArea(double length, double width) {
return length * width;
}
double calculateArea(double radius) {
return Math.PI * radius * radius;
}
double calculateVolume(double sideLength) {
return sideLength * sideLength * sideLength;
}
double calculateVolume(double radius, double height) {
return Math.PI * radius * radius * height;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Shape shape = new Shape();
System.out.print("Enter length and width of rectangle: ");
double length = scanner.nextDouble();
double width = scanner.nextDouble();
double rectangleArea = shape.calculateArea(length, width);
System.out.println("Area of rectangle: " + rectangleArea);
System.out.print("Enter radius of circle: ");
double radius = scanner.nextDouble();
BHARATH CHOWDRY DS 09/07/2024

double circleArea = shape.calculateArea(radius);


System.out.println("Area of circle: " + circleArea);
System.out.print("Enter side length of cube: ");
double sideLength = scanner.nextDouble();
double cubeVolume = shape.calculateVolume(sideLength);
System.out.println("Volume of cube: " + cubeVolume);
System.out.print("Enter radius and height of cylinder: ");
radius = scanner.nextDouble();
double height = scanner.nextDouble();
double cylinderVolume = shape.calculateVolume(radius, height);
System.out.println("Volume of cylinder: " + cylinderVolume);

scanner.close();
}

}
2.Define a class with student marks calculate total and
averagemarksof the student using constructor overloading

import java.util.Scanner;

class Student {
private String name;
private int[] marks;
private int totalMarks;
private double averageMarks;
public Student() {
this.name = "";
this.marks = new int[0];
this.totalMarks = 0;
this.averageMarks = 0.0;
}
public Student(String name, int[] marks) {
this.name = name;
this.marks = marks;
calculateTotalAndAverage();
}
private void calculateTotalAndAverage() {
totalMarks = 0;
for (int mark : marks) {
totalMarks += mark;
}
if (marks.length > 0) {
averageMarks = (double) totalMarks / marks.length;
} else {
averageMarks = 0.0;
}
}
public void displayStudentInfo() {
System.out.println("Student Name: " + name);
System.out.println("Total Marks: " + totalMarks);
System.out.println("Average Marks: " + averageMarks);
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter student name: ");
String name = scanner.nextLine();
System.out.print("Enter number of subjects: ");
int numSubjects = scanner.nextInt();

int[] marks = new int[numSubjects];


for (int i = 0; i < numSubjects; i++) {
System.out.print("Enter mark for subject " + (i + 1) + ": ");
marks[i] = scanner.nextInt();
}

Student student = new Student(name, marks);

student.displayStudentInfo();
}
}

You might also like