Question

Given a class: public class Money { int wholeNumber; int decimalPart; boolean positive; char currencySymbol; }...

Given a class:

public class Money {

int wholeNumber;

int decimalPart;

boolean positive;

char currencySymbol;

}

Write a constructor for setting the 4 instance variables (in the order specified above). The constructor must validate the input throwing an IllegalArgumentException (you can specify any error message you see fit) if any of the following conditions are not met:

  1. either the int values are negative
  2. decimalPart is greater than 99
  3. the currencySymbol is not one of '$', '€', or '£'

Write an accessor method for each of the 4 instance variables. The constructor from the previous question is already declared, you DO NOT need to provide it again.

Write a mutator method for each of the 4 instance variables. The constructor and accessor methods from previous questions are already declared, you DO NOT need to write them again. However, the mutators must still validate input they receive. Throwing an IllegalArgumentException (you can specify any error message you see fit) if any of the following conditions are not met:

  1. either the int values are negative
  2. decimalPart is greater than 99
  3. the currencySymbol is not one of '$', '€', or '£'

Homework Answers

Answer #1

Ok so the required class is written below.

If you got any doubt feel free to drop that in comment section.

Code:

public class Money{
   int wholeNumber;
   int decimalPart;
   boolean positive;
   char currencySymbol;
  
   public Money(int wholeNumber,int decimalPart ,boolean positive ,char currencySymbol) {
      
      
       try {
           if(wholeNumber<0 || decimalPart<0 || decimalPart>99 ||
                   (currencySymbol!='$' && currencySymbol!='€' && currencySymbol!='£')) {
      
           throw new IllegalArgumentException("Invalid Value");
          
       }
           else {
               setCurrencySymbol(currencySymbol);
               setDecimalPart(decimalPart);
               setPositive(positive);
               setWholeNumber(wholeNumber);
           }
       }
       catch (IllegalArgumentException e) {
           System.out.println(e.getMessage());
       }
      
  
   }
  
   //accessors
   public char getCurrencySymbol() {
       return currencySymbol;
   }
   public int getDecimalPart() {
       return decimalPart;
   }
   public int getWholeNumber() {
       return wholeNumber;
   }
   public boolean getPositive() {
       return positive;
   }
  
   //mutators
   public void setCurrencySymbol(char currencySymbol) {
       this.currencySymbol = currencySymbol;
   }
   public void setDecimalPart(int decimalPart) {
       this.decimalPart = decimalPart;
   }
   public void setPositive(boolean positive) {
       this.positive = positive;
   }
   public void setWholeNumber(int wholeNumber) {
       this.wholeNumber = wholeNumber;
   }

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
public class Auto { private String make; private String model; private int year; } a) write...
public class Auto { private String make; private String model; private int year; } a) write a default constructor for the Auto Class n) Write a constructor to initialize all instance variables c) write accessor and mutator for make variable d) create an onject name it myAuto with values of Toyota, Camry, and 2016
Java Programming In this lab students continue to write programs using multiple classes. By the end...
Java Programming In this lab students continue to write programs using multiple classes. By the end of this lab, students should be able to Write classes that use arrays and ArrayLists of objects as instance variables Write methods that process arrays and ArrayLists of objects Write getter and setter methods for instance variables Write methods using object parameters and primitive types Question- class StringSet. A StringSet object is given a series of up to 10 String objects. It stores these...
In C++ Employee Class Write a class named Employee (see definition below), create an array of...
In C++ Employee Class Write a class named Employee (see definition below), create an array of Employee objects, and process the array using three functions. In main create an array of 100 Employee objects using the default constructor. The program will repeatedly execute four menu items selected by the user, in main: 1) in a function, store in the array of Employee objects the user-entered data shown below (but program to allow an unknown number of objects to be stored,...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the 2 in 2 for $1.99. private double groupPrice; //Part of price, like the $1.99 in 2 for $1.99. private int numberBought; //Total number being purchased. public Purchase () { name = "no name"; groupCount = 0; groupPrice = 0; numberBought = 0; } public Purchase (String name, int groupCount, double groupPrice, int numberBought) { this.name = name; this.groupCount = groupCount; this.groupPrice = groupPrice; this.numberBought...
using System; public static class Lab5 { public static void Main() { // declare variables int...
using System; public static class Lab5 { public static void Main() { // declare variables int inpMark; string lastName; char grade = ' '; // enter the student's last name Console.Write("Enter the last name of the student => "); lastName = Console.ReadLine(); // enter (and validate) the mark do { Console.Write("Enter a mark between 0 and 100 => "); inpMark = Convert.ToInt32(Console.ReadLine()); } while (inpMark < 0 || inpMark > 100); // Use the method to convert the mark into...
1. Consider the following interface: interface Duty { public String getDuty(); } a. Write a class...
1. Consider the following interface: interface Duty { public String getDuty(); } a. Write a class called Student which implements Duty. Class Student adds 1 data field, id, and 2 methods, getId and setId, along with a 1-argument constructor. The duty of a Student is to study 40 hours a week. b. Write a class called Professor which implements Duty. Class Professor adds 1 data field, name, and 2 methods, getName and setName, along with a 1-argument constructor. The duty...
My assignment is listed below. I already have the code complete, but I cannot figure out...
My assignment is listed below. I already have the code complete, but I cannot figure out how to complete this portion: You must create a makefile to compile and build your program. I can't figure out if I need to create a makefile, and if I do, what commands do I use for that? Create a  ContactInfo class that contains the following member variables: name age phoneNumber The ContactInfo class should have a default constructor that sets name = "", phoneNumber...
Use Java To(with Explaination of Steps Please): You’re given a class Employee that represents a worker...
Use Java To(with Explaination of Steps Please): You’re given a class Employee that represents a worker in some unknown industry.Each employee has a name , balance and an hourly rate that is unique to them. So Worker1 can have a rate of $5 per hour and Worker2 can have $8 per hour. There are 2 constructors – one that lets you specify the name, a starting balance and the rate,and another that only lets you specify the name and rate...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the following subsections can all go in one big file called pointerpractice.cpp. 1.1     Basics Write a function, int square 1(int∗ p), that takes a pointer to an int and returns the square of the int that it points to. Write a function, void square 2(int∗ p), that takes a pointer to an int and replaces that int (the one pointed to by p) with its...
C++ PROGRAMMING Hi! I have to make a program that adds fractions and simplifies them. I...
C++ PROGRAMMING Hi! I have to make a program that adds fractions and simplifies them. I feel like I know how to write that. What I'm having trouble with is implementing two files the professer gave us. I would appreicate any help in understanding their purpose as in if Im supposed to take information from those files or give it information. Thank you! I have attatched the homework instructions and the two files given. Implementation The main program, called calculator.cpp...