1. Universidad De Manila
Antonio J. Villegas Street Mehan Garden
Ermita, Manila
College of Engineering and Technology
Compilation of
Exercises in
Object Oriented Programming
Submitted by:
Estorninos, Rose Ann G.
CO-41
Instructor:
Engr. Elmerito D. Pineda
2. EXERCISE #1
package javaapplication13
public class JavaApplication13 {
public static void main(String[] args) {
System.out.println("ttRESUMEn");
System.out.println("tPERSONAL DATA:n");
System.out.println("Rose Ann G. Estorninos");
System.out.println("2314 Gapan St. Gag. Tondo,Manila");
System.out.println("09129694078");
System.out.println("roseannestorninos@yahoo.com");
System.out.println("nObjective: To obtain a position in a stable work enviroment with growth
potential.");
System.out.println("ntEDUCATIONAL BACKGROUND:n");
System.out.println("Primary: Gregoria De Jesus Elementary Schooln");
System.out.println("Secondary: Jose P. Laurel High Schooln");
System.out.println("Tertiary: Universidad De Manila") }}
EXERCISE#2
3. package javaapplication14;
importjava.util.Scanner;
public class JavaApplication14 {
public static void main(String[] args) {
Scanner epal = new Scanner (System.in);
float grade;
System.out.println("input your grade:");
grade = epal.nextFloat();
if ((grade<=100)&&(grade>=75))
{
System.out.println("Pass"); }
else if ((grade<75)&&(grade>=50))
{ System.out.println("Fail");
}
else
{
System.out.println("Input Error!"); }
}
}
EXERCISE #3
4. package javaapplication19;
importjava.util.Scanner;
public class JavaApplication19 {
public static void main(String[] args) {
Scanner input = new Scanner(
System.out.println("Quiz #1:"); //quizzes
int q1 = input.nextInt();
System.out.println("Quiz #2:");
int q2 = input.nextInt();
System.out.println("Quiz #3:");
int q3 = input.nextInt();
doubleavgm = (q1 + q2 + q3) / 3;
System.out.println("Midterm Exam:"); // midterm exam
intmidterme = input.nextInt();
doublemidtermg = (midterme*0.6)+(avgm*0.4); //midterm grade
System.out.println("Quiz #4:");
int q4 = input.nextInt();
System.out.println("Quiz #5:");
int q5 = input.nextInt();
doubleavgf = (q4 + q5) / 2;
System.out.println("Final Exam:"); //Final exam
intfinalse = input.nextInt();
doublefinalsg = (finalse*0.6)+(avgf*0.4);
doublefg = (finalsg*0.6)+(midtermg*.4);
System.out.println("Your Midterm grade is: " + midtermg);
5. System.out.println("Your Finals grade is : " + finalsg);
System.out.println("Your Final grade is : " + fg);}}
EXERCISE #4
package javaapplication15;
importjavax.swing.JOptionPane;
public class JavaApplication15 {
public static void main(String[] args) {
String name;
String msg;
name = JOptionPane.showInputDialog("Enter Your Name:");
msg = "Hello " + name + "!!";
JOptionPane.showMessageDialog(null, msg); }
}
6. EXERCISE #5.1
package javaapplication16;
importjava.io.BufferedReader;
importjava.io.InputStreamReader;
importjava.io.IOException;
public class JavaApplication16 {
public static void main(String[] args) throws Exception {
InputStreamReader g1 = new InputStreamReader( System.in);
BufferedReadergd = new BufferedReader(g1);
BufferedReaderdataIn = new BufferedReader(new InputStreamReader( System.in) );
String name = "";
System.out.print("nntt Please Enter Your Name:");
try{
name = dataIn.readLine();
}catch( IOException e ){
System.out.println("Error!");}
System.out.println("nntt Hello " + name +"!");
System.out.println("ntt^_^Lets Compute the Average of your Grade ^_^");
System.out.print("n n t t Enter your 1st grade: ");
String grade1 = gd.readLine();
7. System.out.print("t t Enter your 2nd grade: ");
String grade2 = gd.readLine();
System.out.print("t t Enter your 3rd grade: ");
String grade3 = gd.readLine();
intgt = Integer.parseInt(grade1)+Integer.parseInt(grade2)+Integer.parseInt(grade3), ave;
ave = gt/3;
System.out.println("t t Your Average is: " + ave);
if (ave>=60) {
System.out.println("t:)");}
else{
System.out.println("t:(");}
}
}
EXERCISE #5.2
package javaapplication20;
8. importjava.util.Scanner;
importjava.text.*;
public class JavaApplication20 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
DecimalFormat two = new DecimalFormat("$0.00");
String line = "";
char again = 'Y';
while (again == 'Y' || again == 'y'){
System.out.println("Enter Account Balance: ");
double balance = input.nextDouble();
double annual = balance;
double monthly = balance;
double daily = balance;
System.out.println("Enter Interest Rate(%): ");
double interest = input.nextDouble()/100;
int count = 1;
while (count <= 10){
annual = annual*(1+interest);
int month = 0;
while (month < 12){
monthly = monthly*(1+(interest/12));
month++;}
int day = 0;
while (day < 365){
daily = daily*(1+(interest/365));
9. day++; }
count++;}
System.out.println("Your interest in 10 years");
System.out.println("Your new balance when compounded annually: " + two.format(annual));
System.out.println("Your new balance when compounded monthly: " + two.format(monthly));
System.out.println("Your new balance when compounded daily: " + two.format(daily));
System.out.println("Again? (Y/N)");
line = input.next();
again = line.charAt(0); }}
}
EXERCISE #5.3
package javaapplication18;
importjava.io.BufferedReader;
importjava.io.IOException;
10. importjava.io.InputStreamReader;
importjava.util.Scanner;
public class JavaApplication18 {
public static void main(String[] args) {
BufferedReaderdataIn = new BufferedReader(new InputStreamReader( System.in) );
String name = "";
System.out.print(" Please Enter Your Name:");
try{
name = dataIn.readLine();
}catch( IOException e ){
System.out.println("Error!");}
System.out.println(" Hello " + name +"!");
System.out.println("^_^Lets determine your Zodiac Sign ^_^");
String zs = "";
String mon = "";
Scanner input = new Scanner(System.in);
System.out.println(" Enter month(1-12): ");
int m = input.nextInt();
System.out.println("Enter day(1-31): ");
int d = input.nextInt();
if((m== 1) && (d <= 19) || (m == 12) && (d >= 22)) {
zs = "Capricorn"; }
else if((m == 2) && (d <= 18) || (m == 1) && (d >= 20)) {
zs = "Aquarius"; }
else if((m == 3) && (d <= 20) || (m == 2) && (d >= 19)) {
zs = "Pisces"; }
12. case 8:mon = "August";break;
case 9:mon = "September";break;
case 10:mon = "October";break;
case 11:mon = "November";break;
case 12:mon = "December";break;}
System.out.println("Zodiac sign for " + mon + " " + d + " is " + zs);
}
}
Exercise #6.1
package oopexercise6.pkg1;
importjava.util.Scanner;
public class OOPExercise61 {
public static void main(String[] args) {
int[] binary = new int[100];
int[] octal = new int[100];
13. char[] hexa = new char[100];
int i=1;
int initial;
Scanner input = new Scanner(System.in);
System.out.print("Enter Decimal Value: ");
int decimal = input.nextInt();
//Choices
System.out.println("Convert to ");
System.out.println("[1] Binaryt[2] Octalt[3] Hexadecimal");
System.out.print(">>> ");
int choice = input.nextInt();
int quotient = decimal;
//equivalent in binary
if (choice ==1){
while(quotient!=0){
binary[i++] = quotient%2;
quotient = quotient/2;}
System.out.print("Equivalent of " + decimal + " in Binary is ");
for(int j=i-1; j>0; j--){
System.out.print(binary[j]);}
System.out.println("");}
//equivalent in octal
else if (choice == 2){
while(quotient!=0){
octal[i++] = quotient%8;
quotient = quotient/8;}
14. System.out.print("Equivalent of " + decimal + " in Octal is ");
for(int j = i-1 ; j> 0; j--){
System.out.print(octal[j]);}
System.out.println("");}
//equivalent in hexadecimal
else if (choice == 3){
while(quotient!=0){
initial = quotient % 16;
if( initial < 10) //to convert
initial =initial + 48;
else
initial = initial + 55;
hexa[i++] = (char) initial;
quotient = quotient / 16;}
System.out.print("Equivalent of " + decimal + " in Hexadecimal is ");
for(int j = i -1 ;j> 0;j--){
System.out.print(hexa[j]);}
System.out.println("");}
else{System.out.println("!!!Invalid Entry!!!"); }
}
}
15. Exercise #6.2
package oopexercise6.pkg2;
importjava.util.Scanner;
importjava.text.DecimalFormat;
public class OOPExercise62 {
public static void main(String[] args) {
int total = 0;
Scanner input = new Scanner(System.in);
System.out.print("The number of integers that you will enter is:");
int in = input.nextInt();
int array[] = new int[in];
float percent[] = new float[in];
System.out.println("Enter " + in + " integers, one per line:" );
for (int i=0; i<array.length; i++) {
16. array[i] = input.nextInt();
percent[i] = array[i]*100.0f;
total = total + array[i];}
System.out.println("The total is " + total +".");
DecimalFormatdf = new DecimalFormat("####.##");
System.out.println("The numbers are:");
for (int j=0; j<array.length ; j++){
System.out.println(array[j] + " which is " + df.format(percent[j]/total) + "% of the total.");
}
}
}
18. Introduction (Exercise 1)
1. What is your understanding about System.out.println()?
System.out.println() is like printf in C++ it just print out what is inside the “”, ln is use to
determine that you need to move in next line.
2. How does the Java Technology platform improve on other language platforms?
The Java Technology platform improves on other language platforms in terms of
flexibility because you can use different kinds of programming language on.
Flow Controls (Exercise 5)
1. What is the significance of using control structures?
The control structure is used in making decisions, choosing in different conditions that
gives different output.
2. For you, which is preferably the most convenient control structure to be used in comparisons, IFELSE or SWITCH?
For the most convenient control structure to be used in comparisons is switch.
Array (Exercise 6)
1. Why is it necessary, being a programmer, to use arrays?. What are the risks of not using arrays?
As a programmer it is necessary to use array because it shorten the program and lessen
the chance of error in assigning a variable. The risks of not using arrays are you
will make a too
long program and there is a high chance of error in assigning variable.
2. When do usually single dimensional array is used?
Single dimensional array is used in assigning a variable like students name,
teachers name or any records.