CIT-111 Homework #5, Part A Chapter 5, part 1 and alternate part 2 (page 341)
A breakdown of the problem is as follows:
EXTRA CREDIT: (some or all three)
Use the driver program MoneyDemo (download from
Blackboard) to test your Money class methods.
Solution:
CODE ( money.java )
public class Money {
private int dollars, cents;
public Money() {
this.dollars = 0;
this.cents = 0;
}
public Money(int bucks) {
this.dollars = bucks;
this.cents = 0;
}
public Money(int bucks, int pennies) {
this.dollars = bucks;
this.cents = pennies;
}
public int getDollars() {
return dollars;
}
public void setDollars(int bucks) {
if(bucks < 0) {
this.dollars =
0;
}
else
this.dollars =
bucks;
}
public int getCents() {
return cents;
}
public void setCents(int pennies) {
if (pennies < 0) {
this.cents =
0;
}
else
this.cents =
pennies;
}
public void add(Money pmt1) {
this.dollars += pmt1.cents / 100 +
pmt1.dollars;
this.cents += pmt1.cents %
100;
}
static Money add( Money pmt1, Money pmt2 ) {
int dollars, cents,
total_cents;
total_cents = pmt1.cents +
pmt2.cents;
dollars = pmt1.dollars +
pmt2.dollars + total_cents / 100;
cents = total_cents % 100;
return new Money(dollars,
cents);
}
public void minus(Money pmt1) {
if (pmt1.cents > this.cents)
{
this.cents +=
100 - pmt1.cents;
this.dollars +=
pmt1.dollars - 1;
}
else {
this.cents -=
pmt1.cents;
this.dollars +=
pmt1.dollars;
}
}
static Money minus(Money pmt1, Money pmt2) {
int dollars, cents;
if (pmt1.dollars > pmt2.dollars)
{
if(pmt1.cents
> pmt2.cents) {
cents = pmt1.cents - pmt2.cents;
dollars = pmt1.dollars - pmt2.dollars;
}
else {
cents = pmt2.cents - pmt1.cents;
dollars = pmt1.dollars - pmt2.dollars - 1;
}
}
else {
if(pmt2.cents
> pmt1.cents) {
cents = pmt2.cents - pmt1.cents;
dollars = pmt2.dollars - pmt1.dollars;
}
else {
cents = pmt1.cents - pmt2.cents;
dollars = pmt2.dollars - pmt1.dollars - 1;
}
}
if(dollars < 0) dollars =
0;
return new Money(dollars,
cents);
}
public boolean equals(Money other) {
if (cents != other.cents)
return
false;
if (dollars != other.dollars)
return
false;
return true;
}
@Override
public String toString() {
return "Money [$" + dollars + "." +
cents + "]";
}
}
Sample tester code :
( MoneyTester.java )
import Money;
public class MoneyTester {
public static void main(String[] args) {
Money money1 = new Money(20,
2);
Money money2 = new Money(32,
87);
Money money3 = new Money(32,
87);
System.out.println("Money 1 : " +
money1);
System.out.println("Money 2 : " +
money2);
System.out.println("Sum of money1
and money2 is " + Money.add(money1, money2));
System.out.println("Differnce of
money1 and money2 is : " + Money.minus(money1, money2));
System.out.println("Money1 and
Money2 are equal : " + money1.equals(money2));
System.out.println("Money2 and
Money3 are equal : " + money2.equals(money3));
}
}
Sample Output ( Sample Run ) :
CIT-111 Homework #4, Part A Chapter 4, problem #7 (pages 254-255) A breakdown of the problem is as follows: Class name: Temperature Instance variables: temp (double) and tempScale (char; either ‘C’ or ‘F’) Constructor methods: Temperature() – sets temp to 0 Celsius Temperature(double initialTemp) – sets temp Temperature(char tempType) – sets tempScale to ‘C’ or ‘F’) Temperature(double initialTemp, char tempType) -- sets temp and tempScale to ‘C’ or ‘F’) Accessor methods: getTemp – returns value of...
implement a class called PiggyBank that will be used to represent a collection of coins. Functionality will be added to the class so that coins can be added to the bank and output operations can be performed. A driver program is provided to test the methods. class PiggyBank The PiggyBank class definition and symbolic constants should be placed at the top of the driver program source code file while the method implementation should be done after the closing curly brace...
Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will have four instance variables: • An instance variable called “year” which will be of type int • An instance variable called “month” which will be of type int • An instance variable called “day” which will be of type int • An instance variable called “description” which will be of type String The “Appointment” class must also implement the following methods: • A getter...
Create this c++ program. Create to classes and name their type Account and Money. Follow the instruction listed below. Please read and implement instructions very carefully Money Class Requirements - Negative amounts of Money shall be stored by making both the dollars and cents negative The Money class shall have 4 constructors * A default constructor that initializes your Money object to $0.00 A constructor that takes two integers, the first for the dollars and the second for the cents...
In this practical task, you need to implement a class called MyTime, which models a time instance. The class must contain three private instance variables: hour, with the domain of values between 0 to 23. minute, with the domain of values between 0 to 59. second, with the domain of values between 0 to 59. For the three variables you are required to perform input validation. The class must provide the following public methods to a user: MyTime() Constructor. Initializes...
This needs to be done in Java. Please use basic Java coding at an intro level. THANK YOU FOR YOUR HELP! I WILL LEAVE A GOOD RATING! Design and implement MoneyTest and Money. Use the test-first approach, as we discussed, and document each method before implementing. Money - dollars : int - cents : int + Money (int, int) + getDollars() + getCents() + add (Money) : Money + subtract (Money) : Money + toString() : String + equals (Money)...
Write a Java class that implements the concept of Coins, assuming the following attributes (variables): number of quarters, number of dimes, number of nickels, and number of pennies. Include two constructors (non-argument constructor that assigns 1 to each coin, and another constructor that takes necessary arguments to create an object), needed getter and setter methods, method toString (to print coins objects in a meaningful way), and method equals (to compare two coins objects based on number of coins). Also include...
Need the answers to these RECURSIVE practice problems using Linked Lists in JAVA . Thank you. Complete the Link class by writing methods described below. Do not use loops, or create any more methods (other than those specified), class or instance variables. public class Link private Link next; /null if this is the last link private int value; public Link(Link n, int v) nextn valuev; Do not use loops, or create any more methods (other than those specified), class or...
Java Program Please help me with this. It should be pretty basic and easy but I am struggling with it. Thank you Create a superclass called VacationInstance Variables destination - String budget - double Constructors - default and parameterized to set all instance variables Access and mutator methods budgetBalance method - returns the amount the vacation is under or over budget. Under budget is a positive number and over budget is a negative number. This method will be overwritten in...
Exercise 1 Adjacency Matrix In this part, you will implement the data model to represent a graph. Implement the following classes Node.java: This class represents a vertex in the graph. It has only a single instance variable of type int which is set in the constructor. Implement hashCode() and equals(..) methods which are both based on the number instance variable Node - int number +Node(int number); +int getNumberO; +int hashCode() +boolean equals(Object o) +String toString0) Edge.java: This class represents a...