Java Question

Learning Outcome(s):

  • Explain the basic principles of programming, concept of language, and universal constructs of programming languages.
  • Demonstrate Integrated Development Environment (IDE) for the editing, building, debugging, and testing of programs.

Question One

The following Code should read the first and the second number of the student ID then print the sum and divide it by zero if possible. However, there are three errors in the code. Find each error and specify its type (syntax, runtime, or logic) and correct them in the bellow table. Then, write the code after you correct all errors and provide screenshot of the output.

Note:

1. The correct code should be tested using your first two numbers of your student ID.

2. Your answer should have the code as text as well as the screenshot of the program output (using your own student ID) as a part of your answer. Otherwise, zero marks will be awarded.

import java.util.Scanner;

public class Main {

public static void main(String[] Strings) {

Scanner input = new Scanner(System.in);

System.out.println(“Enter the first number of your student ID: “);

int number1 =input.nextInt();

Systm.out.println(“Enter the second number of your student ID: “);

int number2 =input.nextInt();

System.out.println(“The sum of two two numbers is ” + number1+number2);

System.out.println(“The sum devided by Zero is: ” + (number1+number2)/0);

}

}

Error

Type

Correction

2 Marks

Learning Outcome(s):

Recognize the concept of object-oriented programming and creating classes.

Question Two

Write a complete Java program that does the following:

1. Asks the user to enter his / her name, age, student ID, and GPA.

2. Stores the entered information in adequate variables.

3. Prints the student’s name, age, Student ID, and GPA using “printf” method. All information should be printed in one line separated by tap space.

Note:

3. The class name of your Java program must be your first name followed by (_) and your student ID.

4. Your answer should have the code as text as well as the screenshot of the program output (using your own student ID) as a part of your answer. Otherwise, zero marks will be awarded.

2 Marks

Learning Outcome(s):

Develop a program based on specification using programming language elements including syntax, data types, conditional statement, control structures, procedures and arrays, objects and classes

Question Three

Write a Java program that calculates and prints the area and volume of a sphere. Your program should do the following:

  • Ask the user to enter the radius of the sphere.
  • Calculate the area of the sphere using the formula 4πr^2, where r is the radius, and the value of π is approximately 3.14159.
  • Calculate the volume of the sphere using the formula (4/3)πr^3, where r is the radius.
  • Print the calculated area and volume values to the console in a user-friendly format using System.out.printf() method with proper formatting.
  • Your answer should have the code as text as well as the screenshot of the program output

Output:

2 Marks

Learning Outcome(s):

Instructors: State the Learning Outcome(s) that match this question

Question Four

Write a Java program that does the following:

  • Print your full name.
  • Ask the user to enter their gender As M or F
  • If the gender is M, then print Male. Otherwise print Female.
  • Your answer should have the code as text as well as the screenshot of the program output.