Help SetParams FillArray DisplayResults Quit Upon program execution, the screen
ID: 3534135 • Letter: H
Question
Help SetParams FillArray DisplayResults Quit
Upon program execution, the screen will be cleared and the menu shown above will appear at the top of the screen and properly centered. The menu items are explained below. Note, that upon execution of the program only options Help, SetParams, and Quit will be functional. You need to use a bool type variable flags to check for the proper states.
H or h (for Help) option will invoke a function named help( ) which will display a help screen. The help screen(s) should guide the user how to interact with the program, type of data to be entered, and what results would the program produce. Each help screen should remain on the screen until the user strikes any key (e.g., strike the space bar followed by a carriage return). Once the user strikes a key, the screen will be cleared and the menu is displayed again.
S or s (for SetParams ) option will prompt the user for the number of students and the number of quizzes in a course. The number of students can be up to 50 and the number of quizzes can be up to 5. That is, you need to size arrays that can handle up to these limits. The user
Explanation / Answer
Below is the working code . I have cheked it in eclipse.
--------------------------------------------------------------------------------
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Driver {
Scanner scan=new Scanner(System.in);
int choice;
static int paramFlag=0,fillFlag=0;
int quizes[][],students[],studentData[][];
int numOfStudent,numOfQuiz;
/**
* this method is used to display menu to the user.
*/
public void displayMenu()
{
System.out.println("Menu");
System.out.println("-----------------------");
System.out.println("1.Help");
System.out.println("2.SetParams");
System.out.println("3.Fill Array");
System.out.println("4.Display Result");
System.out.println("5.Quit");
System.out.println("enter your choice");
choice=Integer.parseInt(scan.nextLine());
}
/**
* This method is used for showing help to the user.
* It will be invoked when user select Help from Menu.
*/
public void displayHelp()
{
System.out.println("**************************HELP*********************************");
System.out.println("Read this carefullly to understand the application");
System.out.println("You can choos any options in the menu but you have to maintain");
System.out.println("some kind of order like you have to set parameter before");
System.out.println("Calling the Fill Array Option");
System.out.println("Similarly display menu will work only after the data is entered");
System.out.println("At any point of time u can quit the application by choosing quit menu");
System.out.println("******************************************************************");
displayMenu();
}
/**
* This method is used for setting parameter like number of student and number of quiz.
* It will be invoked when user select SetParam from Menu.
*
*/
public void setParams()
{
paramFlag=1;
System.out.println("Enter the number of students");
numOfStudent=Integer.parseInt(scan.nextLine());
System.out.println("Enter the number of quizzes");
numOfQuiz=Integer.parseInt(scan.nextLine());
displayMenu();
}
/**
* This method is used for filling the array with data.It is for data entry like studentId and quiz marks.
* It will be invoked when user select FillArray from Menu.
*/
public void fillArray()
{
fillFlag=1;
students=new int[numOfStudent];
quizes=new int[numOfQuiz][numOfStudent];
studentData=new int[numOfStudent][numOfQuiz];
Random r = new Random();
int studentId=75678;
for(int i=0;i<numOfStudent;i++,studentId++)
{
students[i]=studentId;
}
for(int i=0;i<numOfStudent;i++)
{
for(int j=0;j<numOfQuiz;j++)
{
studentData[i][j]=r.nextInt(100);
}
}
for(int i=0;i<numOfQuiz;i++)
{
for(int j=0;j<numOfStudent;j++)
{
quizes[i][j]=studentData[j][i];
}
}
displayMenu();
}
/**
* This method is used for displaying the data entered.
* It will be invoked when user select DisplayResult from Menu.
*/
public void displayResult()
{
System.out.print("Id ");
for(int i=0;i<numOfQuiz;i++)
{
System.out.print("q"+(i+1)+" ");
}
System.out.println("");
for(int i=0;i<numOfStudent;i++)
{
System.out.print(students[i]+" ");
for(int j=0;j<numOfQuiz;j++)
{
System.out.print(studentData[i][j]+" ");
}
System.out.println("");
}
System.out.println("");
for(int i=0;i<numOfQuiz;i++)
{
Arrays.sort(quizes[i]);
}
int avg[]=new int[numOfQuiz];
for(int i=0;i<numOfQuiz;i++)
{
int sum=0;
for(int j=0;j<numOfStudent;j++)
{
sum+=quizes[i][j];
}
avg[i]=sum/3;
}
for(int i=0;i<numOfQuiz;i++)
{
System.out.println("Quiz"+(i+1));
System.out.println("Lowest:"+quizes[i][0]);
System.out.println("Highest:"+quizes[i][numOfStudent-1]);
System.out.println("Medium:"+quizes[i][numOfStudent/2]);
System.out.println("Average:"+avg[i]);
}
displayMenu();
}
/**
* this is main method which creates object of the this class and run the application
* It presents a menu to the user, and after user select a menu option it calls the corresponding
* method.
*/
public static void main(String[] args)
{
Driver d=new Driver();
d.displayMenu();
do
{
switch(d.choice)
{
case 1:
d.displayHelp();
break;
case 2:
d.setParams();
System.out.println("Parameters Are Set!!");
break;
case 3:
if(paramFlag==0)
{
System.out.println("You have to set the parameter before filling the array");
d.displayMenu();
break;
}
d.fillArray();
System.out.println("Data Entry Done !!");
case 4:
if(fillFlag==0)
{
System.out.println("You have to fill the array before displaying");
d.displayMenu();
break;
}
d.displayResult();
break;
case 5:
System.out.println("Thanx for using the application");
break;
default:
System.out.println("wrong choice");
}
}while(d.choice!=5);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.