Circle.
java: This class defines a circle with a center and radius and includes
methods to compute its area and circumference.
public class Circle {
private double radius;
private MyPoint center;
// Default constructor
public Circle() {
this.radius = 1.0;
this.center = new MyPoint(0, 0);
}
// Parameterized constructor
public Circle(double radius, MyPoint center) {
this.radius = radius;
this.center = center;
}
// Getters and setters
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
if (radius > 0) {
this.radius = radius;
} else {
System.out.println("Invalid radius");
}
}
public MyPoint getCenter() {
return center;
}
public void setCenter(MyPoint center) {
this.center = center;
}
// Method to calculate the area of the circle
public double getArea() {
return Math.PI * Math.pow(radius, 2);
}
// Method to calculate the circumference of the circle
public double getCircumference() {
return 2 * Math.PI * radius;
}
public static void main(String[] args) {
MyPoint center = new MyPoint(5, 5);
Circle circle = new Circle(3, center);
System.out.println("Area: " + circle.getArea());
System.out.println("Circumference: " + circle.getCircumference());
}
}
Triangle.java: This class models a triangle with three sides and includes
methods to calculate the perimeter and area using Heron's formula.
public class Triangle {
private double sideA;
private double sideB;
private double sideC;
// Default constructor
public Triangle() {
this.sideA = 1;
this.sideB = 1;
this.sideC = 1;
}
// Parameterized constructor
public Triangle(double sideA, double sideB, double sideC) {
this.sideA = sideA;
this.sideB = sideB;
this.sideC = sideC;
}
// Setters and getters
public double getSideA() {
return sideA;
}
public void setSideA(double sideA) {
this.sideA = sideA;
}
public double getSideB() {
return sideB;
}
public void setSideB(double sideB) {
this.sideB = sideB;
}
public double getSideC() {
return sideC;
}
public void setSideC(double sideC) {
this.sideC = sideC;
}
// Method to calculate perimeter
public double getPerimeter() {
return sideA + sideB + sideC;
}
// Method to calculate area using Heron's formula
public double getArea() {
double s = getPerimeter() / 2;
return Math.sqrt(s * (s - sideA) * (s - sideB) * (s - sideC));
}
public static void main(String[] args) {
Triangle triangle = new Triangle(3, 4, 5);
System.out.println("Perimeter: " + triangle.getPerimeter());
System.out.println("Area: " + triangle.getArea());
}
}
Square.java: A class that models a square with a side length, providing methods
to calculate the area and perimeter.
public class Square {
private double side;
// Default constructor
public Square() {
this.side = 1.0;
}
// Parameterized constructor
public Square(double side) {
this.side = side;
}
// Setter for side length
public void setSide(double side) {
if (side > 0) {
this.side = side;
} else {
System.out.println("Invalid side length");
}
}
// Getter for side length
public double getSide() {
return this.side;
}
// Method to calculate area
public double getArea() {
return side * side;
}
// Method to calculate perimeter
public double getPerimeter() {
return 4 * side;
}
public static void main(String[] args) {
Square square = new Square(5);
System.out.println("Area: " + square.getArea());
System.out.println("Perimeter: " + square.getPerimeter());
}
}
Book.java: A class that models a book with title, author, and price attributes,
including methods to display information and set a discount on the price.
public class Book {
private String title;
private String author;
private double price;
// Default constructor
public Book() {
this.title = "Unknown";
this.author = "Unknown";
this.price = 0.0;
}
// Parameterized constructor
public Book(String title, String author, double price) {
this.title = title;
this.author = author;
this.price = price;
}
// Setter for price
public void setPrice(double price) {
if (price > 0) {
this.price = price;
} else {
System.out.println("Invalid price");
}
}
// Getter for price
public double getPrice() {
return this.price;
}
// Method to apply discount
public void applyDiscount(double discount) {
if (discount > 0 && discount <= 100) {
this.price = this.price - (this.price * (discount / 100));
} else {
System.out.println("Invalid discount");
}
}
// Method to display book info
public void displayInfo() {
System.out.println("Title: " + this.title);
System.out.println("Author: " + this.author);
System.out.println("Price: " + this.price);
}
public static void main(String[] args) {
Book book = new Book("Java Programming", "John Doe", 50.0);
book.displayInfo();
book.applyDiscount(10);
System.out.println("Price after discount: " + book.getPrice());
}
}
Car.java: A class that models a car with attributes for make, model, and speed,
and includes methods to accelerate and brake.
public class Car {
private String make;
private String model;
private int speed;
// Default constructor
public Car() {
this.make = "Unknown";
this.model = "Unknown";
this.speed = 0;
}
// Parameterized constructor
public Car(String make, String model, int speed) {
this.make = make;
this.model = model;
this.speed = speed;
}
// Getter for speed
public int getSpeed() {
return this.speed;
}
// Method to accelerate
public void accelerate(int increment) {
this.speed += increment;
System.out.println("Accelerating by " + increment + ". New speed: " + this.speed);
}
// Method to brake
public void brake(int decrement) {
this.speed -= decrement;
if (this.speed < 0) {
this.speed = 0;
}
System.out.println("Braking by " + decrement + ". New speed: " + this.speed);
}
public static void main(String[] args) {
Car car = new Car("Toyota", "Corolla", 50);
System.out.println("Initial speed: " + car.getSpeed());
car.accelerate(20);
car.brake(30);
}
}
Student.java: A class that represents a student with attributes like name, age,
and grade, with methods to display details and calculate pass/fail based on the
grade.
public class Student {
private String name;
private int age;
private double grade;
// Default constructor
public Student() {
this.name = "Unknown";
this.age = 0;
this.grade = 0.0;
}
// Parameterized constructor
public Student(String name, int age, double grade) {
this.name = name;
this.age = age;
this.grade = grade;
}
// Method to display student details
public void displayDetails() {
System.out.println("Name: " + this.name);
System.out.println("Age: " + this.age);
System.out.println("Grade: " + this.grade);
}
// Method to check if student has passed
public void checkPass() {
if (this.grade >= 50) {
System.out.println(this.name + " has passed.");
} else {
System.out.println(this.name + " has failed.");
}
}
public static void main(String[] args) {
Student student = new Student("Alice", 20, 75.5);
student.displayDetails();
student.checkPass();
}
}
Person.java: A class that models a person with attributes like name, age, and
address, along with methods to change the address and display information.
public class Person {
private String name;
private int age;
private String address;
// Default constructor
public Person() {
this.name = "Unknown";
this.age = 0;
this.address = "Not Available";
}
// Parameterized constructor
public Person(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
// Setter for address
public void setAddress(String address) {
this.address = address;
}
// Getter for address
public String getAddress() {
return this.address;
}
// Method to display person details
public void displayDetails() {
System.out.println("Name: " + this.name);
System.out.println("Age: " + this.age);
System.out.println("Address: " + this.address);
}
public static void main(String[] args) {
Person person = new Person("John Doe", 25, "123 Main St");
person.displayDetails();
person.setAddress("456 Oak St");
System.out.println("Updated Address: " + person.getAddress());
}
}
BankAccount.java: A class that models a bank account with attributes for
account number, balance, and owner. It includes methods to deposit, withdraw,
and display account details.
public class BankAccount {
private String accountNumber;
private double balance;
private String owner;
// Default constructor
public BankAccount() {
this.accountNumber = "000000";
this.balance = 0.0;
this.owner = "Unknown";
}
// Parameterized constructor
public BankAccount(String accountNumber, double balance, String owner) {
this.accountNumber = accountNumber;
this.balance = balance;
this.owner = owner;
}
// Method to deposit money
public void deposit(double amount) {
if (amount > 0) {
this.balance += amount;
System.out.println("Deposited: $" + amount);
} else {
System.out.println("Invalid deposit amount");
}
}
// Method to withdraw money
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
this.balance -= amount;
System.out.println("Withdrew: $" + amount);
} else {
System.out.println("Invalid withdrawal amount");
}
}
// Method to display account details
public void displayAccountDetails() {
System.out.println("Account Number: " + this.accountNumber);
System.out.println("Owner: " + this.owner);
System.out.println("Balance: $" + this.balance);
}
public static void main(String[] args) {
BankAccount account = new BankAccount("123456", 500.0, "Alice");
account.displayAccountDetails();
account.deposit(200);
account.withdraw(100);
account.displayAccountDetails();
}
}
Employee.java: A class that models an employee with attributes for name,
position, and salary. It includes methods to give a raise and display employee
information.
public class Employee {
private String name;
private String position;
private double salary;
// Default constructor
public Employee() {
this.name = "Unknown";
this.position = "Unemployed";
this.salary = 0.0;
}
// Parameterized constructor
public Employee(String name, String position, double salary) {
this.name = name;
this.position = position;
this.salary = salary;
}
// Method to give a raise
public void giveRaise(double percentage) {
if (percentage > 0) {
this.salary += this.salary * (percentage / 100);
System.out.println("Salary increased by " + percentage + "%");
} else {
System.out.println("Invalid percentage");
}
}
// Method to display employee details
public void displayDetails() {
System.out.println("Name: " + this.name);
System.out.println("Position: " + this.position);
System.out.println("Salary: $" + this.salary);
}
public static void main(String[] args) {
Employee employee = new Employee("Bob", "Developer", 70000);
employee.displayDetails();
employee.giveRaise(10);
employee.displayDetails();
}
}
Library.java: A class that models a library with attributes for the library's name
and an array of books. It includes methods to add a book and list all books.
public class Library {
private String name;
private Book[] books;
private int count;
// Constructor
public Library(String name, int capacity) {
this.name = name;
this.books = new Book[capacity];
this.count = 0;
}
// Method to add a book to the library
public void addBook(Book book) {
if (count < books.length) {
books[count] = book;
count++;
System.out.println(book.getTitle() + " added to the library.");
} else {
System.out.println("Library is full, can't add more books.");
}
}
// Method to list all books in the library
public void listBooks() {
System.out.println("Books in " + this.name + " Library:");
for (int i = 0; i < count; i++) {
System.out.println(books[i].getTitle());
}
}
public static void main(String[] args) {
Library library = new Library("City Library", 5);
Book book1 = new Book("Java Basics", "Jane Doe", 29.99);
Book book2 = new Book("Advanced Java", "John Smith", 39.99);
library.addBook(book1);
library.addBook(book2);
library.listBooks();
}
}
Movie.java: A class that models a movie with attributes like title, director, and
rating. It includes methods to display movie details and set a new rating.
public class Movie {
private String title;
private String director;
private double rating;
// Constructor
public Movie(String title, String director, double rating) {
this.title = title;
this.director = director;
this.rating = rating;
}
// Method to display movie details
public void displayDetails() {
System.out.println("Title: " + this.title);
System.out.println("Director: " + this.director);
System.out.println("Rating: " + this.rating);
}
// Method to set a new rating
public void setRating(double rating) {
if (rating >= 0 && rating <= 10) {
this.rating = rating;
} else {
System.out.println("Invalid rating. It must be between 0 and 10.");
}
}
public static void main(String[] args) {
Movie movie = new Movie("Inception", "Christopher Nolan", 8.8);
movie.displayDetails();
movie.setRating(9.5);
movie.displayDetails();
}
}