In: Computer Science
Assignment #2*: Build A Report Purpose: Exercise, use, Inputs, Outputs, and perform conditional evaluation Requirements: (Multiple classes/Multiple types of input) • Input: Report Owner’s full name and 7 numbers (at least one double and one integer) o The owner’s name cannot contain any special characters, blank spaces, or numbers • You must use an if statement and at least one switch statement in your program • You are not allowed to have static variables or methods in any class except for the class with the main method. • You have to have at least 2 classes • You are not allowed to use ArrayLists or Vectors, only primitive arrays or string arrays if you want. Application Operation: 1.) Input, via a question in the console, the report owner’s first name as a string and build the last name via input, one character at a time. a. Check, conditionally, to make sure the first name and last name don’t contain any numeric characters, numbers between 0 – 9. If it does you must remove it. The names can not contain any white space either or special characters. 2.) Input report name via a request from the console. 3.) Input, and display, the total of the numeric input after each input is entered. Average the numeric input, indicate lowest numeric input value and the highest numeric input value for the previous numeric inputs, before the next numeric input is asked for. (Example given in class) 4.) Have a program exit input, condition, value available (i.e. if you type -1 the program exits) 5.) Create and display a final report that should have the report name, owner and the following: Numeric output should appear as a table with the following columns: (columns should be underlined) a. Input Number b. Highest Number c. Lowest Number d. Total (by the row) e. Average Number 6.) At the end of the report you must have a grand total for the numeric entries
import java.util.Scanner;
// Defines class Test
class Test
{
// To store first, last, report name
String fName, lName, reportName;
// To store total, average, height and lowest numbers
double total, average, highest, lowest;
// Scanner class object declared
Scanner sc;
// Default constructor to initialize data members
Test()
{
fName = lName = reportName = "";
highest = lowest = total = average = 0.0;
// Scanner class object created
sc = new Scanner(System.in);
}// End of default constructor
// Method to accept name
void acceptName(String message, int type)
{
// To store valid name
String validName = "";
// Displays message
System.out.print("\n Enter your " + message);
// Accepts name
String name = sc.next();
// Loops till end of the string
for(int c = 0; c < name.length(); c++)
{
// Extracts a character at c index position from string
char ch = name.charAt(c);
// Checks if the current character is a digit
if(Character.isDigit(ch))
;// Do nothing
// Otherwise add the character to valid name
else
validName += ch;
}// End of for loop
// Checks if the second parameter type is 1 (first name)
if(type == 1)
fName = validName;
// Checks if the second parameter type is 2 (last name)
else if(type == 2)
lName = validName;
// Otherwise the second parameter type is 3 (report name)
else
reportName = validName;
}// End of method
// Method to accept number
void acceptNumbers()
{
// Counter for number of numbers entered
int counter = 0;
// Loops till user enters -1
do
{
// Accepts a number
System.out.print("\n Enter a number (-1 to stop): ");
int no = sc.nextInt();
// Checks if user entered -1 then come out of loop
if(no == -1)
break;
// Otherwise
else
{
// Increase the counter by one
counter++;
// Checks if counter is 1
if(counter == 1)
// Assigns the number to both highest and lowest
highest = lowest = no;
// Otherwise not the first number
// Checks if number is greater than the earlier
// highest number
else if(no > highest)
// Assigns the number as the highest number
highest = no;
// Checks if number is less than the earlier
// lowest number
else if(no < lowest)
// Assigns the number as the lowest number
lowest = no;
// Calculates total and average
total += no;
average = total / counter;
// Displays message
System.out.print("\n Number: " + no +
"\n Highest: " + highest +
"\n Lowest: " + lowest +
"\n Total: " + total +
"\n Average: " + average);
}// End of else
}while(true); // End of do - while loop
}// End of method
}// End of class
// Driver class definition
public class ValidateTest
{
// main method definition
public static void main(String st[])
{
// Creates an object of class Test
Test t = new Test();
// Calls the method to accept first name
t.acceptName("first name.", 1);
// Calls the method to accept last name
t.acceptName("last name.", 2);
// Calls the method to accept report name
t.acceptName("report name.", 3);
// Displays names
System.out.println("\n First Name: " + t.fName +
"\n Last Name: " + t.lName +
"\n Report Name: " + t.reportName);
// Calls the method to accept numbers
t.acceptNumbers();
}// End of main method
}// End of driver class
Sample Output:
Enter your first name.P12yari
Enter your last name.Mo12han
Enter your report name.Number
First Name: Pyari
Last Name: Mohan
Report Name: Number
Enter a number (-1 to stop): 12
Number: 12
Highest: 12
Lowest: 12
Total: 12.0
Average: 12.0
Enter a number (-1 to stop): 10
Number: 10
Highest: 12
Lowest: 10
Total: 22.0
Average: 11.0
Enter a number (-1 to stop): 6
Number: 6
Highest: 12
Lowest: 6
Total: 28.0
Average: 9.333333333333334
Enter a number (-1 to stop): -1