You are on page 1of 12

Lab Assignment 1 – CSF206 Advanced Java Programming, Even Sem, 2022

Your Name: Madhava Yedla SAP ID: 1000014908 Major: CSE

Instructions
 In this lab assignment, you will solve problem(s) based on what you have learnt in
objects, classes, inheritance and polymorphism.
 There are 4 questions in this assignment.
 Email/paper/other modes of submissions will not be accepted.
 Upload a pdf version of this document.

Due Date: 10 pm, Jan 21, 2022.

Submitting this Assignment

You will submit (upload) this assignment in MS Teams. Name this document as
A1_AJPEVEN2022_John_Doe.pdf in case your name is John Doe. Paste your code and
snapshot of output after each question, save and upload the document.

Grading: 0 points. Submission for each student is mandatory.

Question 1: Write a program to create a class Rectangle with the following information:
length, breadth in integer.
 Add two constructors, (a) default constructor, and (b) constructor to pass on length
and breadth of a Rectangle.
 Add a method printData( ) to print the two information about the rectangle in con-
sole.
 Add methods printArea( ) and printPerimeter( ) to compute and print the area and
perimeter of rectangle in console.
 Create two objects of this class, r1 and r2. Show the output of both the constructors
and all method of these two objects.

Question 2: Write a program to create a class Circles with the following information:
radius in integer.
 Add two constructors, (a) default constructor, and (b) constructor to pass on radius
of a Circle.
 Add a method printData( ) to print the radius of the Circle in console.
 Add methods printArea( ) and printPerimeter( ) to compute and print the area and
perimeter of Circle in console.
Lab Assignment 1 – CSF206 Advanced Java Programming, Even Sem, 2022

Your Name: Madhava Yedla SAP ID: 1000014908 Major: CSE

 Create two objects of this class, c1 and c2. Show the output of both the constructors
and all methods of these two objects.

Question 3: Design and implement a class named RightangleTriangle that extends a


GeometricObject class. The class contains:
 Two double data fields named baze and hite with default
 values 3.0 and 5.0 to denote base and height of the right-angle triangle.
 A no-arg constructor that creates a default triangle.
 A constructor that creates a triangle with the specified base and height.
 The accessor methods (getter and setter) for all the data fields.
 A method named getArea() that returns the area of this triangle.
 A method named printData() to print all data field values in console.

The superclass  GeometricObject class should have following data fields: private String
color = "white"; private java.util.Date dateCreated;
 Implement all types of constructors to initialize and set these data fields. Use super
keyword to set these values in your triangle class.
 Implement another class mainClass where you create an object t1 of the Rightangle-
Triangle class and print its area and the value of all its data fields.

Question 4: Write a program that includes a class named Person and its two subclasses
named Student and Employee.
 Make Faculty and Staff subclasses of Employee.
 A person has a name, address, phone number, and email address.
 A student has a class status (freshman, sophomore, junior, or senior). Define the sta-
tus as a constant.
 An employee has an office, salary, and date hired.
 Use Java API to get the current date as the date hired.
 A faculty member has office hours and a rank.
 A staff member has a title.
 Create a printData( ) method in each class to print the value of its data fields.
 Override this method in subclasses – Faculty, Staff and Student to print output of all
data fields (in addition to the parent class data fields).
Lab Assignment 1 – CSF206 Advanced Java Programming, Even Sem, 2022

Your Name: Madhava Yedla SAP ID: 1000014908 Major: CSE

ANSWERS

1st Question Code:


package com.java;

public class Rectangle {


private int length;
private int breadth;

Rectangle(){

// default constructor

System.out.println("Name : Madhava Yedla\nSap id:1000014908\n");


}

public Rectangle(int length, int breadth){

// constructor to pass length and breadth


this.length = length;
this.breadth = breadth;

public void printData(){

System.out.println("Length:" + length + " " + "Breadth:" + breadth);


}

public void printArea(){

// method to print area of the rectangle


System.out.println("Area of given rectangle is:" + length * breadth);
}

public void printPerimeter(){

// method to print perimeter of the rectangle

System.out.println("Perimeter of the given rectangle is:" + 2*(length


Lab Assignment 1 – CSF206 Advanced Java Programming, Even Sem, 2022

Your Name: Madhava Yedla SAP ID: 1000014908 Major: CSE


+ breadth));
}
}

class Main{

public static void main(String[] args) {

// object r1 to show output of default constructor


Rectangle r1 = new Rectangle();
r1.printData();
r1.printArea();
r1.printPerimeter();

// object r2 to show output of another constructor

Rectangle r2 = new Rectangle(2,8);


r2.printData();
r2.printArea();
r2.printPerimeter();

}
}
}

OUTPUT:
Lab Assignment 1 – CSF206 Advanced Java Programming, Even Sem, 2022

Your Name: Madhava Yedla SAP ID: 1000014908 Major: CSE

2st Question Code:


package com.java;

public class Circles {


private int radius;

Circles(){

// default constructor

System.out.println("Name : Madhava Yedla\nSap id:1000014908\n");


}

Circles(int radius){
// constructor to pass radius
this.radius = radius;
}

public void printData(){

System.out.println("Radius :" + radius);


}

public void printArea(){

// method to print area of the rectangle

System.out.println("Area of given circle is:" + 3.14 *(radius * ra-


dius) );

public void printPerimeter(){

// method to print perimeter of the rectangle

System.out.println("Perimeter of given circle is:" + 2 * 3.14 * ra-


dius);
}
Lab Assignment 1 – CSF206 Advanced Java Programming, Even Sem, 2022

Your Name: Madhava Yedla SAP ID: 1000014908 Major: CSE


}

class circle{

public static void main(String[] args) {

// object c1 to show output of default constructor

Circles c1 = new Circles();


c1.printData();
c1.printArea();
c1.printPerimeter();

// object c2 to show output of another constructor


Circles c2 = new Circles(3);
c2.printData();
c2.printArea();
c2.printPerimeter();
}
}

OUTPUT:
Lab Assignment 1 – CSF206 Advanced Java Programming, Even Sem, 2022

Your Name: Madhava Yedla SAP ID: 1000014908 Major: CSE

3rd Question Code:


package com.java;

import java.time.Instant;
import java.util.Date;

public class GeometricObject{

private String color;


private Date datecreated;

public GeometricObject(){
color = "white";
datecreated=Date.from(Instant.now());
}

public GeometricObject(String color, Date datecreated){


this.color = color;
this.datecreated = datecreated;
}

public String getColor(){


return color;
}

public Date getDatecreated(){


return datecreated;
}

class Righttriangle extends GeometricObject{


private double baze;
private double hite;

public Righttriangle() {

baze = 3.0;
hite =5.0;

public Righttriangle(double base, double hieght, String color, Date cre-


ateDate){
Lab Assignment 1 – CSF206 Advanced Java Programming, Even Sem, 2022

Your Name: Madhava Yedla SAP ID: 1000014908 Major: CSE

super(color, createDate);
baze = base;
hite = hieght;

public void setBase(double base){


this.baze = base;
}

public double getBase(){


return baze;
}

public void setHieght(double hieght){


this.hite = hieght;
}

public double getHieght(){


return hite;
}

public double getArea(){


return (baze * hite)/2;
}

public void printData(){


System.out.println("Base:" + baze + " " + "Height:" + hite + " " +
"Color:" + getColor() + " " +
"Created Date:" + getDatecreated());
}
}

class Main{
public static void main(String[] args) {
Righttriangle t1 = new Righttriangle(3,4,"red", Date.from(In-
stant.now()));

System.out.println("Area :" + t1.getArea());


t1.printData();
}
}
Lab Assignment 1 – CSF206 Advanced Java Programming, Even Sem, 2022

Your Name: Madhava Yedla SAP ID: 1000014908 Major: CSE

OUTPUT:

4th Question Code:

package com.java;

import java.util.Scanner;

class Person {
String name,address,phoneNumber,emailAddress;

Person(String n, String a, String p, String e) {


name = n;
address = a;
phoneNumber = p;
emailAddress = e;
}

void printData() {
System.out.println("Name : " + name);
System.out.println("Address : " + address);
System.out.println("Phone number : " + phoneNumber);
System.out.println("Email Address : " + emailAddress);
}

class Student extends Person {

Student(String n, String a, String p, String e) {


super(n,a,p,e);
}

enum status {
Lab Assignment 1 – CSF206 Advanced Java Programming, Even Sem, 2022

Your Name: Madhava Yedla SAP ID: 1000014908 Major: CSE


FRESHMAN, SOPHOMORE, JUNIOR, SENIOR;
}

status c = status.JUNIOR;

void printData() {
System.out.println("STUDENT");
super.printData();
System.out.println("Status : " + c);
}

class Employee extends Person {


String office, dateHired;
double salary;

Employee(String n, String a, String p, String e, String o, String d,


double s) {
super(n,a,p,e);
office = o;
dateHired = d;
salary = s;
}

void printData() {
super.printData();
System.out.println("Office : " + office);
System.out.println("Date hired : " + dateHired);
System.out.println("Salary : " + salary);
}

class Faculty extends Employee {


String rank;
int officeHours;
Faculty(String n, String a, String p, String e, String o, String d, double
s, String r, int oh) {
super(n,a,p,e,o,d,s);
rank = r;
officeHours = oh;
}
void printData() {
System.out.println("FACULTY");
super.printData();
Lab Assignment 1 – CSF206 Advanced Java Programming, Even Sem, 2022

Your Name: Madhava Yedla SAP ID: 1000014908 Major: CSE


System.out.println("Rank : " + rank);
System.out.println("Office Hours : " + officeHours);
}
}

class Staff extends Employee {


String title;
Staff(String n, String a, String p, String e, String o, String d, double
s, String t) {
super(n,a,p,e,o,d,s);
title = t;
}
void printData() {
System.out.println("STAFF");
super.printData();
System.out.println("Title : " + title);
}
}

OUTPUT:
Lab Assignment 1 – CSF206 Advanced Java Programming, Even Sem, 2022

Your Name: Madhava Yedla SAP ID: 1000014908 Major: CSE

You might also like