82.8k views
5 votes
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.

Required:
Your goal is to enforce information hiding principles in this tasl. 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.

User Chimp
by
8.6k points

1 Answer

3 votes

Answer:

Here is the Coin class:

public class Coin { //class names

private int value; // private member variable of type int of class Coin to store the value

private String coinName; // private member variable of type String of class Coin to store the coint name

private double weight; //private member variable of type double of class Coin to store the weight

public void setValue (int v) { //mutator method to set the value field

value = v; }

public void setName(String n){ //mutator method to set coinName field

coinName = n;}

public void setWeight (double w) { //mutator method to set weight field

weight = w; }

public int getValue () { //accessor method to get the value

return value; } // returns the current value

public String getName () { //accessor method to get the coin name

return coinName; } //returns the current coin name

public double getWeight () { //accessor method to get the weight

return weight; } } //returns the current weight

Step-by-step explanation:

Here is the Main.java

public class Main{ //class name

public static void main(String[] args) { //start of main method

Coin penny = new Coin(); //creates object of Coin class called penny

penny.setName("Penny"); //calls setName method of Coin using object penny to set the coinName to Penny

penny.setValue(1); //calls setValue method of Coin using object penny to set the coin value to 1

penny.setWeight(0.003); //calls setWeight method of Coin using object penny to set the coin weight to 0.003

System.out.println("Coin name: " + penny.getName()); // calls getName method of Coin using penny object to get the current coin name stored in coinName field

System.out.println("Coin value: " + penny.getValue()); // calls getValue method of Coin using penny object to get the coin value stored in value field

System.out.println("Coin weight: " +penny.getWeight()); }} // calls getWeight method of Coin using penny object to get the coin weight stored in weight field

The value of coinName is set to Penny, that of value is set to 1 and that of weight is set to 0.003 using mutator method and then the accessor methods to access these values and prinln() to display these accessed values on output screen. Hence the output of the entire program is:

Coin name: Penny Coin value: 1 Coin weight: 0.003

The screenshot of the program along with its output is attached.

You have been given two classes, a Main.java and a Coin.java. The coin class represents-example-1
User Marcelo Alves
by
7.7k points