Object Oriented Programming Exercises 1
Object Oriented Programming Exercises 1
1. Write an application that asks the user to enter two integers, obtains them from the user and displays the
larger number followed by the words “is larger”. If the numbers are equal, print the message “These numbers
are equal “
// File: Question1.java
// Author: Abdulla Faris
2. Write an application that inputs three integers from the user and displays the sum, average, product, smallest
and largest of the numbers.
// File: Question2.java
// Author: Abdulla Faris
// display the sum, average, product, smallest and the biggest of all three numbers
System.out.printf("\t-------------------------\n");
System.out.printf("\t\t\tSum =\t\t %d\n", num1+num2+num3);
System.out.printf("\t\t\tAverage =\t %d\n", (num1+num2+num3)/3);
System.out.printf("\t\t\tProduct =\t %d\n", num1*num2*num3);
System.out.printf("\t\t\tBiggest =\t %d\n", bigger);
System.out.printf("\t\t\tSmallest =\t %d\n", smaller);
}
}
3. Write an application that reads two integers, determines whether the first is a multiple of the second and
print the result. [Hint Use the remainder operator.]
// File: Question3.java
// Author: Abdulla Faris
4. The process of finding the largest value (i.e., the maximum of a group of values) is used frequently in
computer applications. For example, a program that determines the winner of a sales contest would input the
number of units sold by each sales person. The sales person who sells the most units wins the contest. Write
a Java application that inputs a series of 10 integers and determines and prints the largest integer. Your
program should use at least the following three variables:
a. counter: A counter to count to 10 (i.e., to keep track of how many numbers have been input and to
determine when all 10 numbers have been processed).
b. number: The integer most recently input by the user.
c. largest: The largest number found so far.
// File: Question4.java
// Author: Abdulla Faris
www.oumstudents.tk
int counter=0, number=0, largest=0; // initialize variables
5. Write a Java application that uses looping to print the following table of values:
// File: Question5.java
// Author: Abdulla Faris
6. Write a complete Java application to prompt the user for the double radius of a sphere, and call method
sphereVolumeto calculate and display the volume of the sphere. Use the following statement to calculate the
volume:
// File: Question6.java
// Author: Abdulla Faris
www.oumstudents.tk