Can you please tell me if this code needs to be fixed, if it does can you please post the fixed code below please and thank you
/**
* Models a Whole Life Policy.
*
* @author Tina Comston
* @version Fall 2019
*/
public class WholeLifePolicy
{
// instance variables
private double faceValue;
// your code here - code the remaining instance field
// constants
/**
* surrender rate.
*/
public static final double SURRENDER_RATE = .65;
/**
* ADMIN FEE.
*/
public static final double ADMIN_FEE = .005;
// constructors and methods
/**
* Default constructor for objects of class WholeLifePolicy.
* This is the default constructor.
*
* Default values are:
* face value = 0.0
* policy years = 0
* policy number WLP9999999
*
*/
public WholeLifePolicy()
{
this.setPolicyNum("WLP9999999");
this.setFaceValue(0.0);
this.setPolicyYrs(0);
}
/**
* Explicit constructor for objects of class WholeLifePolicy.
*
* @param inPolicyNum Number of the policy
* @param inFaceValue Face Value of policy.
* @param inPolicyYrs Length of policy in years.
*
*/
public WholeLifePolicy(String inPolicyNum, double
inFaceValue,
int inPolicyYrs)
{
// your code here, complete the constructor
}
/**
* Copy constructor for objects of class WholeLifePolicy.
* This constructor creates an exact copy of a
* WholeLifePolicy object.
*
* @param inPolicyObject WholeLifePolicy object.
*
*/
public WholeLifePolicy(WholeLifePolicy inPolicyObject)
{
this.setPolicyNum(inPolicyObject.getPolicyNum());
// your code here, complete the constructor
}
/**
* Set the Policy Face Value.
*
* @param inFaceValue Face Value of policy.
*
*/
public void setFaceValue(double inFaceValue)
{
this.faceValue = faceValue;
}
/**
* Get the Policy Face Value.
*
* @return double Face Value of policy.
*
*/
public double getFaceValue()
{
return this.faceValue;
}
/**
* Set the Policy Years.
*
* @param inPolicyYrs Length of policy in years.
*
*/
public void setPolicyYrs(int inPolicyYrs)
{
// your code here, complete the method
}
/**
* Get the Policy Years.
*
* @return int Length of policy in years.
*
*/
public int getPolicyYrs()
{
// your code here, replace the following statement
return 9999;
}
/**
* Set the Policy Number. Use to override the default
* policy number or for copy object processing.
*
* @param inPolicyNum Policy Number.
*
*/
public void setPolicyNum(String inPolicyNum)
{
// your code here, complete the method
}
/**
* Get the Policy Number.
*
* @return String Policy Number.
*
*/
public String getPolicyNum()
{
// your code here, replace the following statement
return "incorrect value";
}
/**
* Calculate surrender value.
* Surrender value is calculated as:
* (surrender rate * face value) *
* (years held / policy years) - amount borrowed -
* (face value * administrative fee)
*
* @param inYrsHeld Length in years policy held to date.
* @param inBorAmt Amount borrowed against policy if any.
*
* @return double Policy surrender value.
*
*/
public double surrenderVal(double inYrsHeld, double inBorAmt)
{
// Termination value is the surrender rate percentage of the
face
// value as a proportion of the years held to policy years less
any
// amount borrowed on the policy less a ADMINFEE on the
// face value.
// your code here, compute the surrender value and replace
the
// following line
return 99999.99;
}
/**
* Return a string representation of the WholeLifePolicy.
*
* @return String output string.
*
* <pre>
* Produce output in the following format:
*
* Policy Information:
* Policy #: WLP1000000
* Policy Years: 20
* Face Value: 50000.0
*
* </pre>
*/
public String toString()
{
String output = "Policy Information:\n";
output = output + "Policy #: " + this.getPolicyNum() + "\n";
// your code here, finish the output string
return output;
}
}
In: Computer Science
The Fibonacci series of numbers is a favorite of mathematicians, and goes as follows: 1, 1, 2, 3, 5, 8, 13, 21, . . . That is, the first two numbers are 1, and every subsequent number is the sum of the previous two. The mathematical definition as a recursive function is the following.
∀n = 0, 1, 2, . . .
F(n) = { 1 if n <=1
F(n-1) + F(n-2) if n > 1
The closed form of such function is in O(φ n ), where φ is a number called the golden ratio.
1. Coding: Write a Java class Fibonacci that has the following static methods.
(a) (20 points) A recursive method that returns F(n).
(b) (20 points) A non-recursive method that returns F(n).
(c) (10 points) A main method to obtain from the user the index n, and call the methods above one after the other. For each of the method calls, measure and display the running time using the following snippet of code.
...
// store the time now
long startTime = System.nanoTime();
// your method call here
...
// display the time elapsed
System.out.println("t = " +
(System.nanoTime() - startTime) + " nanosecs.");
2. Experiments: (10 points) Measure running times for different values of n and fill a table like the following. (You may need to adjust n to smaller values if it takes too long or your stack overflows, 0 points if you do not have at least 5 columns of measurements.)
n | 0 | 1 | 2 | 4 | 6 | 8 | 16
recursive | | | | | | |
non-recursive | | | | | | |
3. Analysis: For your recursive method:
(a) (10 points) What is the worst-case big-O running time with respect to n? (Explain how you computed the running time, 0 points if you do not say what is the basic operation counted and what counting rules you used.)
(b) (10 points) Are your measurements consistent with this big-O running time? (Explain using your measurements, 0 points if you do not say something like "looking at row recursive, when n is doubled the running time is xxx".)
For your non-recursive method:
(c) (10 points) What is the worst-case big-O running time with respect to n? (Explain how you computed the running time, 0 points if you do not say what is the basic operation counted and what counting rules you used.)
(d) 10 points Are your measurements consistent with this big-O running time? (Explain using your measurements, 0 points if you do not say something like "looking at row non-recursive, when n is doubled the running time is xxx".)
In: Computer Science
Hello, trying to create a three way currency conversion on python. One of the things I want to do is create an error message whenever there is an incorrect currency entered. So far, I have managed to display the error message in these two cases: when both From and To currencies are incorrect, and when only the From currency is incorrect. What can I do so that my program can also display a message when the To currency is only incorrect.
money = float(input("Please enter the From amount to be
converted: "))
fromCurrency = input("Please enter the From currency (USD, EUR, or
JYP): ")
toCurrency = input("Please enter the To currency (USD, EUR, or
JYP): ")
## conversions taken as of 9/26/19
# 1 USD = .91 EUR (1.1 factor), 1 USD = 107.63 JYP (.00929 factor),
1 EUR = 117.81 JYP (.0084 factor)
usdEur = money / 1.1
eurUsd = money * 1.1
usdJyp = money / .00929
jypUsd = money * .00929
eurJyp = money / .00848
jypEur = money * .00848
# Conversion from USD to other currencies
if fromCurrency.lower() == ("usd"):
if toCurrency.lower() == ("eur"):
result = usdEur
if toCurrency.lower() == ("jyp"):
result = usdJyp
print(money, fromCurrency, "equals", result, toCurrency)
elif fromCurrency.lower() == ("eur"):
if toCurrency.lower() == ("usd"):
result = eurUsd
if toCurrency.lower() == ("jyp"):
result = eurJyp
print(money, fromCurrency, "equals", result, toCurrency)
elif fromCurrency.lower() == ("jyp"):
if toCurrency.lower() == ("usd"):
result = jypUsd
if toCurrency.lower() == ("eur"):
result = jypEur
print(money, fromCurrency, "equals", result, toCurrency)
else: #does not work when 'to' currency is wrong
print("Error! Currency not available for conversion")
In: Computer Science
In java. Using a linked list only, iterator and scanner.
Develop a program to maintain a Linked List of homework assignments name and due date. When an assignment is assigned, add it to the list, and when it is completed, remove it. You should keep track of the due date. Your program should provide the following services each contained within its own method:
In: Computer Science
For this lab, you will design classes to represent three simple geometric shapes: circles, rectangles, and triangles.
Implement the classes for these three shapes in Java, each in a separate class file, according to the guidelines given below:
The classes must include instance fields (also known as attributes or data members) for the information necessary to represent objects of these classes. Recall that the instance fields represent the properties of objects. The most fundamental property of a circle is its radius, so this would be an ideal candidate for an instance field. Likewise, rectangles have a length and width, and a triangle has a base and a height (we will assume that these are all isosceles triangles). These fields must all be floating-point values, so you should use double as the data type for these fields.
The classes should include constructors, which allow us to initialize new objects of these classes with sensible starting values. The constructors should allow us to set the values of every instance field in each class.
The classes should include accessors (also known as "getter methods") for all of the instance fields.
The classes should include a method to compute and return the area of each of the three types of shapes. The area() method does not need to accept an argument, since the object already "knows" (through the value(s) of its instance field(s)) all the information it needs to compute its own area.
The classes should also include a toString() method which returns the string representation of the object, in the form of its area surrounded by symbols which indicate the shape of the object. For example, printing each of the three types of shape objects to the screen should look like this (assume the area is 4 units for each of these shapes):
/ 4.0 \ (for triangles)
[ 4.0 ] (for rectangles)
( 4.0 ) (for circles)
Finally, write a main() method in a fourth class which does the following ...
Create at least two shape objects of each class, initialized to different sizes ...
Print a string representation of every shape object, and ...
Call the area() method of every shape object to
get its area, and then compute the total area of all
objects of the same kind (that is, the total area of all
rectangles, the total area of all circles, and the total area of
all triangles). It should then print these totals along with a
descriptive label; for example ...
Total Triangle Area = 8.0
(These shape objects can be declared as individual variables; later, we will see how to unify these objects into a collection under a common type.)
this is java language
In: Computer Science
Complete the project functions for this week. Note that you have two weeks to complete these functions, however you should aim to get most of them done this week so you can focus on your own additions to the project next week. These functions are setup_game(), play_game() and computer_move(). To complete these functions, first remove the library function call from the function in the template (save_game_lib, etc), then replace that with your own code to complete the function. You can test your functions separately from the project by creating a small main() function that calls them (with appropriate inputs), or you can compile the entire project and see if it works.
setup_game(): The setup_game function takes a Game pointer as input, and initialises this structure so that it is ready to play a new game. There are three main tasks that this function needs to achieve. Firstly, it needs to determine whether player 1 and player 2 are human or
computer players. To do this, a prompt should be displayed to the user and they can enter 'h' for human or 'c' for computer for both players. The results of this input should set the "player1" and "player2" fields of the game structure to either 0 for a computer player or 1 for a human player. Next, your function needs to initialise the board to be empty, by filling every square with zeros. Finally, the turn and winner fields of the structure should be set. winner should be set to 0 (since nobody has won yet), and turn should be a randomly chosen as either 1 or 2.
play_game(): This function performs all the actions necessary to play the game. Most of the functionality of the game has already been implemented by other functions, so the main task of the play_game functions is to call these in the correct way. It takes a pointer to a Game as input, and this structure is assumed to be already initialised to either a new game or a game in progress. The function needs to then run the game until either somebody has won, or the board is full (a draw). To do this, on each turn the game needs to display the board, then either (a) get a move from the keyboard for a human player, or (b) determine a computer move for a computer player, then add this move to the board. The turn and winner fields should then be updated to reflect the new state of the game. If a human player enters 's' or 'q' (as indicated by the return value of get_move) then the move isn't added and the turn field is not changed, but instead the game is saved or quit as appropriate. Your play_game function should use the existing functions (get_move, add_move, display_board, board_full, winner, save_game, etc) to perform all the relevant tasks. You can also use the wait_seconds() function to delay the game for a few seconds before the computer moves, in order to stop this happening instantly.
computer_move(): This function is the "artificial intelligence" of the game. It takes the current board array, the player whose turn it is, and the 'level' of the AI as input, and returns a column number (between 1 and COLS) in which the computer will play. This is very similar to the get_move() function, except that rather than asking the player for a move, one is generated by the AI. The 'level' variable determines how 'smart' the AI is. Level 0 should just generate a random column (that isn't full) and return that. Level 1 should look if the computer can win the game in any column, and if so choose that column. If not, a random valid non-full column is chosen. For level 2, the computer should also check if the opponent can win in any column, and choose that column to block them. Level 3 and above do not need to be implemented for this version, so the behaviour should be the same as level 2.
HINT: Create a copy of the board, so that you can 'test' each move in turn (using add_move and winner) to see if it wins, and choose that column if so. If not, you can reset the copy back to the original board and try again.
TEMPLATE FILE:
#include #include #include #include #include #include "connect4.h" int main ( void ){ int option ; Game g ; // intitialise random seed srand(time(NULL)); while (( option = main_menu()) != -1 ){ if ( option == 1 ){ // setup a new game setup_game ( &g ) ; // now play this game play_game ( &g ) ; } else if ( option == 2 ){ // attempt to load the game from the save file if ( load_game ( &g, "game.txt" ) == 0 ){ // if the load went well, resume this game play_game ( &g ) ; } else { printf ( "Loading game failed.\n") ; } } else if ( option == -1 ){ printf ( "Exiting game, goodbye!\n") ; } } }
// WEEK 4-5 TASKS
// setup_game()
// play_game()
// computer_move()
// calcualtes a column for the computer to move to, using
artificial "intelligence"
// The 'level' argument describes how good the computer is, with
higher numbers indicating better play
// 0 indicates very stupid (random) play, 1 is a bit smarter, 2
smarter still, etc..
int computer_move ( int board[COLS][ROWS], int colour, int level
){
// If level 0, this is a 'dumb' opponent, pick a random column that isn't full and return that
// If level 1, this is slightly smarter - if the computer's move can win the game, choose that column, otherwise random
// If level 2, slightly smarter again. If computer can win it will do that, otherwise it will block an opponent's winning move, otherwise random
// Higher levels are up to you!
// Hint - you can copy the board into a 'temporary' board and
trial putting a token in a column, then simply call the winner
function to see
// if that move has won the game. Doing this for the current player
will check if that player can win, doing it for the opponent will
see if the opponent can win next turn
// You can use "lookahead" logic to search for the 'best' move many moves ahead
return computer_move_lib ( board, colour, level ) ;
}
// sets up the game to a new state
// prompts the user if each player should be a human or computer,
and initialises the relevant fields
// of the game structure accordingly
int setup_game ( Game *g ){
// prompt the user to enter whether each player is a human or
computer (h or c)
// set the player1 and player2 fields of the struct accordingly (1
for human, 0 for computer)
// initialise the board to all zeros
// set winner to 0 ( no winner yet!)
// set turn to either 1 or 2 (randomly)
return setup_game_lib ( g ) ;
}
// Starts or resumes playing the Game g. Continues until the game
is over or the user quits.
int play_game ( Game *g ){
return play_game_lib ( g ) ;
}
#ifndef CONNECT4_H #define CONNECT4_H 1 #define ROWS 6 // height of the board #define COLS 7 // width of the board (values of 9 are going to display poorly!!) // These lines detect what sort of compiler you are using. This is used to handle the time delay // function wait() in various operating systems. Most OS will use sleep(), however for windows it is // Sleep() instead. #ifdef _WIN32 #include <windows.h> #else #include <unistd.h> #endif typedef struct { int player1, player2 ; // variables for each player - 1 for human, 0 for computer player int board[COLS][ROWS] ; // the game board. 0 for empty space, 1 for player 1, 2 for player 2 // Note that row 0 is the TOP row of the board, not the bottom! // column 0 is on the left of the board int turn ; // whose turn it is, 1 or 2 int winner ; // who has won - 0 for nobody, 1 for player 1, 2 for player 2 } Game ; // displays the welcome screen and main menu of the game, and prompts the user to enter an option until // a valid option is entered. // Returns 1 for new game, 2 for load game, -1 for quit int main_menu ( void ) ; // displays the board to the screen int display_board ( int[COLS][ROWS] ) ; // sets up the game to a new state // prompts the user if each player should be a human or computer, and initialises the relevant fields // of the game structure accordingly int setup_game ( Game *g ) ; // Returns TRUE if the specified column in the board is completely full // FALSE otherwise // col should be between 1 and COLS int column_full ( int[COLS][ROWS], int col ) ; // plays a game until it is over int play_game ( Game *g ) ; // prompts the user to enter a move, and checks that it is valid // for the supplied board and board size // Returns the column that the user has entered, once it is valid (1-COLS) // note that this value is betweeen 1 and COLS (7), NOT between 0 and 6!! // If the user enters 'S' or 's' the value -1 should be returned, indicating that the game should be saved // If the user enters 'Q' or 'q' the value -2 should be returned, indicating that the game should be abandoned int get_move ( int[COLS][ROWS] ) ; // calcualtes a column for the computer to move to, using artificial "intelligence" // The 'level' argument describes how good the computer is, with higher numbers indicating better play // 0 indicates very stupid (random) play, 1 is a bit smarter, 2 smarter still, etc.. int computer_move ( int[COLS][ROWS], int colour, int level ) ; // adds a token of the given value (1 or 2) to the board at the // given column (col between 1 and COLS inclusive) // Returns 0 if successful, -1 otherwise int add_move ( int b[COLS][ROWS], int col, int colour ) ; // determines who (if anybody) has won. Returns the player id of the // winner, otherwise 0 int winner ( int[COLS][ROWS] ) ; // determines if the board is completely full or not int board_full ( int[COLS][ROWS] ) ; // saves the game to the specified file. The file is text, with the following format // player1 player2 turn winner // board matrix, each row on a separate line // Example: // //1 0 1 0 player 1 human, player 2 computer, player 1's turn, nobody has won //0 0 0 0 0 0 0 board state - 1 for player 1's moves, 2 for player 2's moves, 0 for empty squares //0 0 0 0 0 0 0 //0 0 0 2 0 0 0 //0 0 0 2 0 0 0 //0 2 1 1 1 0 0 //0 2 2 1 1 2 1 int save_game ( Game g, char filename[] ) ; // loads a saved game into the supplied Game structure. Returns 0 if successfully loaded, -1 otherwise. int load_game ( Game *g, char filename[] ) ; // waits for s seconds - platform specific! THIS FUNCTION IS INCLUDED IN THE LIBRARY, NO NEED TO WRITE IT! void wait_seconds ( int s ) ; // library versions of functions. Exactly the same behaviour done by course staff. Please just call these if you have not completed your version as yet. int display_board_lib ( int[COLS][ROWS] ) ; int setup_game_lib ( Game *g ) ; int column_full_lib ( int[COLS][ROWS], int col ) ; int play_game_lib ( Game *g ) ; int get_move_lib ( int[COLS][ROWS] ) ; int add_move_lib ( int b[COLS][ROWS], int col, int colour ) ; int winner_lib ( int[COLS][ROWS] ) ; int board_full_lib ( int[COLS][ROWS] ) ; int computer_move_lib ( int[COLS][ROWS], int colour, int level ) ; int save_game_lib ( Game g, char filename[] ) ; int load_game_lib ( Game *g, char filename[] ) ; int main_menu_lib ( void ) ;
In: Computer Science
What would an example of java code look like?
Producer class
- Declaring toolsPerHour as an instance variable
- Creating constructor that takes toolsPerHour
- Creating getter for toolsPerHour
- Method for calculating days produced
Tester class
- Creating 3 producer objects using constructor
- Setting toolsPerHour
- Calling methods
- Displaying output
In: Computer Science
Provide a specific example of how each layer of the OSI model processes the movement of data in communicating from one entity to another. For example, discuss how email is processed for each layer of the model.
In: Computer Science
I need know how to Write a java program called character length that prompts the user to enter a string and displays:
The length of the string is: xxxxx
The strings first character is: x
(where xxxxx is the length of the string and x is the first character.)
In: Computer Science
Explain whether this statement is true or false. If false, explain the correct operation. “an LCD segment is given 1 to turn it on and 0 to turn it off, just like the colored LEDs”. Explain whether it’
In: Computer Science
Write a program named Triangle.java that asks for the lengths of the three sides of a triangle and computes the perimeter if the input is valid. The input is valid if the sum of every pair of two sides is greater than the remaining side. For example, the lengths 3, 4, and 5 define a valid triangle: 3 plus 4 is greater than 5; 4 plus 5 is greater than 3, and 5 plus 3 is greater than 4. However, the lengths 7, 2, and 4 do not specify a valid triangle because 2 plus 4 is not greater than 7.
Here is the output from running the program twice. Your program’s output does not have to look exactly like this, but it must convey the same information. Use input is shown in bold.
Enter lengths of sides of the triangle: 3 4 5 The perimeter of the triangle is 12.0 Enter lengths of sides of the triangle: 7 2 4 Those sides do not specify a valid triangle.
In: Computer Science
Write one Java program and satisfy the following requirements:
if (letter == 'A' | | letter == 'a')
System.out.println("Excellent");
else if (letter == 'B' | | letter == 'b')
System.out.println("You can do better");
else if (letter == 'C' | | letter == 'c')
System.out.println("Try harder");
else if (letter == 'D' | | letter == 'd')
System.out.println("Try much harder");
else
System.out.println("Try another major! ");
3. Given the following tax table information, write Java code to assign the double taxRate appropriately given the double pay. Select the selection statement (if, if-else, or switch) that makes the most sense.
If pay is more than 100,000, tax rate is 40%
If pay is more than 60,000 and less than or equal to 100,000, tax rate is 30%
If pay is more than 30,000 and less than or equal to 60,000, tax rate is 20%
If pay is more than 15,000 and less than or equal to 30,000, tax rate is 10%
If pay is more than 5,000 and less than or equal to 15,000, tax rate is 5%
If pay is less than or equal to 5,000, tax rate is 0%
In: Computer Science
A shipping company uses the following function to calculate the cost (in dollars) of shipping based on the weight of the package in pounds:
Weight | Cost |
---|---|
0 < w <= 1 | 3.5 |
1 < w <= 3 | 5.5 |
3 < w <= 10 | 8.5 |
10 < w <= 20 | 10.5 |
Your program, named Shipping.java should ask for the weight of the package and display the shipping cost. If the weight is negative or zero, display a message “Invalid input.” If the weight is greater than 20, display a message “The package cannot be shipped.”
Here is an example of the output of the program. Your output does not have to look exactly like this, but it must reflect the same information. User input is shown in bold.
Enter weight of package in pounds: 5.7 Cost: $8.50 Enter weight of package in pounds: 25.2 The package cannot be shipped.
What I did:
import java.util.Scanner;
public class Shipping
{
public static void main(String[]args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter weight of package in pounds"+" ");
double weight = input.nextDouble();
double cost;
if(weight<=1&&weight>0)
{
cost=3.5;
System.out.println("Cost:"+" "+"$"+cost);
}
else if(weight<=3&&weight>1)
{
cost=5.5;
System.out.println("Cost:"+" "+"$"+cost);
}
else if(weight<=10&&weight>3)
{
cost =8.5;
System.out.println("Cost:"+" "+"$"+cost);
}
else if(weight<=20&&weight>10)
{
cost=10.5;
System.out.println("Cost:"+" "+"$"+cost);
}
else if(weight==0||weight<0)
{
System.out.println( "Invalid input.");
}
else
{
System.out.println( "The package cannot be shipped.");
}
}
}
Is this correct?
In: Computer Science
Question 11 (1 point)
What is wrong with the following recursive method, which is meant to compute the sum of all numbers from 1 to n? public int summation(int n) { return n + summation(n-1);}
Question 11 options:
The method lacks a base case |
|
The method lacks a recursive step |
|
The base case is incorrect |
|
The recursive step is incorrect |
Question 12 (1 point)
In which of these situations does it make sense to consider a recursive solution?
Question 12 options:
The problem involves arithmetic such as addition, multiplication, etc. |
|
The problem is really confusing |
|
The problem involves something that must be done repeatedly |
|
The problem can be divided up into smaller, but identical, sub-problems |
Question 13 (1 point)
An incorrect recursive algorithm sometimes results in a StackOverflowError. Which of the following statements is true about this type of error?
Question 13 options:
The recursive step may be incorrect |
|
The base case may be missing |
|
This is analogous to an infinite loop for an iterative solution |
|
All of these |
|
None of these |
Question 14 (1 point)
The following two method signatures are equivalent.
public getItem(E[] items)
public getItem(T[] items)
Question 14 options:
True | |
False |
Question 15 (1 point)
You are looking through the documentation for a software library you want to use in your application and see this method. What types of objects can be passed to it as an argument? Choose all valid options.
public int doSomething(E arg) { ... }
Question 15 options:
instances of Double |
|
instance of Number |
|
instances of a class called E |
|
instances of Object |
Question 16 (1 point)
Based on this method signature, what types of objects can be passed to it as an argument? Choose all valid options.
public <E extends Comparable<E>> int doWork(E arg) { }
Question 16 options:
instances of Double |
|
instance of Number |
|
instances of a class called E |
|
instances of Object |
In: Computer Science
Using Java
Write a simple calculator which can add, subtract, multiply, and divide. Here are the specifications for the calculator:
The calculator will need to trap the following exception conditions:
The NumberFormatException is already provided in Java. You will write your own UnknownOperatorException and DivideByZeroException classes.
All exception classes should have at least two constructors:
Provide listings for your Calculator class (or classes) and your exception class. Also, provide a PrintScreen(s) demonstrating each of the six operators and three exceptions in use.
Here’s a sample dialog that a user might have with the calculator:
Your calculator is now on. The result is currently 0.0 Enter an operator (+, -, *, or /) and a number, "R" to reset, or "P" to turn off power + 5 The result of 0.0 + 5.0 is 5.0 Enter an operator (+, -, *, or /) and a number, "R" to reset, or "P" to turn off power / 2 The result of 5.0 / 2.0 is 2.5 Enter an operator (+, -, *, or /) and a number, "R" to reset, or "P" to turn off power / 0 You cannot divide by 0. The result is still 2.5. Enter an operator (+, -, *, or /) and a number, "R" to reset, or "P" to turn off power R The calculator has been reset to 0. Enter an operator (+, -, *, or /) and a number, "R" to reset, or "P" to turn off power – 18.4 The result of 0.0 - 18.4 is -18.4 Enter an operator (+, -, *, or /) and a number, "R" to reset, or "P" to turn off power * 20 The result of -18.4 * 20.0 is -368.0 Enter an operator (+, -, *, or /) and a number, "R" to reset, or "P" to turn off power + abc "abc" is not a valid number. The result is still -360.0 Enter an operator (+, -, *, or /) and a number, "R" to reset, or "P" to turn off power & 15 "&" is not a valid operator. The result is still -368.0 Enter an operator (+, -, *, or /) and a number, "R" to reset, or "P" to turn off power P Good bye. |
In: Computer Science