You are on page 1of 9

NAME EREEN NCUBE

STUDENT ID L0225996V

FACULTY APPLIED SCIENCES AND ENGINEERING

PROGRAMME BSCH IN INFORMATION TECHNOLOGY

COURSE EAIT 2103 OBJECT ORIENTED PROGRAMING

PART 2.1

ASSIGNMENT 1
QUESTION 1

Write a program to print the area of two rectangles having sides (4,5) and (5,8) respectively by
creating a class named 'Rectangle' with a method named 'Area' which returns the area and length and
breadth passed as parameters to its constructor.

package com.mycompany.rectangle;

public class Rectangle {

public static void main(String[] args) {

int length = 5, breadth = 4 ;

int length1 = 8 , breadth1 = 5;

int areaA = length * breadth;

int areaB = length1 * breadth1;

System.out.println("Area for first rectangle = " + areaA);

System.out.println("Area for second rectangle = " + areaB);

}
QUESTION 2

Write a program to print the area of a rectangle by creating a class named 'Area' having two methods.
First method named as 'setDim' takes length and breadth of rectangle as parameters and the second
method named as 'getArea' returns the area of the rectangle. Length and breadth of rectangle are
entered through keyboard.

package com.mycompany.area;

import java.util.Scanner;

public class Area {

int length,breadth;

void setDim(int l,int b){

length = l;

breadth=b; }

int getArea(){

return length*breadth; }

public static void main(String[]args) {

Scanner obj = new Scanner(System.in);

int length,breadth;

System.out.println("Enter length :");

length=obj.nextInt();

System.out.println("Enter breadth :");

breadth=obj.nextInt();

Area rect=new Area();

rect.setDim(length,breadth);

System.out.println("The area of the rectangle =: "+rect.getArea()); obj.close();


}

QUESTION 3

Print the average of three numbers entered by user by creating a class named 'Average' having a
method to calculate and print the average.

package com.mycompany.average;

import java.util.Scanner;

public class Average {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.print("Input the first number: ");

double x = in.nextDouble();

System.out.print("Input the second number: ");

double y = in.nextDouble();
System.out.print("Input the third number: ");

double z = in.nextDouble();

System.out.print("The average value is " + average(x, y, z)+"\n" );

public static double average(double x, double y, double z)

return (x + y + z) / 3;

QUESTION 4

Print the sum, difference and product of two complex numbers by creating a class named 'Complex'
with separate methods for each operation whose real and imaginary parts are entered by user.
package com.mycompany.complex;

import java.util.Scanner;

public class Complex {

double real;

double imag;

public Complex(double real, double imag) {

this.real = real;

this.imag = imag;

public static void main(String[] args) {

Scanner obj = new Scanner(System.in);

double real;

double imag;

System.out.println("Enter real number :");

real=obj.nextInt();

System.out.println("Enter imag number :");

imag=obj.nextInt();

Complex n1 = new Complex(real,imag)

System.out.println("Enter real number :");


real=obj.nextInt();

System.out.println("Enter imag number :");

imag=obj.nextInt();

Complex n2 = new Complex(real, imag),sum,diff,prod;

sum= add(n1, n2);

System.out.printf("Sum = %.1f + %.1fi", sum.real, sum.imag);

diff= subtract(n1, n2);

System.out.printf(" difference = %.1f + %.1fi", diff.real, diff.imag);

prod= mult(n1,n2);

System.out.printf(" product = %.1f + %.1fi", prod.real, prod.imag);

public static Complex add(Complex n1, Complex n2)

Complex sum = new Complex(0.0, 0.0);

sum.real = n1.real + n2.real;

sum.imag = n1.imag + n2.imag;

return(sum);

public static Complex subtract(Complex n1, Complex n2){

Complex diff = new Complex(0.0, 0.0);

diff.real = n1.real - n2.real;


diff.imag = n1.imag - n2.imag;

return(diff);

public static Complex mult(Complex n1, Complex n2){

Complex prod = new Complex(0.0, 0.0);

prod.real = n1.real * n2.real;

prod.imag = n1.imag * n2.imag;

return(prod);

QUESTION 5

Write a program that would print the information (name, year of joining, salary, address) of three
employees by creating a class named 'Employee'. The output should be as follows:

Name Year of joining Address


Robert 1994 64C- WallsStreat
Sam 2000 68D- WallsStreat
John 1999 26B- WallsStreat
package com.mycompany.employee;

public class Employee {

public static void main(String[] args) {

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

System.out.print("Name Year of joining Address\n");

for(int n =1; n<2; n++){

System.out.println("""

Robert \t 1994 \t 64-Wall

sam \t 2000 \t 64D-Wall

John \t 1999 \t 26B;Wall|n""");

You might also like