Question

In: Computer Science

*JAVA* For this assignment you have been given two classes, a Main.java and a Coin.java. The...

*JAVA*

For this assignment you have been given two classes, a Main.java and a Coin.java. The coin class represents a coin. Any object made from it will have a 1) name, 2) weight and 3) value. As of now, the instance variables in Coin.java are all public, and the main function is calling these variables directly for the one coin made in it.

Your goal is to enforce information hiding principles in this project. Take Coin.java, make all instance variables private and create set/get functions for each instance variable. Then replace the direct references in main() to each instance variable with a call to an appropriate set or get function.

OUTPUT:

Look at the output the program prints before you edit your two scripts. Your final output should be identical to it. You should not change the output for the program when editing the scripts.

RUBRIC:

  • Coin.java instance variables private.
  • Coin.java has set/get functions for each variables. Said functions are written correctly and function.
  • Direct calls to instance variables in Main are replaced by appropriate set/get function calls.

Do not make a constructor for Coin.java.

Here are the pre-made scripts that are needed to be modify:

Coin.java:

public class Coin

{

public String coinName = "";

public int value = 0;

public double weight = 0;

}//end class

Main.java:

public class Main

{

public static void main(String[] args)

{

Coin penny = new Coin();

penny.coinName = "Penny";

penny.value = 1; penny.weight = 0.003;

System.out.println("Coin name: " + penny.coinName);

System.out.println("Coin value: " + penny.value + " cent(s)");

System.out.println("Coin weight: " + penny.weight + " pound(s)");

}//end main function

}//end class

Solutions

Expert Solution

Thanks for the question.
Here is the completed code for this problem. 

Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please rate the answer. 

Thanks!

===========================================================================

public class Coin {

    //make all instance variables private
    private String coinName = "";
    private int value;
    private double weight;

    public Coin() {
        coinName = "";
        value = 0;
        weight = 0;
    }

    //create set/get functions for each instance variable.
    public String getCoinName() {
        return coinName;
    }

    //create set/get functions for each instance variable.
    public double getWeight() {
        return weight;
    }

    //create set/get functions for each instance variable.
    public int getValue() {
        return value;
    }

    //create set/get functions for each instance variable.
    public void setCoinName(String coinName) {
        this.coinName = coinName;
    }

    //create set/get functions for each instance variable.
    public void setValue(int value) {
        this.value = value;
    }

    //create set/get functions for each instance variable.
    public void setWeight(double weight) {
        this.weight = weight;
    }
}//end class

============================================================

 public class Main {

    public static void main(String[] args) {

        Coin penny = new Coin();

        // use setter to set values
        penny.setCoinName("Penny");
        penny.setValue(1);
        penny.setWeight(0.003);

        // use getters to get values of the object istance variables
        System.out.println("Coin name: " + penny.getCoinName());
        System.out.println("Coin value: " + penny.getValue() + " cent(s)");
        System.out.println("Coin weight: " + penny.getWeight() + " pound(s)");

    }//end main function

}//end class

============================================================


Related Solutions

java programming You will be given two interfaces and two abstract classes, FileTextReader, FileTextWriter, AbstractFileMonitor, and...
java programming You will be given two interfaces and two abstract classes, FileTextReader, FileTextWriter, AbstractFileMonitor, and AbstractDictionary. Your job is to create two classes the first class should be named FileManager, the second class should be named Dictionary. The FileManager will implement the interfaces FileTextReader and FileTextWriter and extend the clas AbstractFileMonitor. Your class signature would look something like the following: public class FileManager extends AbstractFileMonitor implements FileTextReader, FileTextWriter{... The constructor signature of the FileManager should look like the following:...
java programming You will be given two interfaces and two abstract classes, FileTextReader, FileTextWriter, AbstractFileMonitor, and...
java programming You will be given two interfaces and two abstract classes, FileTextReader, FileTextWriter, AbstractFileMonitor, and AbstractDictionary. Your job is to create two classes the first class should be named FileManager, the second class should be named Dictionary. The FileManager will implement the interfaces FileTextReader and FileTextWriter and extend the clas AbstractFileMonitor. Your class signature would look something like the following: public class FileManager extends AbstractFileMonitor implements FileTextReader, FileTextWriter{... The constructor signature of the FileManager should look like the following:...
C++ Assignment 1: Make two classes practice For each of the two classes you will create...
C++ Assignment 1: Make two classes practice For each of the two classes you will create for this assignment, create an *.h and *.cpp file that contains the class definition and the member functions. You will also have a main file 'main.cpp' that obviously contains the main() function, and will instantiate the objects and test them. Class #1 Ideas for class one are WebSite, Shoes, Beer, Book, Song, Movie, TVShow, Computer, Bike, VideoGame, Car, etc Take your chosen class from...
Write a Java Program using the concept of objects and classes. Make sure you have two...
Write a Java Program using the concept of objects and classes. Make sure you have two files Employee.java and EmployeeRunner.java. Use the template below for ease. (Please provide detailed explanation) Class Employee Class Variables: Name Work hour per week Hourly payment Class Methods: - Get/Sets - generateAnnualSalary(int:Duration of employment)               annual salary = Hourly payment * Work hours per week * 50 If the employee is working for more than 5 years, 10% added bonus             -void displayAnnualSalary ( )...
You are an employee of University Consultants. Ltd., and have been given the following assignment. You...
You are an employee of University Consultants. Ltd., and have been given the following assignment. You are to present an investment analysis of a new small residential income producing property for sale to a potential investor. The asking price for the property is $1, 250,000; rents are estimated at $200,000 during the first year and are expected to grow at 3 percent per year thereafter. Vacancies and collection losses are expected to be 10 percent of rents. Operating expenses will...
Java program In this assignment you are required to create a text parser in Java/C++. Given...
Java program In this assignment you are required to create a text parser in Java/C++. Given a input text file you need to parse it and answer a set of frequency related questions. Technical Requirement of Solution: You are required to do this ab initio (bare-bones from scratch). This means, your solution cannot use any library methods in Java except the ones listed below (or equivalent library functions in C++). String.split() and other String operations can be used wherever required....
JAVA A simple Class in a file called Account.java is given below. Create two Derived Classes...
JAVA A simple Class in a file called Account.java is given below. Create two Derived Classes Savings and Checking within their respective .java files. (modify display() as needed ) 1. Add New private String name (customer name) for both, add a New int taxID for Savings only. 2. Add equals() METHOD TO CHECK any 2 accounts Demonstrate with AccountDemo.java in which you do the following: 3. Create 1 Savings Account and 3 Checking Accounts, where 2 checkings are the same....
JAVA You will be given a grocery list, filed by a sequence of items that have already been purchased.
 Instructions You will be given a grocery list, filed by a sequence of items that have already been purchased. You are going to determine which items remain on the the list and output them so that you know what to buy. You will be give an integer n that describes how many items are on the original grocery list. Following that, you will be given an array of n grocery list items (strings) that you need to buy. After your grocery list...
in java please: You have been given the job of creating a new order processing system...
in java please: You have been given the job of creating a new order processing system for the Yummy Fruit CompanyTM. The system reads pricing information for the various delicious varieties of fruit stocked by YFC, and then processes invoices from customers, determining the total amount for each invoice based on the type and quantity of fruit for each line item in the invoice. The program input starts with the pricing information. Each fruit price (single quantity) is specified on...
3. Josh has been given two assignments, assignment A and assignment B. He estimates that the...
3. Josh has been given two assignments, assignment A and assignment B. He estimates that the chance of getting a passing grade on assignment A is 60% and the chance of getting a passing grade on assignment B is 50%. Assuming that assignment grades are statistically independent, how likely is it that Josh gets a passing grade on both assignments?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT