Anonymous

Register for more FREE stuff!

my subscriptions

Methods

Question 1

[Maximum mark: 3]



Create a Java program which will contain a method printIntroduction, which will print the following statement when called:


  • "Welcome to my Java program!"

Answers and Explanations

Question 2

[Maximum mark: 3]



Write a Java program which will contain the method greetUser. Your program should first allow the user to input their name and then call the greetUser method to print:


  • "Hello, {name}! Welcome to my Java program."

Answers and Explanations

Question 3

[Maximum mark: 4]



Write a Java program that prompts the user to enter an integer. Then, your program should call the isEven method which will check if the number input by the user is even.


If an odd number is given, the program should print:

  • "The number {number} is odd."

If an even number is given, the program should print:

  • "The number {number} is even."

Answers and Explanations

Question 4

[Maximum mark: 4]



Write a Java program that will allow the user to calculate the area of a triangle based on their inputs. It should prompt the user to input the height and base, and then calculate the area of the triangle in the areaCalculator method using the formula:

\[\text{Area} = \frac{\text{base} \times \text{height}}{2}\]

If the base or height are smaller or equal to zero, your program should print:

  • "Enter valid dimensions."

Otherwise, the program should print:

  • "The area of this triangle is {area}."

Answers and Explanations

Question 5

[Maximum mark: 6]



Write a Java program that prompts the user to enter three integers. The program should then call a method called findMax that takes these three integers as arguments and returns the largest of the three. The program should print the largest integer to the console.


If all three integers are equal, the program should print:

  • "All numbers are equal."

If the integers are not equal, the program should print:

  • "The largest number is {max}."

Answers and Explanations

Question 6

[Maximum mark: 6]



Write a Java program that performs the following tasks:

  • Prompts the user to enter a positive integer n.
  • Calls a method called countEvenNumbers that takes n as an argument and counts and prints the number of even numbers from 1 to n.
  • Calls another method called squareNumber that calculates and prints the square of number n.

If the entered number is less than or equal to 0, the program should print:

  • "Please enter a positive integer."

If the entered number is a positive integer, the program should print:

  • "The count of even numbers from 1 to {n} is {count}"
  • "The square of number {n} is {square}"

Answers and Explanations

Question 7

[Maximum mark: 6]



Write a Java program that will perform the following tasks:

  • Prompts the user to enter their employment type. It can be either full-time or part-time.
  • Prompts the user to enter their hourly wage.
  • Calls a method called calculateBonus that takes employment_type and wage as arguments and calculates the bonus. Full-time employees receive a 10% bonus on top of their hourly wage. Part-time employees receive a 5% bonus on top of their wage. Then, this function should print the total monthly salary with the bonus of an employee (assuming there is 160 working hours in a month).
  • Your program should have appropriate error handling in the case when a user inputs a wrong employment type or a negative wage.


If the entered wage is a positive number and a correct employment type is input (full-time or part-time), the program should print:

  • "The total monthly salary for this {employment_type} employee is {salary}"

Answers and Explanations

Question 8

[Maximum mark: 6]



Write a Java program which will check if given dimensions result in a square. Your program should do the following:


  • Prompt the user to input length and height as integers.
  • Call a method isSquare which will return the value true when the shape is a square (length is equal to height), and false otherwise.
  • Your main method should then print whether the shape is a square or not.

Your program should print:

  • "The following dimensions result in a square: {true_or_false}."

Answers and Explanations

Question 9

[Maximum mark: 8]



Consider arrays representing grade averages and students. Your task is to create a Java program that:


  • Uses predefined arrays for grade averages and students.
  • Calls a method gotDistinction which finds and prints all students who got distinction in this academic year. Distinction is given to all students with grades average above 5.8. The method should take as an argument a single student and their respective grade average.

The parallel arrays look as follows:

  • grade_averages: {5.2, 4.3, 6.3, 6.6, 5.1, 4.8, 5.9}
  • students: {"John", "Alice", "Mark", "Tom", "Sue", "Mike", "Sarah"}

If two students received distinction, your program should print:

  • "{student} received distinction with a grade of {grade}."
  • "{student} received distinction with a grade of {grade}."

Answers and Explanations

Question 10

[Maximum mark: 7]



Write a Java program which will allow the user to add and subtract two numbers. Your program should do the following:


  • Prompt the user to choose the operation they want to perform. They can either enter addition or subtraction.
  • Prompt the user to input two integers.
  • Call either the addition or subtraction method, depending on what operation the user input in the first prompt.
  • Return the value of the mathematical operation.

If the user selected addition the program should print:

  • "The result of the addition is {result}."

If the user selected subtraction the program should print:

  • "The result of the subtraction is {result}."

If the wrong mathematical operation is input, the user shouldn't have the option to input the numbers, the program should just print:

  • "Wrong operation. Only addition and subtraction are possible."

Answers and Explanations