9.
4 CodeHS
public class Square extends Rectangle {
public Square(double sideLength){
super(sideLength, sideLength);
}
public void setSideLength(double sideLength){
setWidth(sideLength);
setHeight(sideLength);
}
public double area(){
return getWidth() * getWidth();
}
public double perimeter(){
return 4 * getWidth();
}
@Override
public String toString(){
return getWidth() + " x " + getHeight() + " Square";
}
}
public class SquareTester
{
public static void main(String[] args)
{
Square sq = new Square(5.0);
System.out.println(sq);
System.out.println("Area: " + sq.area());
System.out.println("Perimeter: " + sq.perimeter());
sq.setSideLength(7.0);
System.out.println("\nAfter resizing:");
System.out.println(sq);
System.out.println("Area: " + sq.area());
System.out.println("Perimeter: " + sq.perimeter());
}
}
public class Rectangle {
private double width;
private double height;
public Rectangle(double width, double height){
this.width = width;
this.height = height;
}
public double getWidth(){
return width;
}
public void setWidth(double width){
this.width = width;
}
public double getHeight(){
return height;
}
public void setHeight(double height){
this.height = height;
}
public String toString(){
return width + " x " + height + " Rectangle";
}
}
public class BankTester {
public static void main(String[] args) {
Account regular = new Account(1001, 100.00);
regular.deposit(50.00);
regular.withdraw(20.00);
System.out.println(regular);
StudentAccount student = new StudentAccount(2002, 100.00);
student.deposit(50.00);
student.withdraw(20.00);
System.out.println(student);
}
}
public class Account {
private int accountNumber;
private double balance;
public Account(int accountNumber, double openingBal){
this.accountNumber = accountNumber;
this.balance = openingBal;
}
public double getBalance(){
return balance;
}
public void deposit(double amount){
balance += amount;
}
public void withdraw(double amount){
balance -= amount;
}
public String toString(){
return String.format("Regular account current balance $%.1f", balance);
}
}
public class StudentAccount extends Account {
public StudentAccount(int accountNumber, double openingBal){
super(accountNumber, openingBal);
}
@Override
public void deposit(double amount){
double bonus = amount * 0.10;
super.deposit(amount + bonus);
}
@Override
public void withdraw(double amount){
super.withdraw(amount + 1.50);
}
@Override
public String toString(){
return String.format("Student account current balance $%.1f", getBalance());
}
}
public class EmployeeTester {
public static void main(String[] args) {
Employee e1 = new Employee("Mr. Karel", 75000.0);
System.out.println(e1);
HourlyEmployee h1 = new HourlyEmployee("Mike", 18.0, 40.0);
System.out.println(h1);
h1.setHourlySalary(20.0);
System.out.println("Updated hourly: " + h1.getHourlySalary());
System.out.println(h1);
}
}
public class HourlyEmployee extends Employee {
private double hoursPerWeek;
public HourlyEmployee(String name, double hourlySalary, double hoursPerWeek){
super(name, hourlySalary * hoursPerWeek * 52);
this.hoursPerWeek = hoursPerWeek;
}
public double getHourlySalary(){
return getAnnualSalary() / (hoursPerWeek * 52);
}
public void setHourlySalary(double hourlySalary){
double annual = hourlySalary * hoursPerWeek * 52;
setAnnualSalary(annual);
}
/**
* Example output:
* Mike makes $18.0 per hour
*/
public String toString(){
return getName() + " makes $" + getHourlySalary() + " per hour";
}
}
public class Employee {
private String name;
private double salary;
public Employee(String name, double annualSalary){
this.name = name;
this.salary = annualSalary;
}
public String getName(){
return name;
}
public double getAnnualSalary(){
return salary;
}
public void setAnnualSalary(double annualSalary){
this.salary = annualSalary;
}
/**
* Example output:
* Mr. Karel makes $75000.0 per year
*/
public String toString(){
return name + " makes $" + salary + " per year";
}
}
import java.util.Scanner;
public class StudentTester {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the student name:");
String name = scanner.nextLine();
System.out.println("Please enter the Math Score:");
int mathScore = scanner.nextInt();
System.out.println("Please enter the ELA Score:");
int elaScore = scanner.nextInt();
System.out.println("Please enter the Service Hours:");
int serviceHours = scanner.nextInt();
HSStudent student = new HSStudent(name, mathScore, elaScore, serviceHours);
System.out.println("Pass Math? " + student.passMath());
System.out.println("Pass ELA? " + student.passEla());
System.out.println("Completed Service Hours? " + student.completeService());
System.out.println(student);
scanner.close();
}
}
public class StudentTest {
private int mathScore;
private int elaScore;
private String name;
public StudentTest(String name, int mathScore, int elaScore){
this.name = name;
this.mathScore = mathScore;
this.elaScore = elaScore;
}
public int getMathScore() {
return mathScore;
}
public void setMathScore(int mathScore) {
this.mathScore = mathScore;
}
public int getElaScore() {
return elaScore;
}
public void setElaScore(int elaScore) {
this.elaScore = elaScore;
}
public String getName() {
return name;
}
}
public class HSStudent extends StudentTest {
private int serviceHours;
public HSStudent(String name, int mathScore, int elaScore, int serviceHours){
super(name, mathScore, elaScore);
this.serviceHours = serviceHours;
}
public int getServiceHours(){
return serviceHours;
}
public void setServiceHours(int serviceHours){
this.serviceHours = serviceHours;
}
public boolean passMath(){
return getMathScore() >= 525;
}
public boolean passEla(){
return getElaScore() >= 560;
}
public boolean completeService(){
return serviceHours >= 75;
}
public boolean gradQualify(){
return passMath() && passEla() && completeService();
}
public String toString(){
if (gradQualify()){
return getName() + " has qualified for graduation.";
} else {
return getName() + " has not yet qualified for graduation.";
}
}
}
9.5 CodeHS
import java.util.ArrayList;
public class PieTester {
public static void main(String[] args) {
ArrayList<Pie> pies = new ArrayList<Pie>();
pies.add(new ApplePie(6));
pies.add(new PecanPie(8, "molasses"));
pies.add(new Pie("Rhubarb", 12));
for (Pie pie : pies) {
System.out.println(pie.getSlices() + " slice " + pie.getType() + " pie");
}
}
}
public class Pie {
private String type;
private int slices;
public Pie (String type, int slices) {
this.type = type;
this.slices = slices;
}
public int getSlices (){
return slices;
}
public void eatSlice(){
slices --;
}
public String getType(){
return type;
}
}
public class PecanPie extends Pie {
private String syrupType;
public PecanPie (int slices, String syrupType)
{
super("Pecan", slices);
this.syrupType = syrupType;
}
public String getSyrupType()
{
return syrupType;
}
public class ApplePie extends Pie {
public ApplePie (int slices)
{
super("Apple", slices);
}
public boolean hasSlice()
{
return super.getSlices() > 0;
}
@Override
public void eatSlice()
{
if (this.hasSlice())
{
super.eatSlice();
}
}
}
import java.util.Scanner;
public class PersonTester {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Please enter the Person's name: ");
String personName = input.nextLine();
System.out.print("Please enter the Person's birthday: ");
String personBirthday = input.nextLine();
System.out.print("Please enter the Student's name: ");
String studentName = input.nextLine();
System.out.print("Please enter the Student's birthday: ");
String studentBirthday = input.nextLine();
System.out.print("Please enter the Student's grade: ");
int grade = input.nextInt();
Person p = new Person(personName, personBirthday);
Student s = new Student(studentName, studentBirthday, grade);
System.out.println("Same: " + p.equals(s));
}
}
public class Person {
private String name;
private String birthday;
public Person (String name, String birthday)
{
this.name = name;
this.birthday = birthday;
}
public String getBirthday(){
return birthday;
}
public String getName(){
return name;
}
//Create a equals method here
public boolean equals(Person other) {
return this.name.equals(other.name) && this.birthday.equals(other.birthday);
}
}
public class Student extends Person {
private int grade;
public Student(String name, String birthday, int grade){
super(name, birthday);
this.grade = grade;
}
public int getGrade(){
return grade;
}
}
import java.util.*;
public class CompanyTester {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ArrayList<Company> companies = new ArrayList<>();
while (true) {
System.out.print("Please enter a company name, enter 'exit' to quit: ");
String name = input.nextLine();
if (name.equalsIgnoreCase("exit")) {
break;
}
System.out.print("Is this an online company, 'yes' or 'no': ");
String online = input.nextLine();
if (online.equalsIgnoreCase("yes")) {
System.out.print("Please enter the website address: ");
String website = input.nextLine();
companies.add(new OnlineCompany(name, website));
} else {
System.out.print("Please enter the street address: ");
String street = input.nextLine();
System.out.print("Please enter the city: ");
String city = input.nextLine();
System.out.print("Please enter the state: ");
String state = input.nextLine();
companies.add(new Company(name, street, city, state));
}
}
System.out.println();
for (Company c : companies) {
System.out.println(c + "\n");
}
}
}
public class OnlineCompany extends Company {
private String webAddress;
public OnlineCompany(String name, String webAddress){
super(name);
this.webAddress = webAddress;
}
@Override
public String address(){
return webAddress;
}
@Override
public String toString(){
return super.getName() + "\nWebsite: " + address();
}
}
public class Company {
private String name;
private String streetAddress;
private String city;
private String state;
public Company(String name){
this.name = name;
this.streetAddress = null;
this.city = null;
this.state = null;
}
public Company(String name, String streetAddress, String city, String state){
this.name = name;
this.streetAddress = streetAddress;
this.city = city;
this.state = state;
}
public String getName(){
return name;
}
public String address(){
return streetAddress + "\n" + city + ", " + state;
}
public String toString(){
return name + "\n" + address();
}
}
import java.util.*;
public class AssignmentRunner {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ArrayList<Assignment> assignments = new ArrayList<>();
while (true) {
System.out.print("Enter the assignment's name (exit to quit): ");
String name = input.nextLine();
if (name.equalsIgnoreCase("exit")) {
break;
}
System.out.print("Enter the due date: ");
String dueDate = input.nextLine();
System.out.print("How many points is the assignment worth? ");
double availablePoints = input.nextDouble();
System.out.print("How many points were earned? ");
double earnedPoints = input.nextDouble();
input.nextLine();
System.out.print("Is this a (T)est or a (P)roject? ");
String type = input.nextLine();
if (type.equalsIgnoreCase("T")) {
System.out.print("What type of test is it? ");
String testType = input.nextLine();
assignments.add(new Test(name, dueDate, availablePoints, earnedPoints, testType));
} else if (type.equalsIgnoreCase("P")) {
System.out.println("Does this project require (true/false) ... ");
System.out.print("Groups? ");
boolean groups = input.nextBoolean();
System.out.print("A presentation? ");
boolean presentation = input.nextBoolean();
input.nextLine();
assignments.add(new Project(name, dueDate, availablePoints, earnedPoints, groups,
presentation));
}
}
printSummary(assignments);
}
public static void printSummary(ArrayList<Assignment> assignments) {
for (Assignment a : assignments) {
double percentage = (a.getEarnedPoints() / a.getAvailablePoints()) * 100.0;
System.out.println(a.getName() + " - " + percentage);
}
}
}
public class Assignment {
private String name;
private String dueDate;
private double availablePoints;
private double earnedPoints;
public Assignment(String name, String dueDate, double availablePoints, double
earnedPoints) {
this.name = name;
this.dueDate = dueDate;
this.availablePoints = availablePoints;
this.earnedPoints = earnedPoints;
}
public String getName() {
return name;
}
public String getDueDate() {
return dueDate;
}
public double getAvailablePoints() {
return availablePoints;
}
public double getEarnedPoints() {
return earnedPoints;
}
public void setName(String name) {
this.name = name;
}
public void setDueDate(String dueDate) {
this.dueDate = dueDate;
}
public void setAvailablePoints(double availablePoints) {
this.availablePoints = availablePoints;
}
public void setEarnedPoints(double earnedPoints) {
this.earnedPoints = earnedPoints;
}
}
public class Project extends Assignment {
private boolean groups;
private boolean presentation;
public Project(String name, String dueDate, double availablePoints, double earnedPoints,
boolean hasGroups, boolean hasPresentation) {
super(name, dueDate, availablePoints, earnedPoints);
this.groups = hasGroups;
this.presentation = hasPresentation;
}
public boolean hasGroups() {
return groups;
}
public boolean hasPresentation() {
return presentation;
}
public void setGroups(boolean groups) {
this.groups = groups;
}
public void setPresentation(boolean presentation) {
this.presentation = presentation;
}
}
public class Test extends Assignment {
private String testType;
public Test(String name, String dueDate, double availablePoints, double earnedPoints, String
testType) {
super(name, dueDate, availablePoints, earnedPoints);
this.testType = testType;
}
public String getTestType() {
return testType;
}
public void setTestType(String testType) {
this.testType = testType;
}
}