Java Assignment

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Assignment on Java Programming Language

1. Write a Java program to print the area and perimeter of a rectangle.


public class areaperimeter {
public static void main(String[] args) {
double a=2.5;
double b=6.8;
double perimeter=2*(a+b);
double area=a*b;
System.out.println("perimeter"+perimeter);
System.out.println("area"+area);
}
}
Input Expected Output Actual Output Status
2.5 Perimeter is 2*(6.8 + 2.5) = perimeter18.6 executed
6.8 18.60\n area17.0
Area is 2.5 * 6.8 = 17.00

2. Write a Java program and compute the sum of an integer's digits.


public class sum {
public static void main(String[] args) {
int number = 234;
int sum = 0;
int tempNumber = Math.abs(number);
while (tempNumber > 0) {
sum += tempNumber % 10;
tempNumber /= 10;
}
System.out.println("The sum of the digits is: " + sum);
}
}
Input Expected Output Actual Output Status
234 The sum of the digits is: 9 The sum of the digits is: 9 executed

3. Write a Java program to make such a pattern like a right angle triangle with the number
increased by 1. (Remember to match the output given exactly, including the spaces and new
lines)
for n=3:
1
23
456
public class PATTERN{
public static void main(String[] args) {
int n = 6;
int number = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(number + " ");
number++;
}
System.out.println();
}}
Input Expected Output Actual Output Status
6 1 \n 1 executed
2 3 \n 23
4 5 6 \n 456
7 8 9 10 \n 7 8 9 10
11 12 13 14 15 \n 11 12 13 14 15
16 17 18 19 20 21 16 17 18 19 20 21

4. Write a Java program to convert an integer number to a binary number.


public class converttobinary {
public static void main(String[] args) {
int number = 123;
String binary = Integer.toBinaryString(number);
System.out.println("The binary representation of " + number + " is: "
+ binary);
}}
Input Expected Output Actual Output Status
123 Binary number is: 1111011 The binary representation of 123 is: executed
1111011

5. Create a class Student with private attributes for name and age. Use a constructor to
initialize these attributes and provide public getter methods to access them.In the
main method, an instance of Student is created and the student's details are printed.
Guideline to Solve:
 Define the Student class with private attributes.
 Use a constructor to initialize the attributes.
 Implement getter methods for the attributes.
class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
public class studentclass{
public static void main(String[] args) {
Student student = new Student("Bob", 21);
System.out.println("Student Name: " + student.getName());
System.out.println("Student Age: " + student.getAge());
}
}

Input Expected Output Actual Output Status


Bob Student Name: Bob\n Student Name: Bob executed
21 Student Age: 21 Student Age: 21
6. Create a class Rectangle with attributes length and width. Provide two constructors: one
with no parameters (default to 1) and another with parameters to initialize the attributes.
Use the this keyword to avoid name space collision.Create a getArea() function that returns
the area of the rectangle.
Guideline to Solve:

 Define the Rectangle class with attributes and constructors.


 Define a default Rectangle constructor that inializes length and width to 1.
 Use the this keyword in the parameterized constructor.
 Define a getArea() funtion that returns the area of their rectangle.
class Rectangle {
private double length;
private double width;
public Rectangle() {
this.length = 1.0;
this.width = 1.0;
}
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double getArea() {
return this.length * this.width;
}
public void display() {
System.out.println("L, W, A: " + this.length + ", " + this.width + ",
" + this.getArea());
}
}
public class rectdemo {
public static void main(String[] args) {
Rectangle defaultRectangle = new Rectangle();
System.out.print("Default Rectangle: ");
defaultRectangle.display();
Rectangle parameterizedRectangle = new Rectangle(2.0, 3.0);
System.out.print("Parameterized Rectangle: ");
parameterizedRectangle.display();
}
}
Input Expected Output Actual Output Status
2 Default Rectangle: L, W, A : 1.0, Default Rectangle: L, W, A: 1.0, executed
3 1.0, 1.0\n 1.0,1.0
Parameterised Rectangle: L, W, Parameterized Rectangle: L, W,
A : 2.0, 3.0, 6.0 A: 2.0, 3.0,6.0

7. Create a class Circle that encapsulates the properties of a circle. The class should have a
private field for the radius, a constructor to initialize the radius, and methods to calculate
the area and circumference of the circle.
NOTE: use Math.PI for PI calculations

Guideline to Solve:
 Define the Circle class with attributes and constructors.
 Use this keyword in the parameterized constructor.
 Define a getArea() funtion that returns the area of Circle (use Math.PI)
 Define a getCircumference() funtion that returns the circumference of Circle (use
Math.PI)

Input Expected Output Actual Output Status


1.0 Area: 3.14\n Area: 3.141592653589793 executed
Circumference: 6.28 Circumference:
6.283185307179586

8. Create a class Department having a method getCourses that prints "These are the
department's courses". It will have two subclasses, ComputerScience and
MechanicalEngineering, each having a method with the same name that prints specific
courses for the respective departments. Call the method by creating an object of each of the
three classes.
class Department {
public void getCourses() {
System.out.println("These are the department's courses.");
}
}
class ComputerScience extends Department {
public void getCourses() {
System.out.println("Courses: Data Structures, Algorithms, Operating
Systems");
}
}
class MechanicalEngineering extends Department {
public void getCourses() {
System.out.println("Courses: Thermodynamics, Fluid Mechanics, Heat
Transfer");
}
}
public class main {
public static void main(String[] args) {
Department cs = new ComputerScience();
cs.getCourses();
Department me = new MechanicalEngineering();
me.getCourses();
}
}
Input Expected Output Actual Output Status
Courses: Data Structures, Courses: Data Structures, executed
Algorithms, Operating Algorithms, Operating Systems
Systems\n Courses: Thermodynamics, Fluid
Courses: Thermodynamics, Mechanics, Heat Transfer
Fluid Mechanics, Heat Transfer
9. There are two class cls1 and cls2 which is subclass of cls1. cls1 having a method "add" which
add two numbers. Create two method inside cls2 which will take 2 parameters as input i.e. a
and b and print the sum, multiplication and sum of their squares i.e (a^2) + (b2).

class Cls1
{ public int add(int a, int b)
{ return a + b;
}}
class Cls2 extends Cls1
{public int multiply(int a, int b)
{ return a * b;
}
public int sumOfSquares(int a, int b)
{ return (a * a) + (b * b);
}}
public class clsdemo
{ public static void main(String[] args)
{ Cls2 cls2 = new Cls2();
System.out.println(cls2.add(3, 5));
System.out.println(cls2.multiply(3, 5));
System.out.println(cls2.sumOfSquares(3, 5));
}}
Input Expected Output Actual Output Status
3 8\n 8 executed
5 15\n 15
34 34

10. Write a Java program to swap two numbers using call by object reference.
class Swap {
int value;
Swap(int value) {
this.value = value;
}
}
public class swapnumber{
public static void main(String[] args) {
Swap a = new Swap(6);
Swap b = new Swap(11);
System.out.println("Before Swap: " + a.value + " " + b.value);
int temp = a.value;
a.value = b.value;
b.value = temp;
System.out.println("After Swap: " + a.value + " " + b.value);
}
}
Input Expected Output Actual Output Status
6 Before Swap: 6 11 Before Swap: 6 11 executed
11 After Swap: 11 6 After Swap: 11 6
11. Write a program to create a method that takes an integer as a parameter and throws an
exception if the number is odd.
public class evenodd{
public static void checkNumber(int number) throws Exception {
if (number % 2 != 0) {
throw new Exception(number + " is odd.");
} else {
System.out.println(number + " is even");
}
}
public static void main(String[] args) {
int[] numbers = {9, 7, 8};
for (int number: numbers) {
try {
checkNumber(number);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
}
Input Expected Output Actual Output Status
9 Error: 9 is odd. 9 is odd. executed
7 Error: 7 is odd. 7 is odd.
8 8 is even 8 is even

12. Write a program to create an interface Flyable with a method called fly_obj(). Create three
classes Spacecraft, Airplane, and Helicopter that implement the Flyable interface. Implement
the fly_obj() method for each of the three classes.

Input Expected Output Actual Output Status


Spacecraft is flying Spacecraft is flying executed
Airplane is flying Airplane is flying
Helicopter is flying Helicopter is flying

You might also like