This is an interactive program that requires you to implement a collection of ut
ID: 659739 • Letter: T
Question
This is an interactive program that requires you to implement a collection of utility methods.
Specification
doGrades()
Write a static class method named doGrades that uses a repetition control statement to query the user to enter in a letter grade.
The method does not return anything, nor does it receive any parameters.
The method checks if an entered value is a valid grade and prints a status message.
The input loop is terminated when an ampersand (&) is entered (sentinel value). Issue a confirmation prompt when the user enters the sentinel value.
Validity checking of the grade value is defined in the isGrade() method.
isGrade()
Write a static class method named isGrade that receives a char as a parameter. It returns the boolean value true if the received character is a valid grade value; otherwise, it returns false.
Valid grade values can be either upper- or lower-case.
Valid grades values are:
doZipcodes()
Write a static class method named doZipcodes that does not return anything, nor does it take any parameters.
Enter into a loop prompting the user to enter a five digit zipcode.
Validate the zipcode and print a status message.
If the user enters a zipcode value of 0 (zero), then issue a confirmation prompt to make sure they are are really done entering zipcodes. Terminate the loop if they answer yes to the confirmation prompt.
Zipcode validation is performed by the isZipcode() method.
isZipcode()
Write a static class method named isZipcode that receives a the entered zipcode stored in a String object as a parameter. Return the boolean value false if any of the following are true:
the String length is not equal to five (5),
the String contains a non-digit character,
the String when converted to an int is less than 1 or greater than 99999;
otherwise, return the boolean value true.
doRanges()
Write a static class method named doRanges() that takes no parameters and returns nothing (i.e. it has a void return type).
Use a repetition control statement to get the following integer inputs:
minimum value
maximum value
value
Test to see if the value falls between the minimum and maximum values, and print a status message.
The loop is terminated if the minimum and maximum values are equal. Issue a confirmation prompt to make sure the user is done comparing numbers.
The range check is implemented in the inRange() method.
inRange
Write a static class method named inRange that takes three int values as parameters. The following is the method header.
Return -1 if the value is less than the minimum value. Return 0 if the value is greater than or equal to the minimum value and is less than or equal to the maximum value. Return 1 if the value is greater than the maximum value.
main
Write a static class method named main() that calls methods that validate letter grades, validate zipcodes, and test if a number fall in a range of values.
The following methods are called:
Hints
Converting a String object into an int can be accomplished using the int Integer.parseInt(String) static class method defined in the Integer class that comes with the JSDK.
A specific String character can be accessed using the char stringObject.charAt(int) instance method defined in the String class.
This program requires you to write the following methods having the names listed.
Be sure your prompts are short, yet descriptive. In some cases a two line prompt may be necessary.
Example Output
Note: For documentation purposes user inputs are inside angle-brackets.
Explanation / Answer
Program:
import java.io.*;
import java.util.*;
import java.lang.*;
class gradeziprange
{
//main method
public static void main(String[] args)
{
doGrades();
doZipcodes();
doRanges();
}
// Method for grade check
public static void doGrades()
{
String[] grade=new String[1];
Scanner in = new Scanner(System.in);
do
{
System.out.println("Enter the Grade:");
grade[0]=in.nextLine();
if(grade[0].equals("&"))
break;
else
if(isGrade(grade[0])==true)
System.out.println("Grade '" + grade[0]+"' is valid.");
else
System.out.println("Grade '" + grade[0]+"' is invalid.");
}while(!grade[0].equals("&"));
}
// method to check the input is grade or not
public static boolean isGrade(String grade)
{
if(grade.equalsIgnoreCase("A")||
grade.equalsIgnoreCase("B")|| grade.equalsIgnoreCase("C")|| grade.equalsIgnoreCase("D")|| grade.equalsIgnoreCase("F")||
grade.equalsIgnoreCase("W")|| grade.equalsIgnoreCase("Y")|| grade.equalsIgnoreCase("I")|| grade.equalsIgnoreCase("P")|| grade.equalsIgnoreCase("Z"))
return true;
else
return false;
}
// Method for zip check
public static void doZipcodes()
{
String zip,opt="n";
boolean stat;
Scanner in = new Scanner(System.in);
do
{
System.out.println("Enter the five digit zip
code:");
zip=in.nextLine();
if(zip.equals("0"))
{
System.out.println("Are you sure you want to
exit?");
opt=in.nextLine();
if (opt.equalsIgnoreCase("y"))
break;
}
else
{
stat=isZipcode(zip);
if(stat==false)
{
if(!isNumeric(zip))
System.out.println(" *** error:
zipcode "+zip+" is not a number");
else if(zip.length()>5)
System.out.println(" *** error:
zipcode "+zip+" too long");
else if(zip.length()<5)
System.out.println(" *** error:
zipcode "+zip+" too low");
}
else
System.out.println(" zipcode "
+zip+"is valid");
}
}while(!opt.equalsIgnoreCase("y"));
}
// Method to check input is zip or not
public static boolean isZipcode(String zip)
{
if(isNumeric(zip))
{
int val=Integer.parseInt(zip);
if(zip.length()!=5||val<1||val>99999)
return false;
else
return true;
}
else
return false;
}
// Method to check input string contains number or not
public static boolean isNumeric(String str)
{
try
{
double d = Double.parseDouble(str);
}
catch(NumberFormatException nfe)
{
return false;
}
return true;
}
// Method for range check
public static void doRanges()
{
String opt1="n";
int min,max,val,ret;
try{
DataInputStream in = new
DataInputStream(System.in);
do
{
System.out.println("Enter minimum integer
range value: ");
min=Integer.parseInt(in.readLine());
System.out.println("Enter maximum integer
range value( "+min+" ) to exit");
max=Integer.parseInt(in.readLine());
if(min==max)
{
System.out.println("Are you sure you
want to exit?");
opt1=in.readLine();
if (opt1.equalsIgnoreCase("y"))
break;
}
else if(min!=max)
{
System.out.println("Enter integer
value:");
val=Integer.parseInt(in.readLine());
ret=inRange(min, max, val);
if (ret==-1)
System.out.println(val+" less
than minimum value "+min);
else if (ret==1)
System.out.println(val+" greater
than maximum value "+max);
else if (ret==0)
System.out.println(val+" is
between "+ min + " and "+max);
}
}while(!opt1.equalsIgnoreCase("y"));
}catch(Exception e){};
}
// Method to check input in range or not
public static int inRange(int min, int max, int value)
{
if(value>=min && value<=max)
return 0;
else if(value>max)
return 1;
else
return -1;
}
}
Result:
Enter a grade (A, B, C, D, F, W, Y, I, P, Z, or & to exit): A
Grade 'A' is valid.
Enter a grade (A, B, C, D, F, W, Y, I, P, Z, or & to exit): e
Grade 'e' is invalid.
Enter a grade (A, B, C, D, F, W, Y, I, P, Z, or & to exit): b Grade 'b' is valid.
Enter a grade (A, B, C, D, F, W, Y, I, P, Z, or & to exit): &
Are you sure you want to exit? n
Enter a grade (A, B, C, D, F, W, Y, I, P, Z, or & to exit): 5
Grade '5' is invalid.
Enter a grade (A, B, C, D, F, W, Y, I, P, Z, or & to exit): &
Are you sure you want to exit? y
Enter a five digit zipcode (0 to exit): 844
*** error: zipcode "844" too short
Enter a five digit zipcode (0 to exit): 852856
*** error: zipcode "852856" too long
Enter a five digit zipcode (0 to exit): hello
*** error: zipcode "hello" is not a number
Enter a five digit zipcode (0 to exit): 85256
zipcode "85256" is valid
Enter a five digit zipcode (0 to exit): 0
Are you sure you want to exit? n
Enter a five digit zipcode (0 to exit): 0
Are you sure you want to exit? y
Enter minimum integer range value: 0
Enter maximum integer range value (0 to exit): 20
Enter integer value: 44
44 greater than maximum value 20
Enter minimum integer range value: -10
Enter maximum integer range value (-10 to exit): 10
Enter integer value: 0
0 is between -10 and 10
Enter minimum integer range value: 13
Enter maximum integer range value (13 to exit): 18
Enter integer value: 7
7 less than minimum value 13
Enter minimum integer range value: 1
Enter maximum integer range value (1 to exit): 1
Are you sure you want to exit? n
Enter minimum integer range value: -99
Enter maximum integer range value (-99 to exit): -99
Are you sure you want to exit? y
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.