Develop a java program using the control structures of


Question 1:

The objectives of this question are

(a) to declare variables

(b) to write simple Java program without selection and repetition structures

(c) to write Java program with selection structure

(a) Note: You are NOT allowed to use selection or repetition structure for Question 1(a).

Write a program that accepts one 3-digit and one 1-digit whole numbers from the command line, and output the working of the division of the 3-digit number (dividend) by the 1-digit number (divisor), showing the carry, the quotient and the remainder.

The program should output the carry digit on the first line, the divisor and dividend on the second line, and the quotient and remainder on the last line.

For example, if the two numbers are 158 and 7, the program should output:

        1 1
7)  1 5 8
----------
     0 2 2 R4

If the two numbers are 447 and 3, then the program should output:

       1 2
3) 4 4 7
------------

    1 4 9 R0

Note that you should not hardcode the values of the integers to be divided in your program. Instead, your program must read the values from the command line.

Submit your program listing together with screenshots showing THREE runs of your program with the sample inputs ("158" , "7") , ("447" , "3") on the command line and another pair of your own choice. Include your name and student number in the output.

(b) To determine the amount of subsidy for a doctor visit for common illnesses, refer to Table 1 from the webpage https://www.chas.sg/content.aspx?id=636, reproduced here.

363_Table.jpg

The subsidies for doctor visit for common illnesses applicable to only 3 categories of Singapore citizens with and without income are listed here:

- Pioneer citizens (born on or before 31 December 1949 and who is a Singapore citizen before 1 Jan 1987) receives up to $28.50 per doctor visit.

- For non-pioneer citizens with household income up to $1100 per person receives up to $18.50 per doctor visit,

- For non-pioneer citizens without income and living in residence with annual value up to $13000 receives up to $18.50 per doctor visit.

Singapore citizens not under these categories do not qualify for any subsidy for doctor visit for common illnesses.

Outline the control structures of structured programming to write a program that

(i) prompts and reads
- the bill amount,
- the birth year of the citizen,
- the year citizenship is obtained only if the user enters 1949 or earlier for the birth year,
- whether the citizen has an income only if the citizen is not from the pioneer generation,
- either the monthly income or the annual value of the residence only if the citizen is not from the pioneer generation.

(ii) uses table 1 to determine and display the amount subsidy. If no subsidy is applicable, it prints a message to state that the citizen does not qualify for subsidy.

(iii) computes the amount payable. The amount payable cannot be negative.

Four sample runs are shown below with user inputs are underlined:

Run 1:
Enter bill amount: $18 Enter birth year: 1949
Enter year of citizenship: 1986 Amount of subsidy: $28.50
No payment required.

Run 2:
Enter bill amount: $30 Enter birth year: 1940
Enter year of citizenship: 2000 Enter Y if you have income: Y
Enter household monthly income per person: $1000 Amount of subsidy: $18.50
Please pay: $11.50

Run 3:
Enter bill amount: $18.95 Enter birth year: 1977
Enter Y if you have income: Y
Enter household monthly income per person: $1000 Amount of subsidy: $18.50
Please pay: $0.45

Run 4:
Enter bill amount: $50 Enter birth year: 2000
Enter Y if you have income: N
Enter annual value of residence: $14000 Amount of subsidy: $0.00
Please pay: $50.00

Submit your program listing together with screenshot showing FOUR runs of your program for varying output. Include your name and student number in the output.

Question 2:

The objectives of this question are

(a) to practice the use of selection structure in simple Java program
(b) to practice the use of repetition structure in a Java program
(c) to practice the use of method in a Java program
(d) to manipulate string

Without using arrays or regular expressions, write a Java program to implement the game MasterMindTM using only strings, characters and integers. Use the Scanner class to read user input and the Math class to generate random number.

For the game, you must adhere to the following constraints:
- Your program makes 4 random choices to pick 4 different colours from 6 colours (stored as a string "RGBYOP"). The letters represent the 6 distinct colours: Red, Green, Blue, Yellow, Orange and Purple. The random choices are stored as a single string e.g., "YBRP".
- The player's guess is read as a string and consists of 4 different letters for 4 different colours e.g., "BORY". Each correct colour guessed in the right position earns a black pin (B) and each correct colour guessed in a wrong position earns a white pin (W). In this example, the guess earns 2 white pins and 1 black pin, and the program displays WWB.

- The player wins if he is able to guess the 4 correct colours in the correct positions within 8 attempts.
- The player is allowed to quit a game at any time by entering q when prompted for a guess.
- At the end of a game, the program prompts the player whether he wants to play a new game. The user should enter q if he wishes to exit the program.
- Outline the control structures of structured programming and use ALL three repetition
structures: for, while and do-while..

An example run is shown below:
Enter your guess using 4 distinct letters from RGBYOP and Q to quit: YPGO Result : WWB
Enter your guess using 4 distinct letters from RGBYOP and Q to quit: YGPR Result : WW
Enter your guess using 4 distinct letters from RGBYOP and Q to quit: GPOB Result : WWB
Enter your guess using 4 distinct letters from RGBYOP and Q to quit: YPOB Result : WWB
Enter your guess using 4 distinct letters from RGBYOP and Q to quit: YOBG Result : WWWW
Enter your guess using 4 distinct letters from RGBYOP and Q to quit: GOYB Result : WWWB
Enter your guess using 4 distinct letters from RGBYOP and Q to quit: GYBO Result : WWWB
Enter your guess using 4 distinct letters from RGBYOP and Q to quit: OBPG Result : WWB
Sorry, you have already made 8 guesses The answer is OYGB

Enter to play again or Q to quit:
Enter your guess using 4 distinct letters from RGBYOP and Q to quit: GBPY Result : WWWB
Enter your guess using 4 distinct letters from RGBYOP and Q to quit: GYPB Result : WWBB
Enter your guess using 4 distinct letters from RGBYOP and Q to quit: GYBP Result : BBBB
Congratulations! You have guessed correctly in 3 attempts Enter to play again or Q to quit: q

Hope you had fun!

Submit your program listing together with screenshot showing two runs of your program output. 

Question 3

The objectives of this question are

(a) to practice arrays in Java.

(b) to practice methods in Java.

Develop a Java program using the control structures of structured programming on an array data structure for a Java quiz application given this skeletal program.

import java.util.Scanner; public class Q3
{
public static void main (String [] args)
{
Scanner sc = new Scanner(System.in); String [] question = new String [5]; String [] optionA = new String [5]; String [] optionB = new String [5]; String [] optionC = new String [5]; char [] answer = new char [5]; boolean [] starred = new boolean [5];
initQuiz(question, optionA,optionB, optionC, answer, starred);
// incomplete main - add other statements here
}

// incomplete class - add other methods here

public static void initQuiz(String [] question, String [] optionA,
String [] optionB, String [] optionC, char [] answer, boolean [] starred)
{
question[0] = "What of the following is the default value of a local variable?"; optionA[0] = "0";
optionB[0] = "Depends upon the data type of variable"; optionC[0] = "Not assigned";
answer[0] = 'C'; starred[0] = false;

question[1] = "Which of the following is not a keyword in Java?"; optionA[1] = "int";
optionB[1] = "Boolean"; optionC[1] = "static"; answer[1] = 'B'; starred[1] = false;

question[2] = "Which will legally declare, construct, and initialize an array?"; optionA[2] = "int myList [] = {4, 3, 7};";
optionB[2] = "int [] myList = {\"1\", \"2\", \"3\"};"; optionC[2] = "int [] myList = (5, 8, 2);";
answer[2] = 'A'; starred[2] = true;

question[3] = "What is returned when the method substring(2, 4) is invoked on the " " string \"example\"?"
+ "\nInclude the answer in quotes as the result is of type String"; optionA[3] = "\"xam\"";
optionB[3] = "\"amp\"";
optionC[3] = "\"am\"";
answer[3] = 'C'; starred[3] = true;

question[4] = "What is the purpose of method parseInt defined in Integer class."; optionA[4] = "The method converts an integer to a String";
optionB[4] = "The method is used to convert String to an integer."; optionC[4] = "The method is used to convert String to Integer class"; answer[4] = 'B';
starred[4] = false;
}

public static int displayMenu()
{
Scanner sc = new Scanner(System.in); System.out.println("Menu\n1. Take Quiz \n2. Clear Attempts\n" +
"3. Remove Student \n4. List Student Scores\n0. Exit"); System.out.print("Enter option: ");
int option = sc.nextInt(); sc.nextLine(); return option;
}
}

Use several arrays to record the details of students who take the quiz:
- an array name to record names of students,
- an array scoreinPercent to record their scores (out of a maximum score of 100) and
- an array numAttempt to record the number of attempts they take.

Complete the Java skeletal program with the following methods:

(a) A method takeQuiz to allow either a new student to take the quiz or an existing student to retake the quiz. The method accepts the necessary arrays and an integer count which is the number of student data already recorded in the arrays.

The method prompts the user for a name. If the name does not exist and the count is already the maximum, the method displays the error message: Limit reached. Student cannot take quiz! Otherwise, allow the student to take (or retake) the quiz and print his score details. Increment count if the student who is taking the quiz is new.

A non-starred question is worth 1 mark and a starred question gets a bonus of 1 additional mark. The maximum number of marks is capped at 5 (100%) since there are 5 questions, even though a student may score 7 marks if all 3 non-starred questions and 2 starred questions are answered correctly.

This method returns the value of count.

A sample output, with user input underlined, of the takeQuiz method invocation is shown here:

Enter student name: Cindy
1) What of the following is the default value of a local variable?
A) 0
B) Depends upon the data type of variable
C) Not assigned Your answer? C

2) Which of the following is not a keyword in java?
A) int
B) Boolean
C) static
Your answer? C

*3) Which will legally declare, construct, and initialize an array?
A) int myList [] = {4, 3, 7};
B) int [] myList = {"1", "2", "3"};
C) int [] myList = (5, 8, 2); Your answer? C

*4) What is returned when the method substring(2, 4) is invoked on the string "example"?
Include the answer in quotes as the result is of type String
A) "xam"
B) "amp"
C) "am"

Your answer? C

5) What is the purpose of method parseInt defined in Integer class.
A) The method converts an integer to a String
B) The method is used to convert String to an integer.
C) The method is used to convert String to Integer class Your answer? C

You have answered 1 question(s) and 1 starred question(s) correctly Your score is 60.00%
Number of attempts: 1

A sample output is shown here when the method is invoked again for Cindy:
Enter student name: Cindy Existing student

1) What of the following is the default value of a local variable?
A) 0
B) Depends upon the data type of variable
C) Not assigned Your answer? b

2) Which of the following is not a keyword in java?
A) int
B) Boolean
C) static
Your answer? b

*3) Which will legally declare, construct, and initialize an array?
A) int myList [] = {4, 3, 7};
B) int [] myList = {"1", "2", "3"};
C) int [] myList = (5, 8, 2); Your answer? b

*4) What is returned when the method substring(2, 4) is invoked on the string "example"?
Include the answer in quotes as the result is of type String
A) "xam"
B) "amp"
C) "am"
Your answer? b

5) What is the purpose of method parseInt defined in Integer class.
A) The method converts an integer to a String
B) The method is used to convert String to an integer.
C) The method is used to convert String to Integer class Your answer? b

You have answered 2 question(s) and 0 starred question(s) correctly Your score is 40.00%
Number of attempts: 2

(b) A method clearAttempts to set to zero both the score and number of attempts for a particular student. The method accepts the necessary arrays and an integer count which is the number of data already recorded in the arrays. The method prompts for the name of the student whose data, except for his name, is to be cleared and displays an appropriate message such as Clearing attempts and score! when the data is cleared.

If the student name does not exist, the method displays the error message: Name does not exist!

(c) A method removeStudent to remove the student's data from all the relevant arrays. The method accepts the necessary arrays and an integer count which is the number of data already recorded in the arrays.

The method prompts for the name of the student whose data is to be removed, displays an appropriate message such as Removing student...! and overwrites the data of the student by packing the array elements and reducing the count by 1.

If the name does not exist, the method displays the error message: Name does not exist!

This method returns the value of count.

(d) A method listStudents to display the student details. The method accepts the necessary arrays and an integer count which is the number of data already recorded in the arrays.

If no data has been recorded in the arrays, the method displays the message: No data recorded yet!

Otherwise, the method displays the list of names together with the score details. A sample output is shown below:

Name Score(%)   Number of Attempts
Alan 40 1
Cindy 60 1
Brad 40 2

(e) The method main which does the following:
- declare and create the arrays name, scoreinPercent and numAttempt
that can record a maximum of 4 student data each.
- declare and initialise count, the number of data already recorded in the arrays to zero,
- repeatedly display a menu to allow the user to select an option and call the appropriate method to perform the task according to his selection until the user chooses to exit the program.

Submit the program listing together with a screenshot showing the output. In your run, you should demonstrate that each of the options and repetitions are working. Include your name and student number in the first line of your program output

Question 4

The objectives of this question are
(a) to write a basic Java class
(b) to write a simple tester to test your Java class

You have been engaged to develop a prototype using the object-oriented programming approach for a quiz application and to implement classes for this object-oriented application. The classes will be used to re-implement Question 3 using object-oriented programming approach.

(a) Write programs using object oriented programming approach to implement a Java class to model a Question. This class should have the following:
- Suitable instance variables for
- the text of the question
- the text of option A
- the text of option B
- the text of option C
- the answer
- whether the question is a starred question
- the number of marks allocated to the question.
This differs from Question 3, to allow for flexibility in the mark allocation.

- Two constructors, one with the input values for all the instance variables and the other with values for all the instance variables except for: whether the question is starred (set to set to the default value false) and number of marks allocated is set to 1. Avoid repeating assignment statements by making one of the constructors invoke the other constructor.

- The get and set methods for number of marks and the answer.

- The get method for whether the question is starred.

- A changedStarredStatus() method that does not have any parameter and does not return any value. This method changes a non-starred question to starred, and changes a starred question to non-starred.

- A isDifficult() method which returns true if the marks allocated is more than 2. Returns false otherwise.
- A compareTo() method that takes as parameter another question, compares the marks allocated to this question and the parameter, and returns 1 of this question is allocated more marks, 0 if their marks allocated are the same, and -1 if this question is allocated fewer marks.

- A toString() method that returns with descriptions, the values of the instance variables.

(b) Write a test program to exercise the class written in part (a) above. The test program should perform the following:
- Create the following 2 questions using the appropriate constructors, hold the objects in a single suitable data structure.

Question Text: Which of the following is not a keyword in java? Option A: int
Option B: Boolean Option C: static Answer: B
Is star question: No Marks allocated: 1

Question Text: Which will legally declare, construct, and initialize an array? Option A: int myList [] = {4, 3, 7};
Option B: int [] myList = {"1", "2", "3"};
Option C: int [] myList = (5, 8, 2); Answer: A
Is star question: Yes Marks allocated: 2
- Now, make the first question starred and set its marks to 3

- Use the compareTo method to compare the questions and display the questions with the fewer marks allocated.
- For questions that are considered difficult, add 1 to the marks. Display the BEFORE and AFTER details of these questions. A sample run of your program should show this output:

The question with fewer marks is
Which will legally declare, construct, and initialize an array?
A) int myList [] = {4, 3, 7};
B) int [] myList = {"1", "2", "3"};
C) int [] myList = (5, 8, 2); Answer: A
Starred:Yes Marks: 2

Increasing marks of difficult questions: Before update
Which of the following is not a keyword in java?
A) int
B) Boolean
C) static Answer: B Starred:Yes Marks: 3
After update, marks: 4

Submit your program listing together with screenshot of your output. Include your name and student identification number as part of the program output.

Request for Solution File

Ask an Expert for Answer!!
JAVA Programming: Develop a java program using the control structures of
Reference No:- TGS01557959

Expected delivery within 24 Hours