Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

Write a method qualityPoints that inputs a student’s average and returns 4 if it’s 90–100, 3 if 80–89, 2 if 70–79, 1 if 60–69 and 0 if lower than 60. Incorporate the method into an application that reads a value from the user and displays the result.

Short Answer

Expert verified
The method qualityPoints checks the student's average and returns 4 for averages 90-100, 3 for 80-89, 2 for 70-79, 1 for 60-69, and 0 for less than 60. Input is taken from the user and the result is displayed.

Step by step solution

01

Analyze the Problem

Understand the requirements for the qualityPoints method. It must accept an integer representing a student's average and return an integer value (4, 3, 2, 1, or 0) based on the range in which the average falls.
02

Write the qualityPoints Method

Create a method called qualityPoints that uses if-else statements to check the student's average and return the corresponding quality points. Each if-else block will check for a specific range of values.
03

Handling Different Ranges

In the method, use if-else conditions to check the ranges of the scores and return the appropriate number: return 4 for 90–100, 3 for 80–89, 2 for 70–79, 1 for 60–69, and 0 for less than 60.
04

Create the Main Application

Create the main application where the user's input is collected, preferably using a Scanner object in Java, and then pass this input to the qualityPoints method.
05

Output the Result

After calling the qualityPoints method with the user input, display the result which is the return value from the method. This will show how many quality points the student's average corresponds to.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

Key Concepts

These are the key concepts you need to understand to accurately answer the question.

Control Flow with if-else Statements
Understanding control flow is essential in programming, especially when you need to execute code based on certain conditions. Java, like many programming languages, uses if-else statements to control the flow of execution. Let's consider the qualityPoints method we're working with.

In this method, we determine the quality points of a student based on their average score. We do this by setting up several conditions using if-else statements. When the program runs, it checks these conditions in the order they are written. If the student's average is 90 or above and less than or equal to 100, we return 4. But, if the average doesn't meet this first condition, the program checks the next condition: is the average between 80 and 89? If so, return 3. This process continues until all conditions have been checked.

An important thing to note is that as soon as a condition is met and code within its block is executed, the program exits the if-else chain. So, make sure to arrange your statements in the correct order — from specific to general — so that the correct block of code gets executed for each input.
Method Implementation in Java
In Java, methods are used to perform certain actions, and they are also known as functions. A method must be declared within a class. It begins with its return type, which can be a data type like 'int' for integers, 'String' for texts, or even 'void' when no value is returned.

When implementing the qualityPoints method, we are defining a method that takes an integer as input and returns another integer. It encapsulates the if-else logic to determine the quality point. Here's a simplified structure:
  • Method Declaration: Define the method with the appropriate access modifier, return type, and parameter. For our example, 'public static int qualityPoints(int average)'.
  • Method Body: Include the if-else statements within curly braces that house the logic for returning the correct quality points based on the student's average.
Methods can be called upon as many times as needed, which promotes code reusability and makes our programs more readable and maintainable.
User Input Handling with Scanner
Interacting with users by getting input from them is a common requirement in many Java programs. The Scanner class provided in the 'java.util' package is a simple text scanner which can parse primitive types and strings using regular expressions.

To use Scanner for our qualityPoints application, we create an instance of the Scanner class which will read input from the user. Here's how we do it in simple steps:
  • Create an instance of Scanner by passing 'System.in' to its constructor, like 'Scanner scanner = new Scanner(System.in);'.
  • Prompt the user to enter their average score.
  • Use an appropriate Scanner method, such as 'nextInt()' if you're expecting an integer, to read the user's input.
  • Store this input in a variable to be passed to the qualityPoints method.
After the input has been used and the program no longer needs the Scanner, we should close it using 'scanner.close();' to avoid any resource leaks.

It's crucial to handle this input carefully, ensuring type compatibility and validating the input to prevent errors that could crash the application.

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Write an application that prompts the user for the radius of a circle and uses a method called circleArea to calculate the area of the circle.

Write a method isMultiple that determines, for a pair of integers, whether the second integer is a multiple of the first. The method should take two integer arguments and return true if the second is a multiple of the first and false otherwise. [Hint: Use the remainder operator.] Incorporate this method into an application that inputs a series of pairs of integers (one pair at a time) and determines whether the second value in each pair is a multiple of the first.

Write an application that plays “guess the number” as follows: Your program chooses the number to be guessed by selecting a random integer in the range 1 to 1000. The application displays the prompt Guess a number between 1 and 1000. The player inputs a first guess. If the player's guess is incorrect, your program should display Too high. Try again. or Too low. Try again. to help the player “zero in” on the correct answer. The program should prompt the user for the next guess. When the user enters the correct answer, display Congratulations. You guessed the number!, and allow the user to choose whether to play again. [Note: The guessing technique employed in this problem is similar to a binary search, which is discussed in Chapter 19, Searching, Sorting and Big O.]

Write a method integerPower(base, exponent) that returns the value of base exponent For example, integerPower(3, 4) calculates 34 (or 3 * 3 * 3 * 3). Assume that exponent is a positive, nonzero integer and that base is an integer. Use a for or while statement to control the calculation. Do not use any Math class methods. Incorporate this method into an application that reads integer values for base and exponent and performs the calculation with the integerPower method.

The greatest common divisor (GCD) of two integers is the largest integer that evenly divides each of the two numbers. Write a method gcd that returns the greatest common divisor of two integers. [Hint: You might want to use Euclid’s algorithm. You can find information about it at en.wikipedia.org/wiki/Euclidean_algorithm.] Incorporate the method into an application that reads two values from the user and displays the result.

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free