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

Assignment 2 Complete

The document contains multiple Java programming assignments focusing on classes, objects, and methods. It includes examples of calculating areas for shapes like circles and cylinders using inheritance and interfaces, checking integer values, defining a student class, and manipulating an ArrayList. Each section provides code snippets demonstrating the concepts discussed.

Uploaded by

weralij635
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)
11 views5 pages

Assignment 2 Complete

The document contains multiple Java programming assignments focusing on classes, objects, and methods. It includes examples of calculating areas for shapes like circles and cylinders using inheritance and interfaces, checking integer values, defining a student class, and manipulating an ArrayList. Each section provides code snippets demonstrating the concepts discussed.

Uploaded by

weralij635
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

Assignment No.

2: Classes, Objects and Methods

Set A

Write a java program to calculate area of Cylinder and Circle. (Use super keyword)

class Circle {

double radius;

Circle(double r) {

radius = r;

double area() {

return Math.PI * radius * radius;

class Cylinder extends Circle {

double height;

Cylinder(double r, double h) {

super(r);

height = h;

double area() {

return 2 * Math.PI * radius * height + 2 * super.area();

public class Main {

public static void main(String[] args) {

Cylinder c = new Cylinder(2.5, 5);

System.out.println("Area of Cylinder: " + c.area());

}
Assignment No. 2: Classes, Objects and Methods

Define an Interface Shape with abstract method area(). Write a java program to calculate an area of

interface Shape {

double area();

final class Circle implements Shape {

final double radius;

Circle(double r) {

radius = r;

public double area() {

return Math.PI * radius * radius;

final class Sphere implements Shape {

final double radius;

Sphere(double r) {

radius = r;

public double area() {

return 4 * Math.PI * radius * radius;

public class Main {

public static void main(String[] args) {

Shape c = new Circle(2.5);

Shape s = new Sphere(2.5);


Assignment No. 2: Classes, Objects and Methods

System.out.println("Area of Circle: " + c.area());

System.out.println("Area of Sphere: " + s.area());

Define an Interface "Integer" with a abstract method check().Write a Java program to check whether

interface IntegerCheck {

void check(int num);

public class NumberCheck implements IntegerCheck {

public void check(int num) {

if (num > 0)

System.out.println("Positive Number");

else if (num < 0)

System.out.println("Negative Number");

else

System.out.println("Zero");

public static void main(String[] args) {

NumberCheck obj = new NumberCheck();

obj.check(-15);

Define a class Student with attributes rollno and name. Define default and parameterized constructo

class Student {
Assignment No. 2: Classes, Objects and Methods

int rollno;

String name;

static int count = 0;

Student() {

count++;

Student(int r, String n) {

this.rollno = r;

this.name = n;

count++;

public String toString() {

return "Roll No: " + rollno + ", Name: " + name;

public static void main(String[] args) {

Student s1 = new Student(1, "John");

System.out.println(s1);

System.out.println("Count: " + Student.count);

Student s2 = new Student(2, "Alice");

System.out.println(s2);

System.out.println("Count: " + Student.count);

Write a java program to accept 'n' integers from the user & store them in an ArrayList collection. Dis
Assignment No. 2: Classes, Objects and Methods

import java.util.*;

public class ReverseArrayList {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

ArrayList<Integer> list = new ArrayList<>();

System.out.print("Enter number of elements: ");

int n = sc.nextInt();

for (int i = 0; i < n; i++) {

System.out.print("Enter element: ");

list.add(sc.nextInt());

Collections.reverse(list);

System.out.println("Reversed List: " + list);

You might also like