Unit 6 Chapter 15 Assignment Comments are REQUIRED; flow charts and pseudocode a
ID: 3775613 • Letter: U
Question
Unit 6 Chapter 15 Assignment
Comments are REQUIRED; flow charts and pseudocode are NOT REQUIRED
Directions
The files must be called <LiFiUnit6Ch15.java> LiFiSaleCheck.java (Sale Checker Class File)
The files must be called as specified above, (LiFi = Your Last Initial Your First Initial)
Proper coding conventions required the first letter of the class start with a capital letter and the first letter of each additional word start with a capital letter.
Style Components
Include properly formatted prologue, comments, indenting, and other style elements.
Topics Covered
Topics with * are covered in this assignment. Ensure you use every item listed below in your completed assignment.
*Exceptions and Exception Messages
*try / catch
*checked and unchecked exceptions
*generic catch block
*throws
Basic Requirements
Write a program that validates a sale in dollars and cents with a $ and a .(period).
See sample output below.
LiFiUnit6Ch15.java
- Driver class should loop until “q” is entered to quit
- If enter is not “q”, then create an instance of the LiFiSaleCheck object passing the entry as an argument
- If no error
- Print amount by calling:
- print numeric from LiFiSaleCheck class
- print alphabetic from LiFiSaleCheck class
- If error
- Print error message (see sample)
LiFiSaleCheck.java class
Sales object should store the sale in 2 integer instance variables, dollars and cents, and include a string variable to hold the error. This should be initialized with null.
LiFiSaleCheck Constructor:
- Receive sale as a string
- Perform error checking to ensure time was entered in proper format to include a color (.) between the dollars and cents and a $ at the start of the string.
- Use indexOf and substring to separate the sale string into the appropriate instance variables
- Use try/catch to catch format errors of dollars and cents as shown in example
- If an error occurs, change the error instance variable to reflect the error (see sample)
- If more than one error occurs in the format of the dollars and cents, show both.
print numeric method:
- Print in the format $123.45 using both dollars and cents instance variables
- Print in the format 123 dollars and 45 cents using both dollars and cents instance variables
print alphabetic method:
- Print in the format 123 dollars and 45 cents using both dollars and cents instance variables
getError method:
- Include a getError method that returns the error instance variable to the print method
Sample
Your output will vary based on input.
<See Unit6 001 Sample>
Explanation / Answer
//Part I Driver Class
LiFiUnit6Ch15.java
package LiFi
import LiFi.LiFiSaleCheck;
import java.util.*;
public class Lifi
{
public static void main(String args[]) throws Exception // Driver class method
{
System.out.println("Enter your choice");
char ch;
Scanner sc = new Scanner(System.in);
ch = sc.next().charAt(0); // Taking input of the letter first
try
{
while(ch !='q') // looping until 'Q' is entered
{
System.out.println("Enter amount of sale in form $#.## <q to quit>");
String[] sale = sc.nextLine().split("."); // split the entered string based on period
LifiSaleCheck salecheck = new LifiSaleCheck(sale); // Initiating the object with entered sale amount
salecheck.printNumeric(); // Calling printNumeric from driver class
salecheck.printAlphabet(); // calling printAlphabet from driver class
}
}
catch(Exception e)
{
System.out.println("Please enter character to proceed");
}
}
}
// Part 2 file LiFiSaleCheck.java
package LiFi
import LiFi.LiFiUnit6Ch15
import java.util.*;
public class LiFiSaleCheck
{
public LiFiSaleCheck(String[] sale) // taking sale argument for constructor
{
String var = null;
if(var==null)
{
Sytem.out.println("Error in sales value");
}
else
{
try { dollars=Integer.parseInt(sale[0]); } // Converting the object into integer
catch(Exception e) // Catching exception and printing the error using Try catch block
{
System.out.println("Invalid dollar format for input string : " + sale[0]);
}
try { cents = Integer.parseInt(sale[1]); } // converting cents to integer
catch(Exception e)
{
System.out.println("Invalid centsformat for input string : " + sale[1]);
}
}
public static void main(String args[]) thorws Exception
{
public static Integer dollars,cents;
}
public void printNumeric()
{
System.out.println("$"+dollars+"."+cents);//Printing in Numberic form
}
public void printAlphabet()
{
System.out.println(dollars+" "+dollars+ " and "+cents+" "+"cents"); // Printing in alphabetic form
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.