Question

Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName...

Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName (a String), numSold (an integer that represents the number of that type of game sold), and priceEach (a double that is the price of each of that type of Game). I only want three instance variables!! The class should have the following methods:

  1. A constructor that has two parameter – a String containing the name of the Game and a double containing its price. The constructor should set the other instance variable to zero.
  2. An empty constructor
  3. A method updateSales() that takes a single integer parameter representing the number of additional games of that type sold. The method should add this number to the numSold instance variable and return nothing
  4.      A method totalValue() that takes no parameters and returns the total value of that game item. This is calculated by multiplying the priceEach times the numSold.
  5.     A toString() method that returns a string containing the name of the game, the number sold, the price each, and the total value
  6. All getters and setters

Write a GameDriver program that uses Game objects to track the sales of games sold at a variety of stores.

  1. Ask the user for how many different types of games are being sold and read in the answer.
  2. Ask the user for how many stores and read in the answer
  3. Use loops. You will need nested loops. The outside one will read in each game and the inside one is for each sale for that game.
  4. Read in the names and prices of the first game. Create an instance of the class.
  5. Loop through reading in sales for each of the stores. Call the updateSales() method after each store.
  6. Call your toString() method to print that game.
  7. Repeat steps d-f for each of the other games.

Homework Answers

Answer #1
import java.util.Scanner;

public class Game {
        
        /*
         * Instance variable in Our Game class
         */
        private String gameName;
        private int numSold;
        private double priceEach;
        
        /*
         * Getters and setters for each field
         */
        public String getGameName() {
                return gameName;
        }
        public void setGameName(String gameName) {
                this.gameName = gameName;
        }
        public int getNumSold() {
                return numSold;
        }
        public void setNumSold(int numSold) {
                this.numSold = numSold;
        }
        public double getPriceEach() {
                return priceEach;
        }
        public void setPriceEach(double priceEach) {
                this.priceEach = priceEach;
        }
        
        /*
         * Default Constructor
         */
        public Game() {
                
        }
        
        /*
         * Parameterized Constructor contains two variables as a parameter
         *i.e. GameName and Price of Each Game.
         *   
         */
        
        public Game(String gameName, double priceEach) {
                this.gameName=gameName;
                this.priceEach=priceEach;
                numSold=0;
        }
        
        /*
         * This method basically counts the number of particular game sold from all stores
         */
        public void updateSales(int additional) {
                numSold=numSold+additional;
        }
        
        /*
         * Total Earning from the every sales market for a particular game
         */
        public double totalValue() {
                return (priceEach*numSold);
        }
        
        /*
         * toString method to print output
         */
        
        @Override
        public String toString() {
                
                String result=gameName+"\t"+numSold+"\t$"+priceEach+"\t$"+totalValue();
                return result;
        }

        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                /*
                 * Total games and stores available in market
                 */
                System.out.println("How many different types of games are being sold");
                int totalGame=sc.nextInt();
                System.out.println("How many stores");
                int stores=sc.nextInt();
                
                /*
                 *For each game we used this for loop.
                 *and get the name,price of each game and use our contructors in it.
                 *We also create an object of particular class.
                 * 
                 */
                
                for (int i = 0; i < totalGame; i++) {
                        System.out.println("Game name");
            /*
             * if you String contains whitespace then we have to scan complete line. 
            */ 
                        sc.nextLine();
                        String gN=sc.nextLine();
                        System.out.println("price");
                        double price=sc.nextDouble();
                        Game game = new Game(gN,price);
                        /*
                         * This loop  is used to count the total number of sales for a particular product
                         */
                        for (int j = 0; j < stores; j++) {
                                System.out.println("Enter sale from this store");
                                int sales=sc.nextInt();
                                game.updateSales(sales);
                        }
                        System.out.println(game.toString());
                }
        }

}

The explaination of code is with the program. I hope you like the solution .Support us by pressing that upvote button.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all...
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all of the methods required for a standard user defined class: constructors, accessors, mutators, toString, equals Create the client for testing the Student class (5 points extra credit) Create another class called CourseSection (20 points extra credit) Include instance variables for: course name, days and times course meets (String), description of course, student a, student b, student c (all of type Student) Create all of...
Write the program in java Implement a class Product. Create instance variables to store product name...
Write the program in java Implement a class Product. Create instance variables to store product name and price and supply the values through constructor. For example new Product(“Toaster’, 29.95). Create methods, getName, getPrice. Write a method productPrinter that prints the product name and its price after reducing it by $5. Create a main class and necessary constructs in the main class to run the Product class.
Create a class called Employee that contains three instance variables (First Name (String), Last Name (String),...
Create a class called Employee that contains three instance variables (First Name (String), Last Name (String), Monthly Salary (double)). Create a constructor that initializes these variables. Define set and get methods for each variable. If the monthly salary is not positive, do not set the salary. Create a separate test class called EmployeeTest that will use the Employee class. By running this class, create two employees (Employee object) and print the annual salary of each employee (object). Then set the...
Write the following problem in Java Create a class Dog, add name, breed, age, color as...
Write the following problem in Java Create a class Dog, add name, breed, age, color as the attributes. You need to choose the right types for those attributes. Create a constructor that requires no arguments. In this constructor, initialize name, breed, age, and color as you wish. Define a getter and a setter for each attribute. Define a method toString to return a String type, the returned string should have this information: “Hi, my name is Lucky. I am a...
For the following class Book: 1- Write a default constructor to give default values (title= Intro...
For the following class Book: 1- Write a default constructor to give default values (title= Intro to Programming and price = 20) to the class’s variables. 2 - Write a parameterized constructor to allow the user to initialize the class variables. 3- Write getter & setter for each variable of this class. 4- Then write a main code to create an instance of this class using parameterized constructor, ask user for inputs to initialize the variables of this instance and...
Write a Python class, Flower, that has 3 instance variables name, num_petals and price which have...
Write a Python class, Flower, that has 3 instance variables name, num_petals and price which have types str, int and float. Your class must have a constructor method to initialize the variables to an appropriate value, and methods for setting the value of each instance variable (set methods) and for retrieving the instance variable values (get methods). You can use the credit card code we looked at for assistance.
In C++ Employee Class Write a class named Employee (see definition below), create an array of...
In C++ Employee Class Write a class named Employee (see definition below), create an array of Employee objects, and process the array using three functions. In main create an array of 100 Employee objects using the default constructor. The program will repeatedly execute four menu items selected by the user, in main: 1) in a function, store in the array of Employee objects the user-entered data shown below (but program to allow an unknown number of objects to be stored,...
write a c++ program A class called car (as shown in the class diagram) contains: o...
write a c++ program A class called car (as shown in the class diagram) contains: o Four private variables: carId (int), carType (String), carSpeed (int) and numOfCars (int). numOfCars is a static counter that should be  Incremented whenever a new Car object is created.  Decremented whenever a Car object is destructed. o Two constructors (parametrized and copy constructor) and one destructor. o Getters and setters for the Car type, ID, and speed. And static getter function for numOfCars....
Java Programming In this lab students continue to write programs using multiple classes. By the end...
Java Programming In this lab students continue to write programs using multiple classes. By the end of this lab, students should be able to Write classes that use arrays and ArrayLists of objects as instance variables Write methods that process arrays and ArrayLists of objects Write getter and setter methods for instance variables Write methods using object parameters and primitive types Question- class StringSet. A StringSet object is given a series of up to 10 String objects. It stores these...
JAVA: MUST BE DONE IN JAVA Assignment: Write algorithms and programs to play “non-betting” Craps. Craps...
JAVA: MUST BE DONE IN JAVA Assignment: Write algorithms and programs to play “non-betting” Craps. Craps is a game played with a pair of dice. In the game, the shooter (the player with the dice) rolls a pair of dice and the number of spots showing on the two upward faces are added up. If the opening roll (called the “coming out” roll) is a 7 (“natural”) or 11 (“yo-leven”), the shooter immediately wins the game. If the coming out...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT