MUST DO THE WHOLE ENTIRE PROJECT. HAVE IT ORGANIZED AND CLEAN. Programming Proje
ID: 3597810 • Letter: M
Question
MUST DO THE WHOLE ENTIRE PROJECT. HAVE IT ORGANIZED AND CLEAN.
Programming Project Purpose
The purpose of this programming project will be to practice the creation and use of a basic Java class as a data type within a Java program. You will create a basic Java class, use that class as a data type and write code that demonstrates that reference variables and instance objects created from your class operate correctly.
Write the Java code of this programming project as if you are a member of a team of programmers working on a large project. As part of the project, you have been assigned to create a specific class to be used as a data type and then test the class to demonstrate that it works correctly.
Step One: Code the Pet Information Class
The pet information class must contain data members (fields) that maintain the state information about a pet for for the records of a veterinary clinic. I. e. the pet information class
• the name of the pet // must not be a zero length string
• if the pet is a dog or a cat
• the breed of the pet // must not be a zero length string
• the age of the pet // must be a value > 0 and may contain a fraction
• the weight of the pet // in pounds or kilos, may contain a fraction, must be > 0
• the name of the owner of the pet // must not be a zero length string
Pet Information Class Design Requirements
• All class data members (fields) must be private.
• All class data members (fields) must be initialized to default or neutral values as appropriate. For example: “-na-” or "unknown" could be used for strings, false or true for Booleans, and a negative number or zero for numeric types.
• All class data members (fields) must have “getter” (accessor) methods and “setter” (mutator or manipulator) methods.
• The class must have a default constructor and a parametrized constructor. A call to a constructor with operator new must result in all class data members (fields) being initialized.
• Class data members (fields) may never contain invalid values, except those values used as initial values when an object of the class is created with the operator new.
• The class may not include any input or output. Data may be passed in as a parameter to a method or constructor or returned by a method.
• Class data members (fields) must not depend on external to the object storage. I. e. a class data member can be initialized by a value passed to a constructor or a setter, but all getters must be NOT be passed any values and must return the values stored in the class data members (fields) without being dependent on any data stored outside of the object.
• No method of the class may be static.
Step Two: Test the Pet Information Class
• In the main method of class Pp1_your-id, make objects of the pet information class by calling both constructors and use the reference variables to call all methods.
• When you are satisfied that your class words correctly, erase the testing code (just the code in main, not the code in your class!).
Step Three: Write the Demonstration Code in the main Method of Class P p 1_ your-clid-or-ulid
Demonstrate that your pet information class works correctly as a data type as follows.
In class Pp1_your-id, create static methods that
• receives an array of type pet information class and collects pet information from the user and stores this information in the pet class objects referenced by all elements in the array.
• receives an array of type pet information class and lists the information about each pet contained in the array to the terminal screen.
• receives an array of type pet information class and verifies the weight of each pet in the array with the user. The user may elect to enter a new weight for any of the pets.
• receives an array of type pet information class and returns the average age of all dogs stored in the array
• receives an array of type pet information class and returns the average age of all cats stored in the array
• receives an array of type pet information class and returns the average weight of all dogs stored in the array
• receives an array of type pet information class and returns the average weight of all cats stored in the array
In the main method
• create an array of 5 elements with the pet information class as the data type.
• calls each of the static methods in order to: collect information about 5 pets, list all information stored about the all pets check and if necessary correct the weights of all pets stored in the array receive and output the average weights and the average age of all dogs and cats stored in the array.
MUST DO THIS PART
• The pet information class includes a method toString based on the following header. toString must return a string summary that includes all the state information (data stored in the class fields) and must not receive any any values passed as parameters. toString must be used in the the class Pp1_your-id to produce output. public String toString()
Tips:
• The array elements must be individually initialized by operator new. This can be done in method main or in another method, such as the method that collects the pet data.
• A Scanner class object may be created in method main and passed to appropriate methods as required or a Scanner class object may be created in a method that needs user input.
• The average weights and ages can be calculated by two or four methods. For example, the average weight of cats can be returned by one method and the average weight of dogs can be returned by another. However, the average weight of either dogs or cats can be returned by a single method that receives as a second parameter the type of pet to average.
• Watch out for numeric to a line of string input (nextInt() or nextDouble() to nextLine() calls in class Scanner. This requires a "dummy read" in between the read of the number and the read of the whole line. Example: If "input" is the Scanner reference variable: double = input.nextDouble(); input.nextLine(); String s = input.nextLine();
Additional Requirements
(a) Identifiers must be descriptive, i. e. must self document. The only exception granted is in the case of a “for variable”, that is a variable created as a simple counter as in the control variable in a “for” loop statement.
(b) Indention of all code blocks (compound statements, anything in braces), including single statements following selection or while statements, is required. NetBeans will do this fairly automatically as you type if your syntax is correct. ALT-SHIFT-F will re-format a whole file (if y
Explanation / Answer
import java.util.Scanner; public class Game { private Player p1; private Player p2; private Dice dice; private int scoreToWin; void displayGameMenu() { System.out.println(); System.out.println("(1) Start a new game"); System.out.println("(2) Play one round"); System.out.println("(3) Who is leading now?"); System.out.println("(4) Display game help"); System.out.println("(5) Exit game"); System.out.print("Choose an option: "); } void selectGameOption(int optionSelected) { switch (optionSelected) { case 1: this.startNewGame(); break; case 2: this.playOneRound(p1); this.playOneRound(p2); break; case 3: this.whoIsLeading(); break; case 4: this.displayGameInstruction(); break; default: break; } } void startNewGame() { String p1Name; String p2Name; Scanner sc = new Scanner(System.in); System.out.print("Please enter player one name: "); p1Name = sc.nextLine(); System.out.print("Please enter player two name: "); p2Name = sc.nextLine(); System.out.print("Please enter the maximum score required to win: "); scoreToWin = sc.nextInt(); p1 = new Player(p1Name); p2 = new Player(p2Name); dice = new Dice(); } void playOneRound(Player p) { int result; int firstDiceRoll = dice.rollDice(); int secondDiceRoll = dice.rollDice(); if (firstDiceRoll == secondDiceRoll) { result = (firstDiceRoll + secondDiceRoll) * 2; p.setTotalScore(result); System.out.printf("%s rolled %d and %d, " + "and scored %d points(BONUS DOUBLE POINTS), " + "for a total of %d points", p.getName(), firstDiceRoll, secondDiceRoll, result, p.getTotalScore() ); } else { result = (firstDiceRoll + secondDiceRoll); p.setTotalScore(result); System.out.printf("%s rolled %d and %d, " + "and scored %d points, " + "for a total of %d points", p.getName(), firstDiceRoll, secondDiceRoll, result, p.getTotalScore() ); } System.out.println(); } void whoIsLeading() { if (p1.getTotalScore() == p2.getTotalScore()) { System.out.format("Its currently a draw, " + "%s has %d, %s has %d", p1.getName(), p1.getTotalScore(), p2.getName(), p2.getTotalScore() ); } else if (p1.getTotalScore() > p2.getTotalScore()) { System.out.printf("%s is leading, %s has %d points, " + "%s has %d points", p1.getName(), p1.getName(), p1.getTotalScore(), p2.getName(), p2.getTotalScore()); } else if (p1.getTotalScore() = scoreToWin && p2.getTotalScore() >= scoreToWin) { System.out.println("Its a draw! Both players have exceeded the score limit"); return true; } else if (p1.getTotalScore() >= scoreToWin && p2.getTotalScore() 5 || optionSelected < 0) { System.out.print("Option entered invalid, please enter a number from 1 to 5: "); optionSelected = sc.nextInt(); } if (optionSelected == 5) { System.out.println("Exiting game"); break; } game.selectGameOption(optionSelected); boolean anyoneWin = game.checkIfAnyoneHasWon(); if (anyoneWin) { System.out.println(); System.out.println("Game ended."); break; } } } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.