Question

In: Computer Science

Java Programming For this assignment, you should modify only the User.java file. Make sure that the...

Java Programming

For this assignment, you should modify only the User.java file. Make sure that the InsufficientFundsException.java file is in the same folder as your User.java File. To test this program you will need to create your own "main" function which can import the User, create an instance and call its methods. One of the methods will throw an exception in a particular circumstance. This is a good reference for how throwing exceptions works in java. I would encourage you to do more research on exceptions in java for a deeper understanding.

For this assignment, to throw the Exceptions you will use "throw new InsufficientFundsException();"

Follow the TODOs and the documentation in the User.java file to complete this assignment. When ready to test, submit the User.java file to GradeScope. (As always eclipse users ensure that you have no package label at the top of your file)

I would suggest having at least both constructors, and getUserName() completed before submitting to GradeScope for the first time.

********

public class User {
   // Class Attributes
   private String username;
   private double balance;

   /***
   * Default Constructor
   * Should set all values to their defaults
   * Strings should be set to "unknown"
   * balance should start at $0
   */
   public User() {
       // TODO 1 IMPLEMENT THIS METHOD
   }

   /***
   * Constructor
   * the class attributes should be set to the values passed in by the method parameters
   *
   * The remaining attributes should be set to their defaults:
   * balance should start at $0
   *
   * @param: username
   */
   public User(String username) {
       // TODO 2 IMPLEMENT THIS METHOD
   }

   /***
   *
   * @return the username
   */
   public String getUserName() {
       // TODO 3 IMPLEMENT THIS METHOD
       return "";
   }

   /***
   *
   * @return current balance amount as double
   */
   public double getBalance() {
       // TODO 4 IMPLEMENT THIS METHOD
       return 0.0;
   }

   /***
   *
   * @return current balance amount as formatted string with $ and 2 decimal point
   * precision
   */
   public String getFormattedBalance() {
       // TODO 5 IMPLEMENT THIS METHOD
       return "";
   }

   /***
   * This method should add a deposit amount to the balance
   *
   * @param deposit
   */
   public void deposit(double deposit) {
       // TODO 6 IMPLEMENT THIS METHOD
   }

   /***
   * This method should detract a withdrawal from the balance
   *
   * @param withdrawal
   *
   * @throws InsufficientFundsException if the withdrawal will put the current
   * balance below $0
   */
   public void withdrawal(double withdrawal) throws InsufficientFundsException {
       // TODO 7 IMPLEMENT THIS METHOD
   }

   /***
   *
   * Given a rate and a number of months, calculate the projected balance.
   *
   * Example Balance = $100
   * Months = 4
   * Rate = 0.01 (1%)
   *
   * After 1 Month Balance = $101 (100 + (100 * 0.01))
   * After 2 Months Balance = $102.01 (101 + (101 * 0.01))
   * After 3 Months Balance = $103.0301 (102.01 + (102.01 * 0.01))
   * After 4 Months Balance = $104.060401 (103.0301 + (103.0301 * 0.01))
   *
   * return 104.060401
   *
   * @param rate (Will always be positive)
   * @param months (Will always be greater than 1)
   * @return estimated balance after "months" months of cumulative interest at
   * rate "rate"
   */
   public double projectedBalance(double rate, int months) {
       // TODO 8 IMPLEMENT THIS METHOD
       return 0.0;
   }
}

---------------------------------------------------------------------------------------------------------------------

public class InsufficientFundsException extends RuntimeException {
   /**
   *
   */
   private static final long serialVersionUID = 7873326070461717954L;

   public InsufficientFundsException() {
       // TODO Auto-generated constructor stub
       super("Insufficient Funds in Account");
   }

}

Solutions

Expert Solution

Explanation:I have completed all the TODO methods as mentioned in the question,main() function is there and User class is tested by calling all the methods of the user class and also InsufficientFundsException is also thrown,you can see everything in the ouput attached with the answer.I have used DecimalFormatter to format the output upto two decimal places,if you want it can be modified to String.valueOf() also.I have used eclipse to write the code,so while uploading to the GradScore please make sure that no package is mentioned.I have modified my answer please comment if you need any modification.

//code starts

//InsufficientFundsException class

public class InsufficientFundsException extends RuntimeException {
   /**
   *
   */
   private static final long serialVersionUID = 7873326070461717954L;

   public InsufficientFundsException() {
       // TODO Auto-generated constructor stub
       super("Insufficient Funds in Account");
   }

}

//User class

import java.text.DecimalFormat;

public class User {
   // Class Attributes
   private String username;
   private double balance;

   /***
   * Default Constructor Should set all values to their defaults Strings should be
   * set to "unknown" balance should start at $0
   */
   public User() {
       username = "unknown";
       balance = 0;
   }

   /***
   * Constructor the class attributes should be set to the values passed in by the
   * method parameters
   *
   * The remaining attributes should be set to their defaults: balance should
   * start at $0
   *
   * @param: username
   */
   public User(String username) {
       this.username = username;
       balance = 0;
   }

   /***
   *
   * @return the username
   */
   public String getUserName() {

       return username;
   }

   /***
   *
   * @return current balance amount as double
   */
   public double getBalance() {

       return balance;
   }

   /***
   *
   * @return current balance amount as formatted string with $ and 2 decimal point
   * precision
   */
   public String getFormattedBalance() {
       DecimalFormat decimalFormat = new DecimalFormat("##.00");
       return decimalFormat.format(balance);
   }

   /***
   * This method should add a deposit amount to the balance
   *
   * @param deposit
   */
   public void deposit(double deposit) {
       balance += deposit;
   }

   /***
   * This method should detract a withdrawal from the balance
   *
   * @param withdrawal
   *
   * @throws InsufficientFundsException if the withdrawal will put the current
   * balance below $0
   */
   public void withdrawal(double withdrawal) throws InsufficientFundsException {
       if ((balance - withdrawal) < 0)
           throw new InsufficientFundsException();
       else
           balance -= withdrawal;

   }

   /***
   *
   * Given a rate and a number of months, calculate the projected balance.
   *
   * Example Balance = $100 Months = 4 Rate = 0.01 (1%)
   *
   * After 1 Month Balance = $101 (100 + (100 * 0.01)) After 2 Months Balance =
   * $102.01 (101 + (101 * 0.01)) After 3 Months Balance = $103.0301 (102.01 +
   * (102.01 * 0.01)) After 4 Months Balance = $104.060401 (103.0301 + (103.0301 *
   * 0.01))
   *
   * return 104.060401
   *
   * @param rate (Will always be positive)
   * @param months (Will always be greater than 1)
   * @return estimated balance after "months" months of cumulative interest at
   * rate "rate"
   */
   public double projectedBalance(double rate, int months) {
       for (int i = 1; i <= months; i++) {
           balance = balance + ((balance * rate));
       }
       return balance;
   }

   public static void main(String[] args) {
       User user = new User("Chris Martin");
       // display the username
       System.out.println("Name :" + user.getUserName());
       // display the initial balance
       System.out.println("Balance :" + user.getFormattedBalance());
       // deposit money into account
       user.deposit(5000);
       // display the balance
       System.out.println("Balance :" + user.getFormattedBalance());
       // withdraw some amount
       user.withdrawal(4900);
       // display the balance
       System.out.println("Balance :" + user.getFormattedBalance());
       // display the projected balance
       System.out.println("The projected balance :" + user.projectedBalance(0.01, 4));
       // withdraw amount greater than balance
       user.withdrawal(6000);

   }
}

Output:


Related Solutions

Your task is to modify the program from the Java Arrays programming assignment to use text...
Your task is to modify the program from the Java Arrays programming assignment to use text files for input and output. I suggest you save acopy of the original before modifying the software. Your modified program should: contain a for loop to read the five test score into the array from a text data file. You will need to create and save a data file for the program to use. It should have one test score on each line of...
Modify Account Class(Java programming) used in the exercises such that ‘add’ and ‘deduct’ method could only...
Modify Account Class(Java programming) used in the exercises such that ‘add’ and ‘deduct’ method could only run if the account status is active. You can do this adding a Boolean data member ‘active’ which describes account status (active or inactive). A private method isActive should also be added to check ‘active’ data member. This data member is set to be true in the constructor, i.e. the account is active the first time class Account is created. Add another private method...
JAVA make sure file name is FirstnameLastname_02_CS1Calculator. Thank you! Overview This program implements a simple, interactive...
JAVA make sure file name is FirstnameLastname_02_CS1Calculator. Thank you! Overview This program implements a simple, interactive calculator. Major topics writing a program from scratch multiple methods testing In this document program logic & program structure (class, methods, variables) input / output assumptions and limitations documentation & style suggested schedule cover letter discussion questions grading, submission, and due date additional notes challenges sample output test plan Program Logic The program prompts the user to select a type of math problem (addition,...
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a...
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a CPU scheduler. The number of CPU’s and the list of processes and their info will be read from a text file. The output, of your simulator will display the execution of the processes on the different available CPU’s. The simulator should also display: -   The given info of each process -   CPU utilization - The average wait time - Turnaround time for each process...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the bubble sort algorithm. You must be able to sort both integers and doubles, and to do this you must overload a method. Bubble sort work by repeatedly going over the array, and when 2 numbers are found to be out of order, you swap those two numbers. This can be done by looping until there are no more swaps being made, or using a...
Answer the following in Java programming language Create a Java Program that will import a file...
Answer the following in Java programming language Create a Java Program that will import a file of contacts (contacts.txt) into a database (Just their first name and 10-digit phone number). The database should use the following class to represent each entry: public class contact {    String firstName;    String phoneNum; } Furthermore, your program should have two classes. (1) DatabaseDirectory.java:    This class should declare ArrayList as a private datafield to store objects into the database (theDatabase) with the...
CS 1102 Unit 5 – Programming AssignmentIn this assignment, you will again modify your Quiz program...
CS 1102 Unit 5 – Programming AssignmentIn this assignment, you will again modify your Quiz program from the previous assignment. You will create an abstract class called "Question", modify "MultipleChoiceQuestion" to inherit from it, and add a new subclass of "Question" called "TrueFalseQuestion". This assignment will again involve cutting and pasting from existing classes. Because you are learning new features each week, you are retroactively applying those new features. In a typical programming project, you would start with the full...
Modify programming problem 4 from Assignment 2. You will create a number of threads—for example, 100—and...
Modify programming problem 4 from Assignment 2. You will create a number of threads—for example, 100—and each thread will request a pid, sleep for a random period of time, and then release the pid. (Sleeping for a random period of time approximates the typical pid usage in which a pid is assigned to a new process, the process executes and then terminates, and the pid is released on the process's termination.) On UNIX and Linux systems, sleeping is accomplished through...
In this programming assignment, you will write a program that reads in the CSV file (passenger-data-short.csv),...
In this programming assignment, you will write a program that reads in the CSV file (passenger-data-short.csv), which contains passenger counts for February 2019 on 200 international flights. The data set (attached below) is a modified CSV file on all International flight departing from US Airports between January and June 2019 reported by the US Department of Transportation. You write a program that give some summary statistics on this data set. Create a header file named flights.h. In this file, you...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT