I'm in an AP Computer Science (high school level) and I'm still trying to grasp some things we're learning in class. We recently got an assignment to modify a 'Pizza' resource class. His directions from the worksheet were:
Part 1: Modify the old Pizza class written earlier. Add an equals() method which overrides the one in the Object class. Two Pizza objects are equal if the toppings, sizes and costs are the same. Use the PizzaMatch class to test the new method.
Part 2: Add a compareTo() method to implement the interface Comparable. This compareTo() should help you find the cheapest pizza in pizza.txt.**
And the output he wanted is
**Part 1 Output
Input a Pizza topping, size, and cost:
sloppyJoe 15 15.30
That pizza is # 20 in the file.
ÏÏÏ
Input a Pizza topping, size, and cost:
cheese 12 12.99
That pizza is not in the file.
Part 2 Output
The cheapest pizza: The 9 inch olive pizza will cost $7.99**
Here is Pizza (the resource class)
import java.util.*;
import java.io.*;
class Pizza
{
private int size;
private double cost;
private String topping;
public Pizza(int pizzaSize, double pizzaCost,String pizzaTopping)
{
size = pizzaSize;
cost = pizzaCost;
topping = pizzaTopping;
}
public void setSize(int input)
{
size = input;
}
public void setCost(double input)
{
cost = input;
}
public void setTopping(String input)
{
topping = input;
}
public int getSize()
{
return size;
}
public double getCost()
{
return cost;
}
public String getTopping()
{
return topping;
}
public String toString()
{
return (size + " inch " + topping + " pizza will cost $" + cost);
}
}
And here is PizzaMatch
import java.util.*;
import java.io.*;
public class PizzaMatch {
public static void main (String [] args)throws Exception
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Input a Pizza topping, size, and cost: ");
String top = keyboard.next();
int size = keyboard.nextInt();
double cost = keyboard.nextDouble();
Pizza input = new Pizza(size, cost, top);
int counter =1;
boolean found = false;
while(inFile.hasNext())
{
String t = inFile.next();
int s = inFile.nextInt();
double c = inFile.nextDouble();
Pizza temp = new Pizza(s,c,t);
//System.out.println("Pizza #"+counter+"\t" + temp);
if(temp.equals(input))
{
System.out.println ( "That pizza is # " + counter
+ " in the file.");
found = true;
}
counter++;
}
if(!found)
System.out.println("That pizza was not in the file.");
}
}
Basically, I'm not sure where to start. I'm still a bit unsure of interfaces, as well. I realize that we have to make a .equals() method, but how? I started out writing
public Boolean equals(Pizza p)
{
if(p==/*this is where I don't know what to write*/)
return true;
}
Any help would be greatly appreciated, for both Part 1 and Part 2 of the assignment! Thanks so much :)