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 programming language Modify the following code. Make changes so that Movies can be sorted by...
JAVA programming language Modify the following code. Make changes so that Movies can be sorted by title ----------------------------------------------------------------- package Model; import java.time.Duration; import java.time.LocalDate; import java.util.ArrayList; import java.util.List; // TODO - Modify the movie class so that Java knows how to compare two Movies for sorting by title public class Movie extends DataStoreObj { private String title; private String description; private LocalDate releaseDate; private Duration runningTime; private Rating rating; private List<Genre> genres = new ArrayList<>(); private List<Actor> actors = new...
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,...
*****************PLEASE PROVIDE THE CODE IN RACKET PROGRAMMING LANGUAGE ONLY AND MAKE SURE CODE RUNS ON THE...
*****************PLEASE PROVIDE THE CODE IN RACKET PROGRAMMING LANGUAGE ONLY AND MAKE SURE CODE RUNS ON THE WESCHEME IDE************* Write a tail-recursive Racket function "kept-short-rec" that takes an integer and a list of strings as parameters and evaluates to an integer. The resulting integer should be the number of strings on the original list whose string length is less than the integer parameter. For example, (kept-short-rec 3 '("abc" "ab" "a")) should evaluate to 2 because there are only 2 strings shorter...
*****************PLEASE GIVE THE CODE IN RACKET PROGRAMMING ONLY AND MAKE SURE THE CODE RUNS ON WESCHEME...
*****************PLEASE GIVE THE CODE IN RACKET PROGRAMMING ONLY AND MAKE SURE THE CODE RUNS ON WESCHEME IDE*************** Write a recursive Racket function "keep-short-rec" that takes an integer and a list of strings as parameters and evaluates to a list of strings. The resulting list should be all of the strings from the original list, maintaining their relative order, whose string length is less than the integer parameter. For example, (keep-short-rec 3 '("abc" "ab" "a")) should evaluate to '("ab" "a") because...
*****************PLEASE GIVE THE CODE IN RACKET PROGRAMMING ONLY AND MAKE SURE THE CODE RUNS ON WESCHEME...
*****************PLEASE GIVE THE CODE IN RACKET PROGRAMMING ONLY AND MAKE SURE THE CODE RUNS ON WESCHEME IDE*************** Write a recursive Racket function "sum-diff" that takes two lists of integers that are the same length and evaluates to an integer. The resulting integer should be the sum of the absolute value of the differences between each pair of integers with the same index in the two lists. For example (sum-diff '(-1 -2 -3) '(1 2 3)) should evaluate to 12 because...
3. For this week’s assignment, you are asked to modify the above Java program to include...
3. For this week’s assignment, you are asked to modify the above Java program to include the following: - When the user clicks on the help menu, a drop-down list will appear with an item called About, then when the user clicks on it, the window will show some instructions about the functionality of the menu, e.g, what the edit menu does, etc. - When the user clicks on the File menu, a drop-down list will appear with one item...
In this assignment, you will create a Java program to search recursively for a file in...
In this assignment, you will create a Java program to search recursively for a file in a directory. • The program must take two command line parameters. First parameter is the folder to search for. The second parameter is the filename to look for, which may only be a partial name. • If incorrect number of parameters are given, your program should print an error message and show the correct format. • Your program must search recursively in the given...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT