Get Instant Help From 5000+ Experts For
question

Writing: Get your essay and assignment written from scratch by PhD expert

Rewriting: Paraphrase or rewrite your friend's essay with similar meaning at reduced cost

Editing:Proofread your work by experts and improve grade at Lowest cost

And Improve Your Grades
myassignmenthelp.com
loader
Phone no. Missing!

Enter phone no. to receive critical updates and urgent messages !

Attach file

Error goes here

Files Missing!

Please upload all relevant files for quick & complete assistance.

Guaranteed Higher Grade!
Free Quote
wave

Utilization of Loops and If Statements

Java is a high-level object-oriented programming language (Maiga and Emanuel 2019). It has its runtime environment known as JRE (Liang 2018). It is mainly used for its security features. With the help of Java, several applications can be built. Such as Network applications, desktop applications, android apps, and many more. While compared with other programming languages, Java has the best feature related to security and functionality. There are several other features of java as well (Grgic, Mihaljevi? and Radovan 2018). These are object-oriented, multithreaded, platform-independent, robust, and many more. In this ass assignment, java has been used as a programming language to create a Calculator Quiz Test Application. The application helps in understanding and developing skills associated with Java. The Calculator Quiz Test application is used to perform a simple addition of three random questions.

The report includes a detailed discussion of the process of creating this application. The report explains the code used while developing the application

The application Calculator Quiz test has been developed using loops and If statements. In java, there are three types of loops. These are For loop, while loop, and do-while loop. For loop is used on having fixed number of iterations. For loop include initializations, condition, increment or decrement, and statement. In the while loop, a part of the program executes again and again on the basis of the given boolean conditions. And in the do-while loop, the part of the program executes at least once and the further execution depends upon the given boolean conditions.

In the Calculator Quiz Test application, for loop has been used to generate the questions using random numbers.

And If else statement is used to run the code for more than one alternative (Kumar 2018). In the program, we have used several if-else statements, for example, if the answer given by the user is current then add + 10 marks for the user. Else deduct 5 marks from the user's total marks.

In the program, the scanner class has been used. It is available in java.util package (Nurzam and Luthfi 2018). This class is used to take input from the user having primitive data types such as int, double, and others (Aung et al., 2021). In the java program it is considered to be the simplest way to read the input. The object of the scanner class is created by defining the predefined object System.in, which illustrate the standard input stream.

In the Calculator Quiz Test application, the scanner class has been used to take the answer from the user. For each question asked by the application, the answer given by the user is stored in the variable answer of int data type. For each correct answer, 10 marks are added to the total marks.

The class of the Calculator Quiz Test application has been defined as public and named Quiz. Here, the public is a type of access modifier (Bandi, Fellah and Bondalapati 2019). Among all the other three modifiers, the public has access modifier has the widest scope. As, the classes, methods, or the data member declared in public can be accessed from any part of the program without any restrictions. So, for this application, public access modifier has been used. The other access modifiers are private, default and protected (Zoller and Schmolitzky 2012). In private access modifiers, the methods, variables and classes declared can not be access outside the class. In default access modifiers, the methods, variables and classes declared can be access if it is in same package (Zaw et al., 2018). If the no access modifiers are specified then it will be the default. And in protected access modifiers, the access level is within the package and outside the package through a child class. 

Usage of Scanner Class

The public static void main(String[] args) is considered to be the main method used in any java program. This method is used to run the java program. Here, the public is defined as an access modifier of this main method. Static is used so that the classes can be loaded into memory ising Java virtual machine and can call the main method. Void is used because this method does not have any return type. And the main is the name of the java main method. It is mandatory because while running the program, the program looks for the main method. And String[] args is the main method that accepts single argument of array having string data type.

System.out.println is used in java to print the statement passed to it. it displays the result on the monitor.

In the Calculator Quiz Test application, a random class has been used to generate random numbers for each question (Yang et al., 2020). The random objects have been created as:

Random r = new Random();

Different methods are used in random classes. These are double, ints, longs, next, next Boolean, next Byte, next Double, next Float, next Gaussian. Next int, next Long, and set Seed.

In java, variables are used to store the data (Gotseva, Tomov and Danov 2019). Each variable has a data type associated with it (Liu et al., 2019). In the Calculator Quiz Test application, several variables have been declared with the data types next to them. such as:

int num1, num2, sum, answer, correct=0, incorrect=0, marks=0, i;

Here, num1 and num2 have been used to generate a random number of questions between 1 to 100.

The correct answer given by the user for each question is stored in the variable correct and similarly, the incorrect answer given by the user for each question is stored in the variable incorrect. The marks scored by the user after answering each question are stored in the variable marks. The array has been used to store the random number of sizes 6. As for each question, 2 objects will be required. In java, an array is an object that stores elements of similar data types. In an array, the elements are stored in a contiguous memory location. It is an index based in which at the 0th index the first element is stored and at the 1st index the 2nd element is stored. Here, the array has been declared as:

int randomNum[] = new int[6];

In the for loop, the program for (i=1; i<=3;i++) that is, program executes three times to generate 3 simple question for the user to sum two numbers generated randomly. The code used in the while loop are:

for(i=1;i<=3;i++)
{
// generating 2 random numbers from range 1 to 100
num1 = r.nextInt(100)+1;
num2 = r.nextInt(100)+1;
sum = num1+num2;

// taking answer from user.
System.out.printf("Question :"+i+"n What's the total of "+num1+"+"+num2+"? ");
answer = sc.nextInt();

// for correct answer
if(answer ==sum)
{
correct++;
marks = marks+10;
}
// for incorrect answer
else{
incorrect++;
marks = marks-5;
}
randomNum[(i-1)*2] = num1;
randomNum[((i-1)*2)+1] = num2;
}

Then, the result is displayed showing the total count of the correct answers, the total count of the incorrect answers, total marks, and the correct answers for each question that were asked. At the end the application also displays the thank you note for the user for taking the quiz test.

Access Modifiers

The code for the results that will be displayed at the end after playing the quiz are:

System.out.println("Total number of correct answers: "+correct);
System.out.println("Total number of incorrect answers: "+incorrect);
System.out.println("Total marks: "+marks);

System.out.println("nAnswers for the quiz:");
for(i=0;i<6;i=i+2)
{
sum = randomNum[i]+ randomNum[i+1];
System.out.println(randomNum[i]+"+"+ randomNum[i+1]+"="+sum);
}
System.out.println("nThank you taking the “Calculator Quiz-Test”. See you again.! ");

The code has been pasted in the word document and formatted as font style Courier with 10pts below:

import java.util.Random;
import java.util.Scanner;

public class Quiz
{
public static void main(String[] args) {
//using random class to generate random numbers
Random r = new Random();

//scanner class to take input from the user
Scanner sc = new Scanner(System.in);
int num1, num2, sum, answer, correct=0, incorrect = 0, marks=0, i;
System.out.println("Welcome to “Calculator Quiz-Test” ");

//creating array
int randomNum[] = new int[6];
for(i=1;i<=3;i++)
{
// generating 2 random numbers from range 1 to 100
num1 = r.nextInt(100)+1;
num2 = r.nextInt(100)+1;
sum = num1+num2;

// taking answer from the user.
System.out.printf("Question :"+i+"n What's the total of "+num1+"+"+num2+"? ");
answer = sc.nextInt();

// for correct answer
if(answer ==sum)
{
correct++;
marks = marks+10;
}
// for incorrect answer
else{
incorrect++;
marks = marks-5;
}
randomNum[(i-1)*2] = num1;
randomNum[((i-1)*2)+1] = num2;
}

// Displaying the results.
System.out.println("Total number of correct answers: "+correct);
System.out.println("Total number of incorrect answers: "+incorrect);
System.out.println("Total marks: "+marks);

System.out.println("nAnswers for the quiz:");
for(i=0;i<6;i=i+2)
{
sum = randomNum[i]+ randomNum[i+1];
System.out.println(randomNum[i]+"+"+ randomNum[i+1]+"="+sum);
}
System.out.println("n Thank you taking the “Calculator Quiz-Test”. See you again.! ");

}
}

The output of the program is similar to the sample output provided in the assignment file. The results are displayed with the welcome message at the beginning of the output. Then the Questions are asked. There is total three questions that have been asked. Each input given by the user are stored and displayed next to each question. At the end the total correct answer given by the user is displayed and total incorrect answers is displayed. the total marks are calculated by adding plus 10 for each correct answer and -5 for each incorrect answer. And the answer for the questions asked are displayed at the last. The answers for each question is calculated using a simple addition operators. And at the end a thank you message is displayed to show the quiz is over.

Conclusion

Thus, the program Calculator Quiz-Test application has been developed using if-else statement, loops such as for loop and array. If else statement is used to run the code for more than one alternative. The For loop is used when there are fixed number of iterations to be used. The For loop includes three things. These are initializations, condition and increment or decrement. An array is an object that has elements of same data types. In an array, the elements are stored in a adjacent memory location. It is an index based in which at the 0th index the first element is stored and at the 1st index the 2nd element is stored. The application Calculator Quiz test also used several other classes such as scanner, and random classes. The Scanner class is used to read the input given by the user having primitive data types such as int, double, and others. It is the considered to be the simplest way to read the input in a java program. The object of the scanner class is created by defining the predefined object System.in, which represents the standard input stream. And random class has been used to generate random numbers for each question. The program asks the user to answer three questions using for loop. Each input given by the user is stored in variable answers. For each correct answer, 10 marks are added to the total marks and for each incorrect answer, 5 marks are subtracted from the total marks.  The assignment is useful in understanding and developing skills in java. Especially with the use of loops, if-else statements, and array expressions. Overall, the assignment was helpful in understanding the basic skills related to java programming language.

References

Aung, S.T., Funabiki, N., Syaifudin, Y.W., Kyaw, H.H.S., Aung, S.L., Dim, N.K. and Kao, W.C., 2021. A proposal of grammar-concept understanding problem in Java programming learning assistant system. J. Adv. Inform. Tech.(JAIT).

Bandi, A., Fellah, A. and Bondalapati, H., 2019. Embedding security concepts in introductory programming courses. Journal of Computing Sciences in Colleges, 34(4), pp.78-89.

Gotseva, D., Tomov, Y. and Danov, P., 2019, October. Comparative study java vs kotlin. In 2019 27th National Conference with International Participation (TELECOM) (pp. 86-89). IEEE.

Grgic, H., Mihaljevi?, B. and Radovan, A., 2018, May. Comparison of garbage collectors in Java programming language. In 2018 41st International Convention on Information and Communication Technology, Electronics and Microelectronics (MIPRO) (pp. 1539-1544). IEEE.

Kumar, M., 2018. Energy Efficiency of Java Programming Language. Wayne State University.

Liang, Y.D., 2018. Introduction to Java programming and data structures. Pearson Education.

Liu, Z., Xia, X., Lo, D., Xing, Z., Hassan, A.E. and Li, S., 2019. Which variables should i log?. IEEE Transactions on Software Engineering, 47(9), pp.2012-2031.

Maiga, J. and Emanuel, A.W.R., 2019. Gamification for Teaching and Learning Java Programming for Beginner Students-A Review. J. Comput., 14(9), pp.590-595.

Nurzam, F.D. and Luthfi, E.T., 2018, March. Implementation of real-time scanner Java language text with mobile vision Android based. In 2018 International Conference on Information and Communications Technology (ICOIACT) (pp. 724-729). IEEE.

Yang, J., Lee, Y., Fernandez, A. and Sanchez, J., 2020. Evaluating and securing text-based java code through static code analysis. Journal of Cybersecurity Education, Research and Practice, 2020(1), p.3.

Zaw, K.K., Funabiki, N., Mon, E.E. and Kao, W.C., 2018, October. An informative test code approach for studying three object-oriented programming concepts by code writing problem in Java programming learning assistant system. In 2018 IEEE 7th Global Conference on Consumer Electronics (GCCE) (pp. 629-633). IEEE.

Zoller, C. and Schmolitzky, A., 2012, October. Measuring inappropriate generosity with access modifiers in Java systems. In 2012 Joint Conference of the 22nd International Workshop on Software Measurement and the 2012 Seventh International Conference on Software Process and Product Measurement (pp. 43-52). IEEE.

Cite This Work

To export a reference to this article please select a referencing stye below:

My Assignment Help. (2022). Java Calculator Essay: Process And Code.. Retrieved from https://myassignmenthelp.com/free-samples/bis2004-object-oriented-programming/calculator-quiz-test-application-file-A1E0CAA.html.

My Assignment Help (2022) Java Calculator Essay: Process And Code. [Online]. Available from: https://myassignmenthelp.com/free-samples/bis2004-object-oriented-programming/calculator-quiz-test-application-file-A1E0CAA.html
[Accessed 05 November 2024].

My Assignment Help. 'Java Calculator Essay: Process And Code.' (My Assignment Help, 2022) <https://myassignmenthelp.com/free-samples/bis2004-object-oriented-programming/calculator-quiz-test-application-file-A1E0CAA.html> accessed 05 November 2024.

My Assignment Help. Java Calculator Essay: Process And Code. [Internet]. My Assignment Help. 2022 [cited 05 November 2024]. Available from: https://myassignmenthelp.com/free-samples/bis2004-object-oriented-programming/calculator-quiz-test-application-file-A1E0CAA.html.

Get instant help from 5000+ experts for
question

Writing: Get your essay and assignment written from scratch by PhD expert

Rewriting: Paraphrase or rewrite your friend's essay with similar meaning at reduced cost

Editing: Proofread your work by experts and improve grade at Lowest cost

loader
250 words
Phone no. Missing!

Enter phone no. to receive critical updates and urgent messages !

Attach file

Error goes here

Files Missing!

Please upload all relevant files for quick & complete assistance.

Plagiarism checker
Verify originality of an essay
essay
Generate unique essays in a jiffy
Plagiarism checker
Cite sources with ease
support
close