Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2006

Tip: Looking for answers? Try searching our database.

program and compile error

Thread view: 
judith - 25 Sep 2006 05:41 GMT
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 Judith Spurlock my email address
is
jspurlock83@hotmail.com

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

// Author:    Judith Spurlock
// Course:    ITSE 2417
// Program No:    2
// Due Date:    9/29/2006
//
// 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");
    }
}
Michael Rauscher - 25 Sep 2006 07:39 GMT
judith schrieb:
> dear someone who can help
> The first attachment is the program i wrote the second attachment is
[quoted text clipped - 4 lines]
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.