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
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
Create a class called Employee that should include four pieces of information as instance variables—a firstName...
Create a class called Employee that should include four pieces of information as instance variables—a firstName (type String), a lastName (type String), a mobileNumber (type String) and a salary (type int). Your class (Employee) should have a full argument constructor that initializes the four instance variables. Provide a set and a get method for each instance variable. The validation for each attribute should be like below: mobileNumber should be started from “05” and the length will be limited to 10...
JAVA Write a Student class with a String name and double gpa class members. Write a...
JAVA Write a Student class with a String name and double gpa class members. Write a constructor, get and set methods, and a toString method.
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...
Haviing trouble with my homework (java) 1)Write the class CustomerAccount, which has fields/attributes called custID of...
Haviing trouble with my homework (java) 1)Write the class CustomerAccount, which has fields/attributes called custID of type String, custName of type String and checkingBalance of type double and savingBalance of type double 2)Write the constructor for CustomerAccount class. The constructor takes parameters to initialize custID, custName, checkingBalance and savingBalance. 3)Write the mutator method for all attributes of the class CustomerAccount. then Write the toString method for class CustomerAccount.
1. Circle: Implement a Java class with the name Circle. It should be in the package...
1. Circle: Implement a Java class with the name Circle. It should be in the package edu.gcccd.csis. The class has two private instance variables: radius (of the type double) and color (of the type String). The class also has a private static variable: numOfCircles (of the type long) which at all times will keep track of the number of Circle objects that were instantiated. Construction: A constructor that constructs a circle with the given color and sets the radius to...
In Java: Assume the existence of a Window class with int instance variables width, height, xPos...
In Java: Assume the existence of a Window class with int instance variables width, height, xPos and yPos. Define the method toString for the Window class. The String representation of a Window object should be of the form "A 'width'x'height' window at ('xPos','yPos')" where the values within the apostrophes are replaced by the actual values of the corresponding instance variables. For example, given a Window object whose width is 80, height is 20, xPos is 0 and yPos is 30,...
Modify the Employee9C superclass so that is an abstract superclass with a constructor to set its...
Modify the Employee9C superclass so that is an abstract superclass with a constructor to set its variables (firstName and lastName). It should contain an abstract method called payPrint. Below is the source code for the Employee9C superclass: public class Employee9C {    //declaring instance variables private String firstName; private String lastName; //declaring & initializing static int variable to keep running total of the number of paychecks calculated static int counter = 0;    //constructor to set instance variables public Employee9C(String...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT