Sei sulla pagina 1di 41

MACHINE PROBLEMS IN JAVA COMPILATION

Our Lady of Fatima University


23 Km Sumulong Highway Brgy. Sta Cruz
Antipolo City
College of Computer Studies





Submitted to:
Mr. Jayson G. Mauricio

Submitted By:
Evarola, Arrah Cristelle T.
BSIT 2Y2-1

MACHINE PROBLEMS IN JAVA COMPILATION
Prelim: Machine Problem No.1 Displaying Information
Codes:
import java.io.*;
import javax.swing.JOptionPane;

public class Prelim_MP1{
public static void main(String []args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("\n\t\tOur Lady Of Fatima University");
System.out.print("\n\t\t\t Antipolo City");
System.out.print("\n\t\t COLLEGE OF COMPUTER STUDIES");

String name="";
String age="";
String birthday="";
String address="";
String school="";
String mother="";
String father="";

System.out.print("\n\t ----------------------------------------");
System.out.print("\n\nEnter your name: ");
name = br.readLine();
System.out.print("Enter your age: ");
age = br.readLine();
System.out.print("Enter your birthday: ");
birthday = br.readLine();
System.out.print("Enter your address: ");
address = br.readLine();
System.out.print("Enter your school: ");
school = br.readLine();
System.out.print("Enter your mother's name: ");
mother = br.readLine();
System.out.print("Enter your father's name: ");
father = br.readLine();

System.out.print("\n\n\tMy Name is " + name + ". I am " + age + " years old and my birthday is on " +
birthday + ". I lived in " + address + " and studying at " + school + ". My parents's are " + father + " and " + mother
+".\n\n");
}
}


MACHINE PROBLEMS IN JAVA COMPILATION
Prelim: Machine Problem No.1 Displaying Information
Output:
























MACHINE PROBLEMS IN JAVA COMPILATION
Prelim: Machine Problem No.2 Displaying with JOptionPane
Codes:
import java.io.*;
import javax.swing.JOptionPane;

public class Prelim_MP2{
public static void main(String[]args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("\n\t\tOur Lady Of Fatima University");
System.out.print("\n\t\t\t Antipolo City");
System.out.print("\n\t\t COLLEGE OF COMPUTER STUDIES");

System.out.print("\n\n\t\t PERSONAL INFORMATION");
String name="";
String age="";
String bday="";
String address="";
String school="";
String fder="";
String mder="";

System.out.print("\n");
System.out.print("\n");
System.out.print("\n\tName: ");
name=br.readLine();
System.out.print("\tAge: ");
age=br.readLine();
System.out.print("\tBirthday: ");
bday=br.readLine();
System.out.print("\tAddress: ");
address=br.readLine();
System.out.print("\tSchool: ");
school=br.readLine();
System.out.print("\tName of your Father: ");
fder=br.readLine();
System.out.print("\tName of your Mother: ");
mder=br.readLine();

JOptionPane.showMessageDialog(null, "\t Ako si " +name + ", " + " " + age + " taong gulang.\n" + "
Ipinanganak ako noong " + bday + ".\n" + "Nakatira ako sa " + address + ".\n\n" + "\t Ako ay nag-aaral sa " +
school + ".\n" + "Ang magulang ko ay sina " + fder + "\nat " + mder + ".");
}
}
MACHINE PROBLEMS IN JAVA COMPILATION
Prelim: Machine Problem No.2 Displaying with JOptionPane
Output:


















MACHINE PROBLEMS IN JAVA COMPILATION
Prelim: Machine Problem No.3 Grade Computation
Codes:
import java.io.*;
import javax.swing.JOptionPane;
import java.io.IOException;
import java.io.BufferedReader;
import java.text.DecimalFormat;

public class Prelim_MP3{
public static void main(String[]args)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int studnum=0;
String fname="";
String mname="";
String lname="";
String course="";
String section="";
float prelim_grade=0;
float midterm_grade=0;
float final_grade=0;
float class_standing=0;
float attendance=0;

System.out.print("\n\n\t\tGrading System");
System.out.print("\n===================================================");
System.out.print("\n\nStudent Number: ");
studnum = Integer.parseInt(br.readLine());
System.out.print("First Name: ");
fname= br.readLine();
System.out.print("Middle Initial: ");
mname= br.readLine();
System.out.print("Last Name: ");
lname= br.readLine();
System.out.print("Course: ");
course= br.readLine();
System.out.print("Section: ");
section= br.readLine();
System.out.print("\n\t -------------------------------");
System.out.print("\n\n\t\tPrelim Exam: ");
prelim_grade = Float.parseFloat(br.readLine());
System.out.print("\n\t\tMidterm Exam: ");
midterm_grade = Float.parseFloat(br.readLine());
System.out.print("\n\t\tFinal Exam: ");
MACHINE PROBLEMS IN JAVA COMPILATION
final_grade = Float.parseFloat(br.readLine());
System.out.print("\n\t\tClass Standing: ");
class_standing = Float.parseFloat(br.readLine());
System.out.print("\n\t\tattendancedance: ");
attendance = Float.parseFloat(br.readLine());

DecimalFormat format1=new DecimalFormat("0.00");
double finalgrade = 0;
finalgrade=((prelim_grade * 0.2) + (midterm_grade * 0.20) + (final_grade * 0.25) +(class_standing * 0.3) +
(attendance * 0.05));
JOptionPane.showMessageDialog(null, "\t"+studnum +"\n" +lname +", " +fname +", " +mname +"\n"
+course +" " +section +"\n\n\tFinal Grade: " +format1.format(finalgrade));

if (finalgrade>=101)
JOptionPane.showMessageDialog(null, "INVALID GRADE!!!","Message",JOptionPane.ERROR_MESSAGE);
else if(finalgrade>=98)
JOptionPane.showMessageDialog(null, "RATING: 1.00","Message",JOptionPane.INFORMATION_MESSAGE);
else if (finalgrade>=95)
JOptionPane.showMessageDialog(null, "RATING: 1.25","Message",JOptionPane.INFORMATION_MESSAGE);
else if (finalgrade>=92)
JOptionPane.showMessageDialog(null, "RATING: 1.50","Message",JOptionPane.INFORMATION_MESSAGE);
else if (finalgrade>=89)
JOptionPane.showMessageDialog(null, "RATING: 1.75","Message",JOptionPane.INFORMATION_MESSAGE);
else if (finalgrade>=86)
JOptionPane.showMessageDialog(null, "RATING: 2.00","Message",JOptionPane.INFORMATION_MESSAGE);
else if (finalgrade>=83)
JOptionPane.showMessageDialog(null, "RATING: 2.25","Message",JOptionPane.INFORMATION_MESSAGE);
else if (finalgrade>=80)
JOptionPane.showMessageDialog(null, "RATING: 2.50","Message",JOptionPane.INFORMATION_MESSAGE);
else if (finalgrade>=77)
JOptionPane.showMessageDialog(null, "RATING: 2.75","Message",JOptionPane.INFORMATION_MESSAGE);
else if (finalgrade>=75)
JOptionPane.showMessageDialog(null, "RATING: 3.00","Message",JOptionPane.INFORMATION_MESSAGE);
else
JOptionPane.showMessageDialog(null, "RATING: 5.00","Message",JOptionPane.WARNING_MESSAGE);
}
}









MACHINE PROBLEMS IN JAVA COMPILATION
Prelim: Machine Problem No.3 Grade Computation
Output:
























MACHINE PROBLEMS IN JAVA COMPILATION
Prelim: Machine Problem No.4 Enrollment System
Codes:
import java.io.*;
import javax.swing.JOptionPane;
import java.io.IOException;
import java.io.BufferedReader;
import javax.swing.*;
import java.text.DecimalFormat;
public class Prelim_MP4{
public static void main(String[]args)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int studnum = 0;
String name = "";
String course = "";
int year_level = 0;
String answer="";
int it1=10000, it2=12000, it3=13000, it4=15000;
int hrm1 = 8000, hrm2=9000, hrm3=9500, hrm4=12000;
int bsn1=15000, bsn2=16000, bsn3=16500, bsn4=17200;
int ba1 = 9000, ba2 =9500, ba3=10000, ba4=10500;
int medtech1=14000, medtech2=14300, medtech3=14800, medtech4=15000;
int psych1=7000, psych2=7500, psych3=9000, psych4=9500;
int pharma1=13000, pharma2=13500, pharma3=14000, pharma4=15000;
int pt1=10000, pt2=12000, pt3=12500, pt4=13000;

JOptionPane.showMessageDialog(null, " Our Lady of Fatima University \n Antipolo Campus
\n\n Enrollment System","Message",JOptionPane.INFORMATION_MESSAGE);
System.out.print("\nStudent number: ");
studnum = Integer.parseInt(br.readLine());
System.out.print("Name: ");
name= br.readLine();
System.out.print("Year level [1-4]: ");
year_level= Integer.parseInt(br.readLine());
do{
System.out.print("\n---------------------------------------------------------------");
System.out.print("\n\n\t\tCOURSE OFFERED");
System.out.print("\n\ta|A - BS Information Technology");
System.out.print("\n\tb|B - BS Hotel and Restaurant Management");
System.out.print("\n\tc|C - BS Nursing");
System.out.print("\n\td|D - BS Business Administration");
System.out.print("\n\te|E - BS Business Administration");
System.out.print("\n\tf|F - BS Medical Technology");
System.out.print("\n\tg|G - BS Psychology");
MACHINE PROBLEMS IN JAVA COMPILATION
System.out.print("\n\th|H - BS Pharmacy");
System.out.print("\n\tj|J - BS Physical Therapy");
System.out.print("\n\n\t Choose Course: ");

course = br.readLine();
char course2=course.charAt(0);
System.out.print("\n\n\n\t" +studnum);
String name2=name.toUpperCase();
System.out.print("\n\t" +name2);
DecimalFormat format1=new DecimalFormat("0.00");

if (year_level==1){
System.out.print("\n\tFIRST YEAR! - ");
switch(course2){
case 'a':
case 'A': System.out.print("BS Information Technology");
System.out.print("\n\n\tTUITION FEE: "+format1.format(it1)); break;
case 'b':
case 'B': System.out.print("BS Hotel and Restaurant Mngt");
System.out.print("\n\n\tTUITION FEE: "+format1.format(hrm1)); break;
case 'c':
case 'C': System.out.print("BS Nursing");
System.out.print("\n\n\tTUITION FEE: "+format1.format(bsn1)); break;
case 'd':
case 'D': System.out.print("BS BusinesseAd");
System.out.print("\n\n\tTUITION FEE: "+format1.format(ba1)); break;
case 'e':
case 'E': System.out.print("BS Med. Tech.");
System.out.print("\n\n\tTUITION FEE: "+format1.format(medtech1)); break;
case 'f':
case 'F': System.out.print("BS Psychology");
System.out.print("\n\n\tTUITION FEE: "+format1.format(psych1)); break;
case 'g':
case 'G': System.out.print("BSPH");
System.out.print("\n\n\tTUITION FEE: "+format1.format(pharma1)); break;
case 'h':
case 'H': System.out.print("BSPT");
System.out.print("\n\n\tTUITION FEE: "+format1.format(pt1)); break;
default: System.out.println("invalid input");
}
}

else if (year_level==2){
System.out.print("\n\tSECOND YEAR! - ");
switch(course2){
case 'a':
MACHINE PROBLEMS IN JAVA COMPILATION
case 'A': System.out.print("BS Information Technology");
System.out.print("\n\n\tTUITION FEE: "+format1.format(it2)); break;
case 'b':
case 'B': System.out.print("BS Hotel and Restaurant Mngt");
System.out.print("\n\n\tTUITION FEE: "+format1.format(hrm2)); break;
case 'c':
case 'C': System.out.print("BS Nursing");
System.out.print("\n\n\tTUITION FEE: "+format1.format(bsn2)); break;
case 'd':
case 'D': System.out.print("BS BusinesseAd");
System.out.print("\n\n\tTUITION FEE: "+format1.format(ba2)); break;
case 'e':
case 'E': System.out.print("BS Med. Tech.");
System.out.print("\n\n\tTUITION FEE: "+format1.format(medtech2)); break;
case 'f':
case 'F': System.out.print("BS Psychology");
System.out.print("\n\n\tTUITION FEE: "+format1.format(psych2)); break;
case 'g':
case 'G': System.out.print("BSPH");
System.out.print("\n\n\tTUITION FEE: "+format1.format(pharma2)); break;
case 'h':
case 'H': System.out.print("BSPT");
System.out.print("\n\n\tTUITION FEE: "+format1.format(pt2)); break;
default: System.out.println("invalid input");
}
}
else if (year_level==3){
System.out.print("\n\tTHIRD YEAR! - ");
switch(course2){
case 'a':
case 'A': System.out.print("BS Information Technology");
System.out.print("\n\n\tTUITION FEE: "+format1.format(it3)); break;
case 'b':
case 'B': System.out.print("BS Hotel and Restaurant Mngt");
System.out.print("\n\n\tTUITION FEE: "+format1.format(hrm2)); break;
case 'c':
case 'C': System.out.print("BS Nursing");
System.out.print("\n\n\tTUITION FEE: "+format1.format(bsn3)); break;
case 'd':
case 'D': System.out.print("BS BusinesseAd");
System.out.print("\n\n\tTUITION FEE: "+format1.format(ba2)); break;
case 'e':
case 'E': System.out.print("BS Med. Tech.");
System.out.print("\n\n\tTUITION FEE: "+format1.format(medtech3)); break;
case 'f':
case 'F': System.out.print("BS Psychology");
MACHINE PROBLEMS IN JAVA COMPILATION
System.out.print("\n\n\tTUITION FEE: "+format1.format(psych2)); break;
case 'g':
case 'G': System.out.print("BSPH");
System.out.print("\n\n\tTUITION FEE: "+format1.format(pharma3)); break;
case 'h':
case 'H': System.out.print("BSPT");
System.out.print("\n\n\tTUITION FEE: "+format1.format(pt2)); break;
default: System.out.println("invalid input");
}
}

else if (year_level==4){
System.out.print("\n\tFOURTH YEAR! - ");
switch(course2){
case 'a':
case 'A': System.out.print("BS Information Technology");
System.out.print("\n\n\tTUITION FEE: "+format1.format(it4)); break;
case 'b':
case 'B': System.out.print("BS Hotel and Restaurant Mngt");
System.out.print("\n\n\tTUITION FEE: "+format1.format(hrm4)); break;
case 'c':
case 'C': System.out.print("BS Nursing");
System.out.print("\n\n\tTUITION FEE: "+format1.format(bsn4)); break;
case 'd':
case 'D': System.out.print("BS BusinesseAd");
System.out.print("\n\n\tTUITION FEE: "+format1.format(ba4)); break;
case 'e':
case 'E': System.out.print("BS Med. Tech.");
System.out.print("\n\n\tTUITION FEE: "+format1.format(medtech4)); break;
case 'f':
case 'F': System.out.print("BS Psychology");
System.out.print("\n\n\tTUITION FEE: "+format1.format(psych4)); break;
case 'g':
case 'G': System.out.print("BSPH");
System.out.print("\n\n\tTUITION FEE: "+format1.format(pharma4)); break;
case 'h':
case 'H': System.out.print("BSPT");
System.out.print("\n\n\tTUITION FEE: "+format1.format(pt4)); break;
default: System.out.println("invalid input");
}
}
else{ System.out.print("\n\tINVALID!!"); }
answer = JOptionPane.showInputDialog(null,"Do youwant to try again?[yes/no]:");
} while(answer.equalsIgnoreCase("YES"));
} }

MACHINE PROBLEMS IN JAVA COMPILATION
Prelim: Machine Problem No.4 Enrollment System
Output:
























MACHINE PROBLEMS IN JAVA COMPILATION
Midterm: Machine Problem No.1 Quiz Array
Codes:
import java.io.*;
import javax.swing.JOptionPane;
import java.io.IOException;
import java.io.BufferedReader;

public class Midterm_MP1{
public static void main(String args[])throws IOException{
String [][] q;
q = new String[10][5];
int score=0;
//#1
q[0][0]="One of the first projects developed using Java was..";
q[0][1]="[A] Java";
q[0][2]="[B] Star 7";
q[0][3]="[C] Oak";
q[0][4]="B";
//#2
q[1][0]="Who created Java?";
q[1][1]="[A] James Gosling";
q[1][2]="[B] Dennis Richie";
q[1][3]="[C] Sun Micro Systems";
q[1][4]="A";
//#3
q[2][0]=" Java was created in..";
q[2][1]="[A] 1990";
q[2][2]="[B] 1901";
q[2][3]="[C] 1992";
q[2][4]="B";
//#4
q[3][0]="It was the first name of Java but soon changed because there was already a language called..";
q[3][1]="[A] Oak";
q[3][2]="[B] JRE";
q[3][3]="[C] Java Mobile";
q[3][4]="A";
//#5
q[4][0]="Java can create all kinds of applications that you could create using any conventional programming
language .";
q[4][1]="[A] True";
q[4][2]="[B] False";
q[4][3]="[C] I dont know.";
q[4][4]="A";
MACHINE PROBLEMS IN JAVA COMPILATION
//#6
q[5][0]="It is a list of data items that all have the same type and the same name.";
q[5][1]="[A] Integer";
q[5][2]="[B] Variable";
q[5][3]="[C] Array";
q[5][4]="C";
//#7
q[6][0]="Variables containing an adress to an object in memory.";
q[6][1]="[A] Object";
q[6][2]="[B] Object Variable";
q[6][3]="[C] Object Reference Variable";
q[6][4]="C";
//#8
q[7][0]=" Meaning of AWT.";
q[7][1]="[A] Abstract Window Toolkit";
q[7][2]="[B] Abstract Windowing Toolkit";
q[7][3]="[C] Actual Window Toolkit";
q[7][4]="B";
//#9
q[8][0]="Package in AWT that is used to handle events generated by the AWT components .";
q[8][1]="[A] java.awt.events";
q[8][2]="[B] java.awt.event_action";
q[8][3]="[C] java.awt.event";
q[8][4]="C";
//#10
q[9][0]="Most frequently used container class.";
q[9][1]="[A] Container";
q[9][2]="[B] Panel";
q[9][3]="[C] Components";
q[9][4]="B";

BufferedReader x = new BufferedReader(new InputStreamReader(System.in));
String answer = new String();
System.out.println();
System.out.println("CS315 Java Test");
System.out.println();
for (int i=0; i<10; ++i){
System.out.print((i+1)+".) ");
answer = JOptionPane.showInputDialog(null,q[i][0] +"\n"+ q[i][1] +"\n"+q[i][2] +"\n"+q[i][3] +"\n"+"\nAnswer: ");
if (answer.equalsIgnoreCase(q[i][4])){
System.out.println("You are Correct!"); score+=1; }
else
System.out.println("Wrong! The correct answer is : " + q[i][4]);
System.out.println(); }
System.out.println();
System.out.println("Your Score is: "+score+" point/s"); } }
MACHINE PROBLEMS IN JAVA COMPILATION
Midterm: Machine Problem No.1 Quiz Array
Output:

















MACHINE PROBLEMS IN JAVA COMPILATION
Midterm: Machine Problem No.2 Vowels and Consonants
Codes:
import java.io.*;
public class Midterm_MP2{
public static void main(String args[])throws IOException{
String[] names;
int count[];
names = new String[5];
count = new int[5];

for (int i=0; i<count.length; ++i)
count[i]=0;

BufferedReader x = new BufferedReader(new InputStreamReader(System.in));
System.out.print(\n);
for(int i=0; i<names.length; ++i)
{
System.out.print("Enter Name for No."+(i+1)+": ");
names[i] = x.readLine();
names[i]= names[i].toUpperCase();
}

for (int i=0; i<names.length; ++i)
{
for (int j=0; j<names[i].length(); ++j)
{
char r = names[i].charAt(j);
switch (r)
{
case 'A': count[i]=count[i]+1;break;
case 'E': count[i]=count[i]+1;break;
case 'I': count[i]=count[i]+1;break;
case 'O': count[i]=count[i]+1;break;
case 'U': count[i]=count[i]+1;break;
default : break;
}
}
}
System.out.println("\n");
for (int i=0; i<names.length; ++i)
System.out.println("Number of Vowels of " + names[i] + " is : " + count[i]);
}
}
MACHINE PROBLEMS IN JAVA COMPILATION
Midterm: Machine Problem No.2 Vowels and Consonants
Output:
























MACHINE PROBLEMS IN JAVA COMPILATION
Midterm: Machine Problem No.3 Jack n Poy
Codes:
import java.io.*;
import java.util.*;
import java.io.InputStreamReader;
import javax.swing.JOptionPane;

public class Midterm_MP3{
public static void main (String [] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

double num = 0;
int point1 = 0;
int point2 = 0;
JOptionPane.showMessageDialog(null," WELCOME TO jack n' Poy!!\n "+args[0]+" and "+args[1]);
System.out.println("\nChoices:\n\t 0 - ROCK\n\t 1 - PAPER\n\t 2 - SCISSORS\n");
Random y = new Random();
for (int i = 1; i<=3 ; i++){
JOptionPane.showMessageDialog(null," ROUND"+ i );
System.out.print(args[0]+" : ");
num = Double.parseDouble(br.readLine());
float x = y.nextFloat();
int m = Math.round(2*x);
System.out.println(" "+ args[1] +" - " + m+ "\n");
System.out.println(" 0 - ROCK\n 1 - PAPER\n 2 - SCISSORS\n");

if ((num==0) && (m==1)) {
point2+=1;
JOptionPane.showMessageDialog(null,""+args[1]+" WIN"); }

else if((num==0) && (m==2)) {
point1+=1;
JOptionPane.showMessageDialog(null,"YOU WIN"); }

else if((num==1) && (m==0)) {
point1+=1;
JOptionPane.showMessageDialog(null,"YOU WIN"); }

else if((num==1) && (m==2)) {
point2+=1;
JOptionPane.showMessageDialog(null,args[1]+" WIN"); }


MACHINE PROBLEMS IN JAVA COMPILATION
else if((num==2) && (m==0)) {
point2+=1;
JOptionPane.showMessageDialog(null,args[1]+" WIN"); }
else if((num==2) && (m==1)) {
point1+=1;
JOptionPane.showMessageDialog(null,"YOU WIN"); }
else {
point1+=0;
JOptionPane.showMessageDialog(null,"DRAW"); }
}

if (point1>point2){
JOptionPane.showMessageDialog(null," YOU WIN \n SCORE \n "+args[0]+" - "+ point1 + "\n
"+args[1]+" - "+ point2 );
JOptionPane.showMessageDialog(null," YOU WIN \n GAME OVER" ); }
else if (point1<point2) {
JOptionPane.showMessageDialog(null," YOU LOSE \n SCORE \n "+args[0]+" - "+ point1 + "\n
"+args[1]+" - "+ point2 );
JOptionPane.showMessageDialog(null," YOU LOSE \n GAME OVER" ); }
else {
JOptionPane.showMessageDialog(null," DRAW \n SCORE \n "+args[0]+" - "+ point1 + "\n
"+args[1]+" - "+ point2 );
JOptionPane.showMessageDialog(null," YOU LOSE \n GAME OVER" ); }
}
static int min(int x, int y){
if(x<y)
return(x);
else
return(y);
}
}















MACHINE PROBLEMS IN JAVA COMPILATION
Midterm: Machine Problem No.3 Jack n Poy
Output:






















MACHINE PROBLEMS IN JAVA COMPILATION
Midterm: Machine Problem No.4 Elevator
Codes:
Midterm_MP4a.java

import java.io.*;
public class Midterm_MP4a {
public static void main(String args[]) throws IOException {
BufferedReader x = new BufferedReader(new InputStreamReader(System.in));
Midterm_MP4b myElevator = new Midterm_MP4b();

String COMMAND = "";
int goFloor = 0;
int LOOP = 0;

System.out.println("\n\n -JAVA ELEVATOR-");
System.out.println(" **********************");
System.out.println(" * | *");
System.out.println(" * ON/OFF *");
System.out.println(" * OPEN/CLOSE *");
System.out.println(" * 6 *");
System.out.println(" * 5 *");
System.out.println(" * 4 *");
System.out.println(" * 3 *");
System.out.println(" * 2 *");
System.out.println(" * G *");
System.out.println(" * | *");
System.out.println(" **********************");

do{
System.out.print("\nENTER CHOICE : ");
COMMAND = x.readLine();
while( (!COMMAND.equalsIgnoreCase("ON")) && (!COMMAND.equalsIgnoreCase("OFF")) &&
(!COMMAND.equalsIgnoreCase("OPEN")) && (!COMMAND.equalsIgnoreCase("CLOSE")) &&
(!COMMAND.equalsIgnoreCase("6")) && (!COMMAND.equalsIgnoreCase("5")) &&
(!COMMAND.equalsIgnoreCase("4")) && (!COMMAND.equalsIgnoreCase("3")) &&
(!COMMAND.equalsIgnoreCase("2")) && (!COMMAND.equalsIgnoreCase("G")) ){
System.out.println("INVALID CHOICE!");
System.out.print("\nENTER CHOICE : ");
COMMAND = x.readLine();
}



MACHINE PROBLEMS IN JAVA COMPILATION
if(COMMAND.equalsIgnoreCase("ON"))
myElevator.powerOn();
else if(COMMAND.equalsIgnoreCase("OFF"))
myElevator.powerOff();
else if(COMMAND.equalsIgnoreCase("OPEN"))
myElevator.doorOpen();
else if(COMMAND.equalsIgnoreCase("CLOSE"))
myElevator.doorClose();
else{
if(COMMAND.equalsIgnoreCase("G"))
goFloor = 1;
else
goFloor = Integer.parseInt(COMMAND);

if(goFloor < myElevator.currentFloor){
LOOP = myElevator.currentFloor - goFloor;
if((myElevator.powerOn == true) && (myElevator.doorOpen == false)){
for(int i = 1 ; i <= LOOP ; i++){
myElevator.goDown(); } }
else{
myElevator.goDown(); } }
else if(goFloor > myElevator.currentFloor){
LOOP = goFloor - myElevator.currentFloor;
if((myElevator.powerOn == true) && (myElevator.doorOpen == false)){
for(int i = 1 ; i <= LOOP ; i++){
myElevator.goUp(); } }
else{
myElevator.goDown(); } }
else
System.out.println("You are already in floor " + goFloor);}
}while(!COMMAND.equalsIgnoreCase("OFF"));
}
}













MACHINE PROBLEMS IN JAVA COMPILATION
Midterm: Machine Problem No.4 Elevator
Codes:
Midterm_MP4b.java

import java.io.*;
public class Midterm_MP4b {
public boolean powerOn = false;
public boolean doorOpen = false;
public int currentFloor = 1;
public int TOP_FLOOR = 6;
public int MIN_FLOOR = 1;

public void powerOn() {
if(powerOn == true){
System.out.println("Power is already turned off."); }
else{
System.out.println("Turning on the power, please wait");
powerOn = true;
System.out.println("Power is now turned on, Welcome");
System.out.println("Floor : " + currentFloor); } }
public void powerOff() {
System.out.println("Turning off the power, please wait");
powerOn = false;
System.out.println("Power is now turned off. Goodbye"); }
public void doorOpen() {
if(powerOn == true){
if(doorOpen == false){
doorOpen = true;
System.out.println("Door is now opened");
System.out.println("Floor : " + currentFloor); }
else{
System.out.println("Door is already opened");
System.out.println("Floor : " + currentFloor); } }
else{
System.out.println("Please turn on the power first"); } }
public void doorClose() {
if(powerOn == true){
if(doorOpen == true){
doorOpen = false;
System.out.println("\nDoor is now closed");
System.out.println("Floor : " + currentFloor); }
else{
System.out.println("Door is already closed..");
MACHINE PROBLEMS IN JAVA COMPILATION
System.out.println("Floor : " + currentFloor); } }
else{
System.out.println("\nPlease turn on the power first."); } }
public void goUp() {
if(powerOn == true){
if(doorOpen == false){
if(currentFloor > TOP_FLOOR){
System.out.println("INVALID FLOOR, PLEASE CHOOSE FROM 1/G - 6");}
else{
currentFloor++;
System.out.println("\nGoing up one floor");
System.out.println("Floor : " + currentFloor); } }
else{
System.out.println("\nPlease close the door first");
System.out.println("Floor : " + currentFloor); } }
else{
System.out.println("\nPlease turn on the power first");
System.out.println("Floor : " + currentFloor); } }
public void goDown() {
if(powerOn == true){
if(doorOpen == false){
if(currentFloor > TOP_FLOOR){
System.out.println("INVALID FLOOR, PLEASE CHOOSE FROM 1/G - 6"); }
else{
currentFloor--;
System.out.println("\nGoing down one floor");
System.out.println("Floor : " + currentFloor); } }
else{
System.out.println("\nPlease close the door first");
System.out.println("Floor : " + currentFloor); } }
else{
System.out.println("\nPlease turn on the power first");
System.out.println("Floor : " + currentFloor); } } }













MACHINE PROBLEMS IN JAVA COMPILATION
Midterm: Machine Problem No.4 Elevator
Output:








































MACHINE PROBLEMS IN JAVA COMPILATION
Finals: Machine Problem No.2 Frame
Codes:
import java.awt.*;
import java.io.*;

class myframe extends Frame{

myframe(String s,int x,int y,int a, int b,Color mycolor,boolean k){
super(s);
setLayout(new GridLayout(3,2));
setBackground(mycolor);
setSize(a,b);
setSize(x,y);
setLocation(x,y);
setLocation(a,b);
setVisible(true);
setResizable(k);
}
}

class Finals_MP2{
public static void main(String args[]) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String title = "";
int x,y,w,h = 0;
String color = "";

String ans = "";
String set="";

do{
System.out.print("\nEnter title: ");
title = br.readLine();
System.out.print("Enter x axis: ");
x =Integer.parseInt(br.readLine());
System.out.print("Enter y axis: ");
y =Integer.parseInt(br.readLine());
System.out.print("Enter width: ");
w = Integer.parseInt(br.readLine());
System.out.print("Enter height: ");
h =Integer.parseInt(br.readLine());
System.out.print("Enter color [R|r - Red / b|B - Blue / p|P - Pink]: ");
MACHINE PROBLEMS IN JAVA COMPILATION
color = br.readLine();
System.out.print("Set Resizable? [true/no]: ");
set = br.readLine();

char color1=color.charAt(0);

if(set.equalsIgnoreCase("true")){
switch(color1){
case 'R':
case 'r': new myframe(title,w,h,x,y,Color.red,true);break;

case 'B':
case 'b': new myframe(title,w,h,x,y,Color.blue,true);break;

case 'P':
case 'p': new myframe(title,w,h,x,y,Color.pink,true);break;

default: System.out.print("Invalid Input.");
}
}
else{
switch(color1){
case 'R':
case 'r': new myframe(title,w,h,x,y,Color.red,false);break;

case 'B':
case 'b': new myframe(title,w,h,x,y,Color.blue,false);break;

case 'P':
case 'p': new myframe(title,w,h,x,y,Color.pink,false);break;

default: System.out.print("Invalid Input.");
}
}
System.out.print(" Do you want to process again? [yes/no]: ");

ans=br.readLine();
}while(ans.equalsIgnoreCase("yes"));
}
}





MACHINE PROBLEMS IN JAVA COMPILATION
Finals: Machine Problem No.2 Frame
Output:








































MACHINE PROBLEMS IN JAVA COMPILATION
Finals: Machine Problem No.4 Calculator
Codes:
import java.awt.*;

public class Finals_MP4 extends Frame{
Button b1,b2,b3,b4,b5,b6,b7,b8,b9;
TextField num1,num2,result;
int z = 0;
public Finals_MP4(String s){
setLayout(new GridLayout(2,1));
Panel p=new Panel();
p.setLayout(new GridLayout(3,2));
Label l1= new Label("First Number:");
Label l2= new Label("Second Number:");
Label l3= new Label("Result is:");
num1 = new TextField();
num2 = new TextField();
result = new TextField();
p.add(l1);
p.add(num1);
p.add(l2);
p.add(num2);
p.add(l3);
p.add(result);

Panel q=new Panel();
q.setLayout(new GridLayout(3,6));
b1 = new Button("+");
b2 = new Button("-");
b3 = new Button("*");
b4 = new Button("/");
b5 = new Button("%");
b6 = new Button("Average");
b7 = new Button("Clear");
b8 = new Button("^2");
b9 = new Button("^3");
q.add(b1);
q.add(b2);
q.add(b3);
q.add(b4);
q.add(b5);
q.add(b6);
q.add(b7);
MACHINE PROBLEMS IN JAVA COMPILATION
q.add(b8);
q.add(b9);
add(p);
add(q);
layout();
pack();
show();
}
public boolean action(Event e, Object o){
int x = Integer.parseInt(num1.getText());
int y = Integer.parseInt(num2.getText());
if("+".equals(e.arg)){
z=x+y; }
else if("-".equals(e.arg)){
z=x-y; }
else if("*".equals(e.arg)){
z=x*y; }
else if("/".equals(e.arg)){
z=x/y; }
else if("%".equals(e.arg)){
z=x%y; }
else if("Average".equals(e.arg)){
int z=((x+y)/2); }
else if("^2".equals(e.arg)){
z=x*x;
int v=y*y; }
else if("^3".equals(e.arg)){
int z=(x*x)*(y*y)*3; }
else{
num1.setText("");
num2.setText("");
result.setText(""); }

result.setText(z+"");
return(super.action(e,e.target));
}
public boolean handleEvent(Event evt){
if(evt.id == Event.WINDOW_DESTROY){
hide();
System.exit(0);
}
return(super.handleEvent(evt));
}
public static void main(String[] args){
new Finals_MP4("Event Example 3");
} }
MACHINE PROBLEMS IN JAVA COMPILATION
Finals: Machine Problem No.3 Calculator
Output:









































MACHINE PROBLEMS IN JAVA COMPILATION
Finals: Machine Problem No.3 Cellphone Interface
Codes:
import java.awt.*;
import java.awt.color.*;

public class Finals_MP3 extends Frame{
Label label1, label2, label3, label4;
TextField textfield1;
TextArea textarea1;
Button buttonA, buttonB, buttonC, buttonD, buttonE, buttonF, buttonG, buttonH, buttonI, buttonJ,
buttonK, buttonL, buttonM, buttonN, buttonO, buttonP, buttonQ, buttonR, buttonS, buttonT,
buttonU, buttonV, buttonW, buttonX, buttonY, buttonZ, buttonSEND;

public Finals_MP3(String s){
super(s);
setBackground(Color.gray);

Panel a=new Panel();
label1=new Label("GALAXY 4S");
setLayout(new BorderLayout());
setFont(new Font("Verdana",Font.BOLD,20));
a.add(label1);
add("North",a);

Panel g=new Panel();
label3=new Label("Message: ");
textarea1=new TextArea();
setLayout(new GridLayout(3,0));
setFont(new Font("Times New Roman",Font.BOLD,14));
g.add(label3);
g.add(textarea1);
add("Center",g);

Panel c=new Panel();
buttonQ=new Button("Q");
buttonW=new Button("W");
buttonE=new Button("E");
buttonR=new Button("R");
buttonT=new Button("T");
buttonY=new Button("Y");
buttonU=new Button("U");
buttonI=new Button("I");
buttonO=new Button("O");
MACHINE PROBLEMS IN JAVA COMPILATION
buttonP=new Button("P");
setLayout(new GridLayout(4,0));
setFont(new Font("Times New Roman",Font.BOLD,12));
c.add(buttonQ);
c.add(buttonW);
c.add(buttonE);
c.add(buttonR);
c.add(buttonT);
c.add(buttonY);
c.add(buttonU);
c.add(buttonI);
c.add(buttonO);
c.add(buttonP);
add("Center",c);

Panel d=new Panel();
buttonA=new Button("A");
buttonS=new Button("S");
buttonD=new Button("D");
buttonF=new Button("F");
buttonG=new Button("G");
buttonH=new Button("H");
buttonJ=new Button("J");
buttonK=new Button("K");
buttonL=new Button("L");
setLayout(new GridLayout(5,0));
setFont(new Font("Times New Roman",Font.BOLD,12));
d.add(buttonA);
d.add(buttonS);
d.add(buttonD);
d.add(buttonF);
d.add(buttonG);
d.add(buttonH);
d.add(buttonJ);
d.add(buttonK);
d.add(buttonL);
add("Center",d);

Panel e=new Panel();
buttonZ=new Button("Z");
buttonX=new Button("X");
buttonC=new Button("C");
buttonV=new Button("V");
buttonB=new Button("B");
buttonM=new Button("N");
buttonN=new Button("M");
MACHINE PROBLEMS IN JAVA COMPILATION
setLayout(new GridLayout(6,0));
setFont(new Font("Times New Roman",Font.BOLD,12));
e.add(buttonZ);
e.add(buttonX);
e.add(buttonC);
e.add(buttonV);
e.add(buttonB);
e.add(buttonN);
e.add(buttonM);
add("Center",e);

Panel f=new Panel();
label4=new Label("");
setLayout(new GridLayout(7,0));
setFont(new Font("Times New Roman",Font.BOLD,14));
f.add(label4);
add("Center",f);

Panel h = new Panel();
buttonSEND = new Button("SEND");
setLayout(new GridLayout(8,0));
setFont(new Font("Times New Roman",Font.BOLD,14));
f.add(buttonSEND);
f.add(buttonSEND);
add("South",f);

layout();
pack();
show();
}
}
public static void main(String []args){
new Finals_MP3("Cellphone");
}
}








MACHINE PROBLEMS IN JAVA COMPILATION
Finals: Machine Problem No.3 Cellphone Interface
Output:






MACHINE PROBLEMS IN JAVA COMPILATION
Finals: Machine Problem No.5 Reservation System
Codes:
import java.awt.*;
import java.awt.event.*;

class Finals extends Frame implements ActionListener{
Choice pass, destination, flight,req;
List list1;
Button b1, b2, b3,b4,b5;
Label name, no, des, type, total, add, request,price,label1,empty;
TextField cname;
double p, amount ,tot;
Finals(String s){
super(s);
setLayout(new BorderLayout());
setFont(new Font("Arial", Font.BOLD, 12));
setBackground(Color.gray);
setForeground(Color.black);

Label A = new Label("JAM AIRWAYS", Label.CENTER);
add("North",A);

Panel p1 = new Panel();
p1.setLayout(new GridLayout(11,2));
list1 = new List();
pass = new Choice();
name = new Label("Customer's Name:");

cname = new TextField ("");

no = new Label("No. of Pasengers: ");

pass.addItem("1");
pass.addItem("2");
pass.addItem("3");
pass.addItem("4");
pass.addItem("5");
pass.addItem("6");
pass.addItem("7");
pass.addItem("8");
pass.addItem("9");
pass.addItem("10");

MACHINE PROBLEMS IN JAVA COMPILATION
des = new Label ("Destination:");
destination = new Choice();
destination.addItem("Hong Kong");
destination.addItem("Singapore");
destination.addItem("Bangkok");
destination.addItem("Vancouver");

type = new Label("Type of Flight: ");

flight = new Choice();
flight.addItem("Business");
flight.addItem("Economy");

add = new Label("Addtional Request");

req = new Choice();
req.addItem("20kgs baggage ");
req.addItem("Breakfast meal");
req.addItem("NewsPaper");

label1 = new Label("");
Label Y = new Label("");
Label Z = new Label("");

list1 = new List(0,false);

b4 = new Button("Add Item");
b4.addActionListener(this);

b5 = new Button("Remove Item");
b5.addActionListener(this);

total = new Label ("Total Bill: ");

b2 = new Button("Reset");
b2.addActionListener(this);

price= new Label("");

b1 = new Button("Compute Bill");
b1.addActionListener(this);

empty = new Label("");

b3 = new Button("Exit");
b3.addActionListener(this);
MACHINE PROBLEMS IN JAVA COMPILATION

p1.add(name);
p1.add(cname);
p1.add(no);
p1.add(pass);
p1.add(des);
p1.add(destination);
p1.add(type);
p1.add(flight);
p1.add(add);
p1.add(req);
p1.add(label1);
p1.add(list1);

p1.add(b4);
p1.add(b5);
p1.add(Z);
p1.add(Y);
p1.add(total);
p1.add(b2);
p1.add(price);
p1.add(b1);
p1.add(empty);
p1.add(b3);

add("Center" , p1);

pack();
layout();
show();
}
public void actionPerformed(ActionEvent event){
String s1, s2;
if (event.getSource() == b4){
s1 = new String(req.getSelectedItem());
list1.addItem(s1);
}
else if (event.getSource() == b5){
list1.removeAll();
}
else if (event.getSource() == b2) {
pass.select(0);
destination.select(0);
flight.select(0);
req.select(0);
cname.setText("");
MACHINE PROBLEMS IN JAVA COMPILATION
price.setText("");
}
else if (event.getSource() == b1){
s2 = new String(pass.getSelectedItem());
p = Integer.parseInt(s2);
if (destination.getSelectedItem() == "Hong Kong"){
if(flight.getSelectedItem() == "Business"){
amount = 120;
}
else if(flight.getSelectedItem() == "Economy"){
amount = 100;
}
}
else if (destination.getSelectedItem() == "Singapore"){
if(flight.getSelectedItem() == "Business"){
amount = 180;
}
else if(flight.getSelectedItem() == "Economy"){
amount = 150;
}
}
else if (destination.getSelectedItem() == "Bangkok"){
if(flight.getSelectedItem() == "Business"){
amount = 270;
}
else if(flight.getSelectedItem() == "Economy"){
amount = 240;
}
}
else if (destination.getSelectedItem() == "Vancouver"){
if(flight.getSelectedItem() == "Business"){
amount = 530; }
else if(flight.getSelectedItem() == "Economy"){
amount = 450; } }
tot = amount * p;
price.setText("$ " + tot);
}
else if( event.getSource() == b3){
System.exit(0);
}
}
public class Finals_MP5 {
public static void main(String args[]){
new Finals("RESERVATION SYSTEM");
}
}
MACHINE PROBLEMS IN JAVA COMPILATION
Finals: Machine Problem No.5 Reservation System
Output:

Potrebbero piacerti anche