In: Computer Science
*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:
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
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
============================================================