program and compile error

J

judith

dear someone who can help
The first attachment is the program i wrote the second attachment is
the
required guidelines from the instructor.

also i don't know if i wrote the program correctly the instructions are
as
follows :You operate several hot dog stands . Define a class named
HotDogStand that has a member variable for the hot dog stand's Id nmber
and
a member variable for how many hot dogs the stand has sold that day.
Create
a constructor that allow a user of the class to initialize both values.
Also create a method named justSold that increments the number of hot
dogs
the stand has sold by one. The idea is that this method will be invoked
each
time the stand sells a hot dog so that you can track the total number
of hot
dogs sold by the stand. Add another method that returns the number of
hot
dogs sold.
Finally add a static variable that tracks the total number of hot dogs
sold
by all hot dog stands and a static variable that returns the value in
this
variable.
Write a main method to test your class with at least three hot dog
stands
that each sell a varity of hot dogs . We had to call this one public
class
program2JS. the JS is our initials. I really need help with this i'm
not
sure i even wrote it correctly thanks
the compile code that i'm getting is as follows
unexpected type required variable s1.getTotalSold() = s1.getNumSold +
s2.getNumSold + s3.getNumSold;
also i don't know what code to put in the public void justSold() and i
don't know how to add justSold each time something is sold to the
getNumSold and i don't know what to program in the public void
justSold() any suggestions or help please judith the program is below

// Program Name: Program2JS.java

import java.util.Scanner;

class HotDogStand
{

private int id;
private int numSold;
private static int totalSold = 0;

public HotDogStand()
{
id = 0;
numSold = 0;
totalSold = 0;
}
public HotDogStand(int newId, int newNumSold)
{
id = newId;
numSold = newNumSold;
}
public int getId()
{
return id;
}
public void setId(int newId)
{
id = newId;
}
public int getNumSold()
{
numSold ++;
return numSold;
}
public int getTotalSold()
{
return totalSold;
}
public void justSold()
{
totalSold = numSold ++;
}
}

public class program2JS
{
public static void main (String [] args)
{
HotDogStand s1 = new HotDogStand();
HotDogStand s2 = new HotDogStand();
HotDogStand s3 = new HotDogStand();
s1.getId();
s2.getId();
s3.getId();
s1.setId(1);
s2.setId(2);
s3.setId(3);
s1.getNumSold();
s2.getNumSold();
s3.getNumSold();
s1.justSold();
s2.justSold();
s3.justSold();
s1.getTotalSold();

int i;
for(i = 1; i <=5; i++)
s1.justSold();
for(i = 1; i <=5; i++)
s2.justSold();
for(i = 1; i <=5; i++)
s3.justSold();
for(i = 1; i <=5; i++)
s1.getNumSold();
for(i = 1; i <=5; i++)
s2.getNumSold();
for(i = 1; i <=5; i++)
s3.getNumSold();

s1.getTotalSold() = s1.getNumSold ()+ s2.getNumSold ()+
s3.getNumSold();

//Test our code with three hot dog stands
//sold at stand 1, 2

s1.justSold();
s2.justSold();
s1.justSold();


System.out.println("Stand" + s1.getId() + "Sold" + s1.getNumSold());
System.out.println("Stand" + s2.getId() + "Sold" + s2.getNumSold());
System.out.println("Stand" + s3.getId() + "Sold" + s3.getNumSold());
System.out.println("Total sold = " + s1.getTotalSold() + "\n");

//Sold some more

s3.justSold();
s1.justSold();


System.out.println("Stand" + s1.getId() + "Sold" + s1.getNumSold());
System.out.println("Stand" + s2.getId() + "Sold" + s2.getNumSold());
System.out.println("Stand" + s3.getId() + "Sold" + s3.getNumSold());
System.out.println("Total sold = " + s1.getTotalSold() + "\n");
}
}
 
Last edited by a moderator:
M

Michael Rauscher

judith said:
dear someone who can help
The first attachment is the program i wrote the second attachment is
the
required guidelines from the instructor.

the compile code that i'm getting is as follows
unexpected type required variable s1.getTotalSold() = s1.getNumSold +
s2.getNumSold + s3.getNumSold;

You can't assign a value to the return value of a method.
also i don't know what code to put in the public void justSold() and i
don't know how to add justSold each time something is sold to the
getNumSold and i don't know what to program in the public void
justSold() any suggestions or help please judith the program is below

You don't have any idea of Java, right?

Your code has a few errors:

// we want to increase both, the numSold and the totalSold values
public void justSold() {
numSold++;
totalSold++;
}

// nothing to increase here
public int getNumSold() {
return numSold;
}

// should be a static method since it accesses a static variable
public static int getTotalSold()

In the main method:
- remove anything between the last HotDogStand declaration (s3) and "int i"
- keep the following three for loops (those who call justSold) but
ensure that they count up to different maximum values (not always 5)
- remove the following three for loops (those who call getNumSold)
because they're useless
- remove the s1.getTotalSold() = ... line
- replace the last System.out.println line by
System.out.println("Total sold = " +
HotDogStand.getTotalSold() + "\n");

Bye
Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,228
Latest member
MikeMichal

Latest Threads

Top