0% found this document useful (0 votes)
16 views7 pages

Lab 5

The document contains multiple Java classes demonstrating object-oriented programming concepts. It includes implementations for a Car, Book, Employee, User, and Rectangle, each with methods for functionality and testing. Each class is designed to encapsulate properties and behaviors relevant to its respective entity.

Uploaded by

k246167
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views7 pages

Lab 5

The document contains multiple Java classes demonstrating object-oriented programming concepts. It includes implementations for a Car, Book, Employee, User, and Rectangle, each with methods for functionality and testing. Each class is designed to encapsulate properties and behaviors relevant to its respective entity.

Uploaded by

k246167
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Task 1:

package com.mycompany.ooplab1;

/**
*
* @author k246167
*/
class Car {
private int yearModel;
private String make;
private int speed;

public Car(int yearModel, String make) {


this.yearModel = yearModel;
this.make = make;

this.speed = 0;
}
public int getYearModel() {
return yearModel;
}

public String getMake() {


return make;
}

public int getSpeed() {


return speed;
}
public void accelerate() {
speed += 5;

}
public void brake() {
if (speed >= 5) speed -= 5;
}
public sta c void main(String[] args) {
Car myCar = new Car(2022, "Toyota");
for (int i = 0; i < 5; i++) {
myCar.accelerate();
System.out.println("Speed a er accelera on: " + myCar.getSpeed());
}
for (int i = 0; i < 5; i++) {
myCar.brake();
System.out.println("Speed a er braking: " + myCar.getSpeed());
}
}
}

Task 2:
package com.mycompany.ooplab1;

/**
*
* @author k246167
*/

class Book {
private String tle;
private String author;
private double price;

public Book(String tle, String author, double price) {


this. tle = tle;
this.author = author;
this.price = price;
}

public void setTitle(String tle) { this. tle = tle; }


public String getTitle() { return tle; }

public void setAuthor(String author) { this.author = author; }


public String getAuthor() { return author; }

public void setPrice(double price) { this.price = price; }


public double getPrice() { return price; }
public String toString() {
return "Title: " + tle + ", Author: " + author + ", Price: $" + price;
}
}
public class BookDemo {
public sta c void main(String[] args) {
Book book = new Book("Great Expecta ons", "Charles Dickens", 79.75);
System.out.println(book.toString());
}
}
Task 3:
package com.mycompany.ooplab1;

/**
*
* @author k246167
*/

class Employee {
private String firstName;
private String lastName;
private double monthlySalary;

public Employee(String firstName, String lastName, double monthlySalary) {


this.firstName = firstName;

this.lastName = lastName;
this.monthlySalary = (monthlySalary > 0) ? monthlySalary : 0.0;
}
public String getFirstName() { return firstName; }
public String getLastName() { return lastName; }
public double getMonthlySalary() { return monthlySalary; }

public void setMonthlySalary(double salary) {


if (salary > 0) this.monthlySalary = salary;
}

public double getYearlySalary() {


return monthlySalary * 12;
}

public void giveRaise(double percent) {


monthlySalary += monthlySalary * (percent / 100);
}
}
public class EmployeeTest {
public sta c void main(String[] args) {
Employee emp1 = new Employee("Alice", "Johnson", 3000);
Employee emp2 = new Employee("Bob", "Smith", 4000);

System.out.println(emp1.getFirstName() + " Yearly Salary: $" + emp1.getYearlySalary());


System.out.println(emp2.getFirstName() + " Yearly Salary: $" + emp2.getYearlySalary());
emp1.giveRaise(10);
emp2.giveRaise(10);

System.out.println("A er 10% raise:");


System.out.println(emp1.getFirstName() + " Yearly Salary: $" + emp1.getYearlySalary());
System.out.println(emp2.getFirstName() + " Yearly Salary: $" + emp2.getYearlySalary());
}
}

Task 4:
package com.mycompany.ooplab1;

/**
*
* @author k246167
*/
class User {

public int age;


public String name;
public User(String name, int age) {
this.name = name;
this.age = age;
}
}
public class UserTest {
public sta c void main(String[] args) {
User user = new User("Teo", 24);
System.out.println("My name is " + user.name + " and I'm " + user.age + " years old.");
}
}

Task 5:
package com.mycompany.ooplab1;

/**
*
* @author k246167
*/

class Rectangle {
private double length;
private double breadth;
public Rectangle(double length, double breadth) {
this.length = length;
this.breadth = breadth;
}

public double getArea() {


return length * breadth;
}
}

public class RectangleTest {


public sta c void main(String[] args) {
Rectangle rect1 = new Rectangle(4, 5);
Rectangle rect2 = new Rectangle(5, 8);

System.out.println("Area of Rectangle 1: " + rect1.getArea());


System.out.println("Area of Rectangle 2: " + rect2.getArea());
}
}

You might also like