Question

In: Computer Science

Programming language to be used: Java Exercises Part 1) The Dog Class In the first part...

Programming language to be used: Java

Exercises

Part 1) The Dog Class

In the first part of the lab, we are writing a class to represent a Dog. It should not have a main method.

  1. Dog needs fields for price (to purchase the dog), breed, name, and age. Use appropriate data types
  2. The class should have the following two kinds of Constructors:
    1. Constructor 1: Write a constructor that accepts a value for each of the fields
    2. Constructor 2: Write a constructor that accepts no arguments and sets default values of your choice
  3. The Dog class requires:
    1. Setters and getters for all fields
    2. A toString() method, which
      1. Returns String
      2. Returns a String describing the dog, such as

Sebastian the Husky is 5 years old.
$45.56 adoption fee

  1. Please note that the dollars must be formatted that way

Part 2) The Sales Class

In the second part of the lab, we are writing a class to represent the buying of a Dog or multiple Dogs. Let’s not consider the logistics of buying Dogs in bulk.

  1. The Sales class has the following fields – a PrintWriter for a file being used (as well as any other fields you decide you want to help with that) and a double holding the total price of dogs purchased
  2. A Sales class has one constructor – it should take a file name to save the output to
  3. The Sales class needs the following functions
    1. An addDog method, which saves a new dog to the file (using the toString to get the String representation) and adds the price to the total double
    2. A finalizeReport method, which closes the file after adding a bottom line (“----“) and printing the total price below that line, properly formatted

Part 3) The DogMarket Class

This class should get a file name from the user. It should use the file to create a Sales class object. Then, it should prompt the user for details of the dogs they want to buy. Use a while loop, which uses the word “quit” to not buy a dog and the word “buy” to buy a dog – other words should be ignored, but should not quit the program. If the user says “buy”, ask them for all of the dog’s info, then make a new Dog object and save it to the file using the Sales object. Once the user is done buying dogs, the program should exit, being sure to use finalizeReport() on the sales object

Solutions

Expert Solution

//Dog.java
public class Dog {

   private double price;
   private String breed;
   private String name;
   private int age;
  
// default constructor
   public Dog()
   {
       price = 45.56;
       breed= "Husky";
       name = "Sebastian";
       age = 5;
   }
  
   //parameterized constructor
   public Dog(String name, String breed, double price, int age)
   {
       this.name = name;
       this.breed = breed;
       this.price = price;
       this.age = age;
   }
  
   // setters
   public void setName(String name)
   {
       this.name = name;
   }
  
   public void setBreed(String breed)
   {
       this.breed = breed;
   }
  
   public void setPrice(double price)
   {
       this.price = price;
   }
  
   public void setAge(int age)
   {
       this.age = age;
   }
  
   // getters
   public String getName()
   {
       return name;
   }
  
   public String getBreed()
   {
       return breed;
   }
  
   public double getPrice()
   {
       return price;
   }
  
   public int getAge()
   {
       return age;
   }
  
   //toString
   public String toString()
   {
       return(name+" the "+breed+" is "+age+" years old.\n"+"$"+String.format("%.2f",price)+" adoption fee");
   }
}
//end of Dog.java

//Sales.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;

public class Sales {
  
   private PrintWriter writer;
   private double totalPrice;
  
   // constructor
   public Sales(String filename) throws FileNotFoundException
   {
       writer = new PrintWriter(new File(filename));
       totalPrice = 0;
   }
  
   // method to add dog details in file and add the price of dog to total price
   public void addDog(Dog dog)
   {
       writer.write(dog.toString()+"\n");
       totalPrice += dog.getPrice();
   }

   // output the total price to file and close the file
   public void finalizeReport()
   {
       writer.write("----\n");
       writer.write("Total price : $"+String.format("%.2f", totalPrice));
       writer.close();
   }
}
//end of Sales.java

// DogMarket.java
import java.io.FileNotFoundException;
import java.util.Scanner;

public class DogMarket {

   public static void main(String[] args) throws FileNotFoundException {
      
       Scanner scan = new Scanner(System.in);
       String filename;
       String command, name, breed;
       double price;
       int age;
       // input the filename
       System.out.print("Enter the output filename : ");
       filename = scan.next();
       scan.nextLine();
       Sales sales = new Sales(filename); // create a Sales object
       // loop that continues till the user quits
       do {
           System.out.println("Buy or Quit ");
           System.out.print("Enter your choice (buy or quit) : ");
           command = scan.nextLine();
          
           // if user selects to buy a dog, input details of Dog and add the dog to Sales object
           if(command.equalsIgnoreCase("buy"))
           {
               System.out.print("Name of the Dog : ");
               name = scan.nextLine();
               System.out.print("Breed of the Dog : ");
               breed = scan.nextLine();
               System.out.print("Enter age of the Dog : ");
               age = scan.nextInt();
               scan.nextLine();
               System.out.print("Enter the price of the Dog : ");
               price = scan.nextDouble();
               scan.nextLine();
               sales.addDog(new Dog(name,breed,price, age));
           }
       }while(!command.equalsIgnoreCase("quit"));
      
       sales.finalizeReport(); // once user quits, call finalize report
       scan.close();
      
   }

}
//end of DogMarket.java

Output:

Output file:


Related Solutions

Java programming language should be used Implement a class called Voter. This class includes the following:...
Java programming language should be used Implement a class called Voter. This class includes the following: a name field, of type String. An id field, of type integer. A method String setName(String) that stores its input into the name attribute, and returns the name that was just assigned. A method int setID(int) that stores its input into the id attribute, and returns the id number that was just assigned. A method String getName() that return the name attribute. A method...
Modify Account Class(Java programming) used in the exercises such that ‘add’ and ‘deduct’ method could only...
Modify Account Class(Java programming) used in the exercises such that ‘add’ and ‘deduct’ method could only run if the account status is active. You can do this adding a Boolean data member ‘active’ which describes account status (active or inactive). A private method isActive should also be added to check ‘active’ data member. This data member is set to be true in the constructor, i.e. the account is active the first time class Account is created. Add another private method...
JAVA PROGRAMMING Part 1 Create a class Student, with attributes id, first name, last name. (All...
JAVA PROGRAMMING Part 1 Create a class Student, with attributes id, first name, last name. (All the attributes must be String) Create a constructor that accepts first name and last name to create a student object. Create appropriate getters and setters Create another class StudentOperationClient, that contains a main program. This is the place where the student objects are created and other activities are performed. In the main program, create student objects, with the following first and last names. Chris...
PUT IN JAVA PROGRAMMING LANGUAGE The Rectangle class: Design a class named Rectangle to represent a...
PUT IN JAVA PROGRAMMING LANGUAGE The Rectangle class: Design a class named Rectangle to represent a rectangle. The class contains: • Two double data fields named width and height that specify the width and height of a rectangle. The default values are 1 for both width and height. • A no-arg (default) constructor that creates a default rectangle. • A constructor that creates a rectangle with the specified width and height. • A method named findArea() that finds the area...
In the programming language java: Write a class encapsulating the concept of a telephone number, assuming...
In the programming language java: Write a class encapsulating the concept of a telephone number, assuming a telephone number has only a single attribute: aString representing the telephone number. Include a constructor, the accessor and mutator, and methods 'toString' and 'equals'. Also include methods returning the AREA CODE (the first three digits/characters of the phone number; if there are fewer than three characters in the phone number of if the first three characters are not digits, then this method should...
Using the Java programming language: Create and implement a class Car that models a car. A...
Using the Java programming language: Create and implement a class Car that models a car. A Car is invented to have a gasoline tank that can hold a constant max of 12.5 gallons, and an odometer that is set to 0 for a new car. All cars have an original fuel economy of 23.4 miles per gallon, but the value is not constant. Provide methods for constructing an instance of a Car (one should be zero parameter, and the other...
Java Programming Part 1 (20%) Implement a class with a main method. Using an enhanced for...
Java Programming Part 1 (20%) Implement a class with a main method. Using an enhanced for loop, display each element of this array: String[] names = {"alice", "bob", "carla", "dennis", "earl", "felicia"}; Part 2 (30%) In a new class, implement two methods that will each calculate and return the average of an array of numeric values passed into it. Constraints: your two methods must have the same name one method should accept an array of ints; the other should accept...
***In Java language***The first programming project involves writing a program that computes the salaries for a...
***In Java language***The first programming project involves writing a program that computes the salaries for a collection of employees of different types. This program consists of four classes. 1. The first class is the Employee class, which contains the employee's name and monthly salary, which is specified in whole dollars. It should have three methods: a. A constructor that allows the name and monthly salary to be initialized. b. A method named annualSalary that returns the salary for a whole...
Using python programming language, compare the four tasks. 1. For the following exercises you will be...
Using python programming language, compare the four tasks. 1. For the following exercises you will be writing a program that implements Euler’s method (pronounced ‘oy-ler’). It may be the case that you have not yet encountered Euler’s method in the course. Euler’s method can be found in Chapter 10.2 of your notes, which gives a good description of why this algorithm works. But for our purposes, we only need to know the algorithm itself - so there’s no need to...
The programming language that is being used here is JAVA, below I have my code that...
The programming language that is being used here is JAVA, below I have my code that is supposed to fulfill the TO-DO's of each segment. This code in particular has not passed 3 specific tests. Below the code, the tests that failed will be written in bold with what was expected and what was outputted. Please correct the mistakes that I seem to be making if you can. Thank you kindly. OverView: For this project, you will develop a game...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT