Object-oriented Programming
Problem with solution
1. Simple Java program that display message (Welcome to Java!) to the console
public class SimpleJavaProgram {
public static void main(String[] args) {
[Link]("Welcome to Java!");
}
}
2. Write a program to display your name, your ID number, date of birth , and department.
The class name of your program should be StudentInformation.
public class StudentInformatin {
public static void main(String[] args) {
String IDNumber = "RAMIT/2033/2010";
int DateOfBirth = 1994;
[Link]("My name is : Abe Kebe");
[Link]("My ID number is : " + IDNumber);
[Link]("From : Faculity of Electrical and Computer Engineering");
}
}
3. (Computing the volume of a cylinder) Write a program that reads in the radius and length
of a cylinder and computes volume using the following formulas:
area = radius * radius * π
volume = area * length
import [Link];
public class VolumeOfACylinder {
public static void main(String[] args) {
Scanner input = new Scanner([Link]); // Create a Scanner object
final double PI = 3.14159265359;
// Prompt user to enter the radius and length of a cylinder
[Link]("Enter the radius and length of a cylinder: ");
double radius = [Link]();
double length = [Link]();
double area = radius * radius * PI;
double volume = area * length;
[Link]("The area is: " + area); // Display results to the console
[Link]("The volume is: " + volume);
}
}
4. (Random month) Write a program that randomly generates an integer between 1 and 12
and displays the English month name January, February, …, December for the number 1,
2, …, 12, accordingly. a. Using nested if statement and b. Using switch
a. Using nested if statement
public class RandomMonthIf {
public static void main(String[] args) {
// Generate an integer between 1 and 12.
int month = (int)(([Link]() * 12) + 1);
// Display the English month name
if(month==1)
[Link]("January");
else if(month==2)
[Link]("February");
else if(month==3)
[Link]("March");
else if(month==4)
[Link]("April");
else if(month==5)
[Link]("May");
else if(month==6)
[Link]("June");
else if(month==7)
[Link]("July");
else if(month==8)
[Link]("August");
else if(month==9)
[Link]("September");
else if(month==10)
[Link]("October");
else if(month==11)
[Link]("November");
else
[Link]("December");
}
}
b. Using switch
public class RandomMonth {
public static void main(String[] args) {
int month = (int)(([Link]() * 12) + 1); // Generate an integer between 1 and 12.
switch (month) { // Display the English month name
case 1: [Link]("January"); break;
case 2: [Link]("February"); break;
case 3: [Link]("March"); break;
case 4: [Link]("April"); break;
case 5: [Link]("May"); break;
case 6: [Link]("June"); break;
case 7: [Link]("July"); break;
case 8: [Link]("August"); break;
case 9: [Link]("September"); break;
case 10: [Link]("October"); break;
case 11: [Link]("November"); break;
case 12: [Link]("December");
}
}
}
5. (Find numbers divisible by 5 and 6) Write a program that displays all the numbers from
100 to 1,000, ten per line, that are divisible by 5 and 6. Numbers are separated by exactly
one space.
public class FindingNumbersDivisible {
public static void main(String[] args) {
// Display 10 numbers per line int count = 0;
// Count the number of numbers divisible by 5 and 6 final int NUMBERS_PER_LINE = 10;
// Test all numbers from 100 to 1,000
for (int i = 100; i <= 1000; i++) {
// Test if number is divisible by 5 and 6
if (i % 5 == 0 && i % 6 == 0) {
count++; // increment count
if (count % NUMBERS_PER_LINE == 0) // Test if numbers per line is 10
[Link](i);
else
[Link](i + " ");
}
}
}
}