You operate several news paper stands distributed throughout town. Define a clas
ID: 3673147 • Letter: Y
Question
You operate several news paper stands distributed throughout town. Define a class named NewsPaperStand that has a member variable for the news paper stand’s ID number and a member variable for how many news papers the stand sold that day. Create a constructor that allows a user of the class to initialize both values.
Also create a function named OneSold that increments the number of news papers the stand has sold by one. This function will be invoked each time the stand sells a news paper so that you can track the total number of news papers sold by the stand. Add another function that returns the number of news papers sold.
Finally, add a static variable that tracks the total number of news papers sold by allnews paper stands and a static function that returns the value in this variable. Write a main function to test your class with at least three news paper stands that each sell a variety of news papers.
Explanation / Answer
/**
* @author srinu
*
*/
public class NewsPaperStand {
String standsID;
int standsSold;
static int newsPapersSold = 0;
/**
* @param standsID
* @param standsSold
*/
public NewsPaperStand(String standsID, int standsSold) {
this.standsID = standsID;
this.standsSold = standsSold;
newsPapersSold += standsSold;
}
/**
*
*/
public void OneSold() {
standsSold++;
newsPapersSold++;
}
/**
* @return
*/
public int numberOfNewsPapersStandsSold() {
return standsSold;
}
/**
* @return the newsPapersSold
*/
public static int getNewsPapersSold() {
return newsPapersSold;
}
/**
* @param newsPapersSold
* the newsPapersSold to set
*/
public static void setNewsPapersSold(int newsPapersSold) {
NewsPaperStand.newsPapersSold = newsPapersSold;
}
/**
* @param args
*/
public static void main(String[] args) {
NewsPaperStand newsPaperStand1 = new NewsPaperStand("news paper1", 5);
NewsPaperStand newsPaperStand2 = new NewsPaperStand("news paper2", 6);
NewsPaperStand newsPaperStand3 = new NewsPaperStand("news paper3", 7);
System.out.println("News paper1 :"
+ newsPaperStand1.numberOfNewsPapersStandsSold());
System.out.println("News paper2 :"
+ newsPaperStand2.numberOfNewsPapersStandsSold());
System.out.println("News paper3 :"
+ newsPaperStand3.numberOfNewsPapersStandsSold());
System.out.println("News paper1 sold by one: ");
newsPaperStand1.OneSold();
System.out.println("News paper1 : "
+ newsPaperStand1.numberOfNewsPapersStandsSold());
System.out
.println("the total number of news papers sold by allnews paper stands : "
+ NewsPaperStand.getNewsPapersSold());
}
}
OUTPUT:
News paper1 :5
News paper2 :6
News paper3 :7
News paper1 sold by one:
News paper1 : 6
the total number of news papers sold by allnews paper stands : 19
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.