Java Assignment
Java Assignment
Java Assignment
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
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());
}
}
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)
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.