Said Jamal Ramadhan DITA/7/23/001/TZ
Buda Amour Buda DITA/7/23/004/TZ
Ibrahim Ally Omar DITA/7/23/051/TZ
Assignment Java Coop
1. Create a class named Vehicle with two attributes named mileage and price.
Create its two subclasses
Car with attributes to store ownership cost, warranty (by years), seating
capacity and fuel type (diesel or petrol).
Bike with attributes to store the number of cylinders, number of gears,
cooling type (air, liquid or oil), wheel type (alloys or spokes) and fuel tank
size (in inches).
Create parameterized constructors of each classes mentioned above.
Codes:
Class Vehicle {
Double mileage;
Double price;
Vehicle (double mileage, double price) {
This. Mileage = mileage;
This. Price = price;
Class Car extends Vehicle {
Double ownership Cost;
Said Jamal Ramadhan DITA/7/23/001/TZ
Buda Amour Buda DITA/7/23/004/TZ
Ibrahim Ally Omar DITA/7/23/051/TZ
In warranty;
In seating Capacity;
String fuel Type;
Car (double mileage, double price, double ownership Cost, in warranty, in
seating Capacity, String fuel Type) {
Super (mileage, price);
this.ownershipCost = ownershipCost;
this.warranty = warranty;
this.seatingCapacity = seatingCapacity;
this.fuelType = fuelType;
class Bike extends Vehicle {
int cylinders;
int gears;
String coolingType;
String wheelType;
double fuelTankSize;
Said Jamal Ramadhan DITA/7/23/001/TZ
Buda Amour Buda DITA/7/23/004/TZ
Ibrahim Ally Omar DITA/7/23/051/TZ
Bike(double mileage, double price, int cylinders, int gears, String coolingType,
String wheelType, double fuelTankSize) {
super(mileage, price);
this.cylinders = cylinders;
this.gears = gears;
this.coolingType = coolingType;
this.wheelType = wheelType;
this.fuelTankSize = fuelTankSize;
2. Create a class named Person with three attributes named fullName, address and
age. Then, create its two sub-classes
Student with attributes to store registrationNumber, program and sponsor.
Lecturer with attributes to store employeeNumber, title and profession.
Create parameterized constructors of each classes mentioned above.
Codes:
class Person {
String fullName;
String address;
int age;
Person(String fullName, String address, int age) {
Said Jamal Ramadhan DITA/7/23/001/TZ
Buda Amour Buda DITA/7/23/004/TZ
Ibrahim Ally Omar DITA/7/23/051/TZ
this.fullName = fullName;
this.address = address;
this.age = age;
}
}
class Student extends Person {
int registrationNumber;
String program;
String sponsor;
Student(String fullName, String address, int age, int
registrationNumber, String program, String sponsor) {
super(fullName, address, age);
this.registrationNumber = registrationNumber;
this.program = program;
this.sponsor = sponsor;
}
}
class Lecturer extends Person {
int employeeNumber;
String title;
String profession;
Lecturer(String fullName, String address, int age, int
employeeNumber, String title, String profession) {
super(fullName, address, age);
this.employeeNumber = employeeNumber;
this.title = title;
this.profession = profession;
Said Jamal Ramadhan DITA/7/23/001/TZ
Buda Amour Buda DITA/7/23/004/TZ
Ibrahim Ally Omar DITA/7/23/051/TZ
}
}
3. Create a class named Shape with a function named “printMe()” that prints "This
is a shape".
Create another class named Polygon inheriting the Shape class with the same
function named “printMe()” that prints "Polygon is a shape".
Create two other classes named Rectangle and Triangle having the same
function which prints "Rectangle is a polygon" and "Triangle is a polygon"
respectively. Again, make another class named Square having the same
function which prints "Square is a rectangle".
Now, try calling the functions by the object of each of these classes.
Codes:
1.) class Shape {
void printMe() {
System.out.println("This is a shape");
}
}
class Polygon extends Shape {
void printMe() {
System.out.println("Polygon is a shape");
}
}
class Rectangle extends Polygon {
void printMe() {
System.out.println("Rectangle is a polygon");
Said Jamal Ramadhan DITA/7/23/001/TZ
Buda Amour Buda DITA/7/23/004/TZ
Ibrahim Ally Omar DITA/7/23/051/TZ
}
}
class Triangle extends Polygon {
void printMe() {
System.out.println("Triangle is a polygon");
}
}
class Square extends Rectangle {
void printMe() {
System.out.println("Square is a rectangle");
}
}
public class Main {
public static void main(String[] args) {
Shape shape = new Shape();
shape.printMe();
Polygon polygon = new Polygon();
polygon.printMe();
Rectangle rectangle = new Rectangle();
rectangle.printMe();
Triangle triangle = new Triangle();
triangle.printMe();
Square square = new Square();
square.printMe();
Said Jamal Ramadhan DITA/7/23/001/TZ
Buda Amour Buda DITA/7/23/004/TZ
Ibrahim Ally Omar DITA/7/23/051/TZ
4. Create a class named Course with attributes courseCode, courseName, unit,
prerequisite, numberOfStudent. Then create getters and setters for each
attributes.
Codes:
1.) class Course {
private String courseCode;
private String courseName;
private int unit;
private String prerequisite;
private int numberOfStudent;
public String getCourseCode() {
return courseCode;
}
public void setCourseCode(String courseCode) {
this.courseCode = courseCode;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
Said Jamal Ramadhan DITA/7/23/001/TZ
Buda Amour Buda DITA/7/23/004/TZ
Ibrahim Ally Omar DITA/7/23/051/TZ
}
public int getUnit() {
return unit;
}
public void setUnit(int unit) {
this.unit = unit;
}
public String getPrerequisite() {
return prerequisite;
}
public void setPrerequisite(String prerequisite) {
this.prerequisite = prerequisite;
}
public int getNumberOfStudent() {
return numberOfStudent;
}
public void setNumberOfStudent(int numberOfStudent) {
this.numberOfStudent = numberOfStudent;
}
}
5. Write a class Person with properties name and age and a method introduce()
that prints a greeting saying “Hi, I’am ##### and I’am ## years old”.
Codes:
Said Jamal Ramadhan DITA/7/23/001/TZ
Buda Amour Buda DITA/7/23/004/TZ
Ibrahim Ally Omar DITA/7/23/051/TZ
class Person {
private String name;
private int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
public void introduce() {
System.out.println("Hi, I'm " + name + " and I'm " + age + " years
old");
}
}
6. Create a Car class with attributes brand, model, and year. Create getters and
setters for each attribute. Create constructors to instantiate an object of the
class.
Codes:
1.) class Car {
private String brand;
private String model;
private int year;
Said Jamal Ramadhan DITA/7/23/001/TZ
Buda Amour Buda DITA/7/23/004/TZ
Ibrahim Ally Omar DITA/7/23/051/TZ
Car(String brand, String model, int year) {
this.brand = brand;
this.model = model;
this.year = year;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
}
Said Jamal Ramadhan DITA/7/23/001/TZ
Buda Amour Buda DITA/7/23/004/TZ
Ibrahim Ally Omar DITA/7/23/051/TZ
7. Implement a BankAccount class with deposit() and withdraw() methods and a
balance attribute. Demonstrate encapsulation by making balance a private
attribute in BankAccount.
Codes:
class BankAccount {
private double balance;
BankAccount(double balance) {
this.balance = balance;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
}
}
public double getBalance() {
return balance;
}
}
Said Jamal Ramadhan DITA/7/23/001/TZ
Buda Amour Buda DITA/7/23/004/TZ
Ibrahim Ally Omar DITA/7/23/051/TZ
8. Create a Circle class that calculates area and circumference using a radius
attribute.
Codes:
class Circle {
private double radius;
Circle(double radius) {
this.radius = radius;
public double calculateArea() {
return Math.PI * radius * radius;
}
public double calculateCircumference() {
return 2 * Math.PI * radius;
}
}
9. Write a Student class with a constructor to initialize name and grades.
Implement a method to calculate the average grade. Create getters and setters
for each attributes.
Codes:
1.) class Student {
private String name;
Said Jamal Ramadhan DITA/7/23/001/TZ
Buda Amour Buda DITA/7/23/004/TZ
Ibrahim Ally Omar DITA/7/23/051/TZ
private int[] grades;
Student(String name, int[] grades) {
this.name = name;
this.grades = grades;
}
public double calculateAverageGrade() {
int sum = 0;
for (int grade : grades) {
sum += grade;
}
return (double) sum / grades.length;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int[] getGrades() {
return grades;
}
public void setGrades(int[] grades) {
this.grades = grades;
}
}
Said Jamal Ramadhan DITA/7/23/001/TZ
Buda Amour Buda DITA/7/23/004/TZ
Ibrahim Ally Omar DITA/7/23/051/TZ
10.Create a Laptop class with attributes brand, processor, and RAM. Implement a
method display_specs().
Codes:
class Laptop {
private String brand;
private String processor;
private int RAM;
Laptop(String brand, String processor, int RAM) {
this.brand = brand;
this.processor = processor;
this.RAM = RAM;
}
public void display_specs() {
System.out.println("Brand: " + brand);
System.out.println("Processor: " + processor);
System.out.println("RAM: " + RAM + "GB");
}
}
Said Jamal Ramadhan DITA/7/23/001/TZ
Buda Amour Buda DITA/7/23/004/TZ
Ibrahim Ally Omar DITA/7/23/051/TZ
11.Write a Movie class with attributes title, genre, and rating. Add a method
is_suitable_for_kids() to check if the rating is below PG-13.
Codes:
class Movie {
private String title;
private String genre;
private String rating;
Movie(String title, String genre, String rating) {
this.title = title;
this.genre = genre;
this.rating = rating;
}
public boolean is_suitable_for_kids() {
return !rating.equals("PG-13") && !rating.equals("R");
}
}
12.Consider the following class diagram. Using inheritance and polymorphism
concept, implement these classes in java.
Said Jamal Ramadhan DITA/7/23/001/TZ
Buda Amour Buda DITA/7/23/004/TZ
Ibrahim Ally Omar DITA/7/23/051/TZ
Codes:
class Programmer {
String company;
Programmer(String company) {
this.company = company;
}
public void code() {
System.out.println("Coding at " + company);
}
}
class Dancer {
String group;
Said Jamal Ramadhan DITA/7/23/001/TZ
Buda Amour Buda DITA/7/23/004/TZ
Ibrahim Ally Omar DITA/7/23/051/TZ
Dancer(String group) {
this.group = group;
}
public void dance() {
System.out.println("Dancing with " + group);
}
}
class Singer {
String band;
Singer(String band) {
this.band = band;
}
public void sing() {
System.out.println("Singing with " + band);
}
}
13.Consider the following class diagram. Using inheritance and polymorphism
concept, implement these classes in java.
Said Jamal Ramadhan DITA/7/23/001/TZ
Buda Amour Buda DITA/7/23/004/TZ
Ibrahim Ally Omar DITA/7/23/051/TZ
Codes:
class Package {
double length;
double width;
Package(double length, double width) {
this.length = length;
this.width = width;
}
public double calculateArea() {
return length * width;
}
public double calculatePerimeter() {
return 2 * (length + width);
}
Said Jamal Ramadhan DITA/7/23/001/TZ
Buda Amour Buda DITA/7/23/004/TZ
Ibrahim Ally Omar DITA/7/23/051/TZ
Class Rectangle extends Package {
Rectangle (double length, double width) {
Super (length, width);
}
}
Class Circle {
Double radius;
Circle (double radius) {
This. Radius = radius;
}
Public double calculate Area () {
Return Math. I * radius * radius;
}
Public double calculate Perimeter () {
Return 2 * Math. I * radius;
}
}