Link Search Menu Expand Document

Question 1

For this question, you will write a program that prints all real zeroes to any quadratic equation. A quadratic equation is one in the form of ax^2 + bx + c. Your program will read the coefficients a, b, c and use the quadratic formula to solve the equation. You can assume that the coefficients will be written by the user as valid doubles. You can also assume that all of a, b, c are non-zero.

Part I:

QuadraticEquation.java Autograded

Create a class QuadraticEquation whose constructor accepts as its three arguments the three coefficients a, b, and c. Write a method evaluate() in the class that returns the value of the quadratic equation given a specific input x. You should also write getters for a, b, and c called getA, getB, and getC respectively.

As a brief refresher on getters: a getter is a method that returns the value of one of the object’s fields. It takes no inputs, and has a return type that matches the type of the field it’s returning. For example here, getA() might look like the following:

public double getA() {
    return a;
}

Once implemented correctly, you can test your work like this in main:

// 3x^2 + 4x - 2
QuadraticEquation qe = new QuadraticEquation(3.0, 4.0, -2.0);
System.out.println(qe.getA());
>>> 3.0

// 3*4 + 4*2 - 2
double output = qe.evaluate(2.0);
System.out.println(output);
>>> 18.0

Part II

QuadraticEquationZeroes.java Autograded

Create a class QuadraticEquationZeroes whose constructor receives an object of type QuadraticEquation. Write methods getSmallerSolution() and getLargerSolution() that get the solutions to ax^2 + bx + c = 0, using the quadratic formula, or 0 if no real-valued solution exists.

The quadratic equation helps find the values of x where ax^2 + bx + c = 0. The equation is as follows:

-b ± √(b^2 - 4ac)
------------------
       2a

Extra details:

  • The part under the square root (b^2 - 4ac) is the discriminant, and when the discriminant is negative, there are no real-valued solutions.
  • The ± indicates “plus or minus”, meaning that one solution is found by subtracting that value and the other is found by adding it.
  • getSmallerSolution() should return the smaller value of x that solves the equation. If there is only one solution, then getSmallerSolution() and getLargerSolution() should return the same value.

Part III

QuadraticEquationRunner.java Autograded

Finally, implement a QuadraticEquationRunner class that runs your program. This class should contain a main method that reads user input (i.e. from a Scanner) for a, b, and c, constructs the appropriate objects, and then prints out a message in the form "<a>x^2 + <b>x + <c> has solutions <smaller_solution> and <bigger_solution>.". The terms in brackets (e.g. <a> and <smaller_solution>) should be replaced with the appropriate values for that particular run of the program.

For example, when a = 3.0, b = 4.0, c = 1.0, the output should be: 3.0x^2 + 4.0x + 1.0 has solutions -1 and -0.333333.

Note: this portion is automatically graded, so please pay close attention to the expected output formatting. We will take a manual pass at grading this as well, but if you want the automatic points, you’ll need to match the formatting closely.

Question 2 Manually Graded

Write a program that asks the user for a specific day and time to print out whether or not a user is allowed to park based on the provided input.

Use a class ParkingSigns to write two methods: parkingSign1() and parkingSign2(). These methods will model the parking signs found below. Each method should use a Scanner to ask for:

  • the current day (as Mon, Tue, Wed, Thu, Fri, Sat, or Sun).
  • the current hour (as an int between 0 and 23)
  • the current minute (as an int between 0 and 59)

If any of the provided inputs are not valid (either the wrong type, or not in the acceptable ranges), then the program should exit with a relevant message.

The methods should each present a message telling the user if they can park at the specified time or not. The messages should also include information about:

  • whether the parking is free or metered
  • if only commercial vehicles are permitted to park at this time
  • if parking is permitted, how long can the user park there for?
    • If there is no current parking restriction, and no parking restriction later that day, assume that they can only park until midnight.
    • If there is no current parking restriction, but there is a parking restriction later that day, print how long they can park until that restriction applies.
    • If there is a parking restriction, e.g. two hour parking, take this into account.
      • if the two hour free parking ends at 5pm and the user parks at 1pm, then print that they can only park for two hours.
      • if the two hour free parking ends at 5pm and there is no later restriction that day, then a user parking at 4pm would be able to park their car until midnight!

These requirements are very intricate! Try to map out with pencil and paper what would happen if you parked at different times of day to make sure you understand all the possible cases. Your program will be tested very thoroughly.


Example: parkingSign0()

parking sign zero

public static void parkingSign0() {

    // Set up the Scanner for taking user input
    Scanner scan = new Scanner(System.in);

    // Prompt for day of week and reject if invalid
    System.out.println("Enter today's day (Mon, Tue, Wed, Thu, Fri, Sat, Sun)");
    String day = scan.next();
    if (!day.equals("Mon") && !day.equals("Tue") && !day.equals("Wed") && !day.equals("Thu") && !day.equals("Fri")
            && !day.equals("Sat") && !day.equals("Sun")) {
        System.out.println("Invalid day! Bye Bye!!!");
        return;
    }

    // Prompt for hour of day (24 hour time) and reject if invalid
    System.out.println("Enter today's time (hours only, 00 to 23)");
    int hour = scan.nextInt();
    if (hour < 0 || hour > 23) {
        System.out.println("Invalid hour! Bye Bye!!!");
        return;
    }

    // Prompt for minute of day (24 hour time) and reject if invalid
    System.out.println("Enter today's time (minutes only, 00 to 59)");
    int mins = scan.nextInt();
    if (mins < 0 || mins > 59) {
        System.out.println("Invalid minutes! Bye Bye!!!");
        return;
    }

    // On Fridays, there is no parking between 10am-12pm
    if (day.equals("Fri") && hour >= 10 && hour <= 11) {
        System.out.println("No parking");
    } else {
        int h1 = 9 - hour;
        int h2 = 23 - hour;
        int min = 59 - mins;
        // If it's Friday morning, you can only park until 10am.
        if (day.equals("Fri") && hour < 10) {
            System.out.println("Free parking up to: " + h1 + "hour(s) " + min + "mins");
        } else { // Otherwise, park through the end of the day (midnight).
            System.out.println("Free parking up to: " + h2 + "hour(s) " + min + "mins");
        }
    }
}

parkingSign1()

parking sign one

parkingSign2()

parking sign two

Submission

Submit QuadraticEquation.java, QuadraticEquationZeroes.java, QuadraticEquationRunner.java from Q1 and ParkingSign.java from Q2 to Gradescope.

Ensure that your files do not have a package header! Specifically, the first lines of your file should not read package xyz or similar.

Specifically, please find these files locally on your file system. By default, they will be found in a folder called eclipse_workspace, often in your user’s directory. You should select each of the files and upload them as files (not as a directory, and preferably not as a .zip either). If you need help, please reach out on Piazza or in Office Hours.