My class recently switched from python to java. I feel like they just threw us in and said " oh they know python so they must know java" My first assignment is quite simple but having no prior knowledge of java i'm lost.

basicly "PizzaRun is a simple program that calculates the number of pizzas to buy based on the sum of the slices entered on the command line arguments after the first argument, the price per pizza." link here

This is what I have so far.

/**
 * @author  *
 */
public class PizzaRun {

public static void main(String[] args) {
    ArrayList<Ineger> pizza = calcWholePie( nSlices );		
}
public static int slicePerPie = 8;
public static void calcWholePie(int nSlices) {
    int pies = 0;
    while(nSlices > 0) {
        if(nSlices > slicePerPie) {
            nSlices = 8 - nSlices;
            pies = pies + 1;
        } else { 
            pies = pies + 1;
        }
    }
System.out.println("buy" + pies + "pizzas");
}

}

Recommended Answers

All 2 Replies

Okay, let's look at the logic of the thing. As you have it now, your calculate method has one serious flaw in it. Walk through the logic manually - what happens if I want to buy 15 slices?

If you correct that error (it's a simple fix) then you have a more or less correct implementation of a modulo-by-loop.

Now you're confused on the method call - you're trying to assign the return value of a void function, which doesn't have a return value, into an arraylist of Integers - why would it be an arraylist? If you return a value from this, it'll be a single int. So either return an int (the number of pies) and print that from the main method or just print the output from the calculate method. Preferably the former, but the latter will be easier. If you do the latter, don't try to assign that method return into anything, just call the function.

I don't see where you're getting your original nSlices from. Maybe you need a hint: args[] is the name of the argument vector, the collection of arguments to the original command that launched the program. In your definition of the problem, that's going to be the price per pizza and the number of slices. So if you figure out how read an array, you'll have the data you need. Trouble is, it'll be in the form of Strings, since the command line input comes in as Strings. Look at the Integer class and figure out how to turn a String into an int.

That should give you enough to work on. Back to work for you!

Really good post, couldn't have asked for more!!..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.