Miyerkules, Abril 4, 2012

JAVA


                                                      1

1.PROBLEM STATEMENT
Combo Meal
2. RELEVANT SOURCE CODE
import java.util.Scanner;
public class JavaApplication4 {


public static void main(String[] args) {

int num1,q1,additional,q2,amt,total,sum,total1;
Scanner a = new Scanner(System.in);

System.out.println(" Combo Meals");
System.out.println(" 1 - Combo1: Rice + Viand P35.00");
System.out.println(" 2 - Combo1: Rice + 2 Viands P55.00");
System.out.println(" 3 - Combo1: Rice + 2 Viands + Drinks P85.00");
System.out.println(" Additional");
System.out.println(" 4 - Rice P10.00");
System.out.println(" 5 - Viand(Any type) P25.00");
System.out.println(" 6 - Drink P20.00");
System.out.println(" 7 - Dessert P25.00");
System.out.println(" 8 - Payment");
System.out.println(" 9 - Exit");
System.out.print("please choose : ");
num1 = a.nextInt();

switch(num1){
case 1:
System.out.println("Combo1 : P35.00");
System.out.print("Quantity : ");
q1 = a.nextInt();
total = q1 * 35;
System.out.println("Your Bill is: "+"P"+total);
System.out.println("Additional Order");
System.out.println(" 4 - Rice P10.00");
System.out.println(" 5 - Viand(Any type) P25.00");
System.out.println(" 6 - Drink P20.00");
System.out.println(" 7 - Dessert P25.00");
System.out.print(" please choose : ");
additional = a.nextInt();
switch(additional){
case 4:

System.out.println("Rice - P10.00");
System.out.print("quantity : ");
q2 = a.nextInt();
total1 = q2 * 10;
sum = total + total1;
System.out.println("your total bill is : "+"P"+sum);

System.out.print("Enter Amount: ");
amt = a.nextInt();
if(amt<sum){
System.out.println("invalid");

}
if(amt >= sum){
int b = amt - sum;
System.out.println("your change is : P"+b+"\n\n\t Thank You ! Please come again ");
break;
}

case 5:
System.out.println("Viand(Any type) - P25.00");
System.out.print("quantity : ");
q2 = a.nextInt();
total1 = q2 * 25;
sum = total + total1;
System.out.println("your total bill is : "+"P"+sum);

System.out.print("Enter Amount: ");
amt = a.nextInt();
if(amt<sum){
System.out.println("invalid");

}
if(amt >= sum){
int b = amt - sum;
System.out.println("your change is : P"+b+"\n\n\t Thank You ! Please come again ");
break;
}

case 6: System.out.println("Drink - P20.00");
System.out.print("quantity : ");
q2 = a.nextInt();
total1 = q2 * 20;
sum = total + total1;
System.out.println("your total bill is : "+"P"+sum);

System.out.print("Enter Amount: ");
amt = a.nextInt();
if(amt<sum){
System.out.println("invalid");

}
if(amt >= sum){
int b = amt - sum;
System.out.println("your change is : P"+b+"\n\n\t Thank You ! Please come again ");
break;
}

case 7:
System.out.println("Dessert - P25.00");
System.out.print("quantity : ");
q2 = a.nextInt();
total1 = q2 * 25;
sum = total + total1;
System.out.println("your total bill is : "+"P"+sum);

System.out.print("Enter Amount: ");
amt = a.nextInt();
if(amt<sum){
System.out.println("invalid");

}
if(amt >= sum){
int b = amt - sum;
System.out.println("your change is : P"+b+"\n\n\t Thank You ! Please come again ");

}



}
break;
case 2:

System.out.println("Combo2 : P55.00");
System.out.print("Quantity : ");
q1 = a.nextInt();
total = q1 * 55;
System.out.println("Your Bill is :"+"P"+total);
System.out.println("Additional Order");
System.out.println(" 4 - Rice P10.00");
System.out.println(" 5 - Viand(Any type) P25.00");
System.out.println(" 6 - Drink P20.00");
System.out.println(" 7 - Dessert P25.00");
System.out.print(" please choose : ");

additional = a.nextInt();
switch(additional){
case 4:

System.out.println("Rice - P10.00");
System.out.print("quantity : ");
q2 = a.nextInt();
total1 = q2 * 10;
sum = total + total1;
System.out.println("your total bill is : "+"P"+sum);

System.out.print("Enter Amount: ");
amt = a.nextInt();
if(amt<sum){
System.out.println("invalid");

}
if(amt >= sum){
int b = amt - sum;
System.out.println("your change is : P"+b+"\n\n\t Thank You ! Please come again ");
break;
}
case 5:
System.out.println("Viand(Any type) - P25.00");
System.out.print("quantity : ");
q2 = a.nextInt();
total1 = q2 * 25;
sum = total + total1;
System.out.println("your total bill is : "+"P"+sum);

System.out.print("Enter Amount: ");
amt = a.nextInt();
if(amt<sum){
System.out.println("invalid");

}
if(amt >= sum){
int b = amt - sum;
System.out.println("your change is : P"+b+"\n\n\t Thank You ! Please come again ");
break;
}

case 6: System.out.println("Drink - P20.00");
System.out.print("quantity : ");
q2 = a.nextInt();
total1 = q2 * 20;
sum = total + total1;
System.out.println("your total bill is : "+"P"+sum);

System.out.print("Enter Amount: ");
amt = a.nextInt();
if(amt<sum){
System.out.println("invalid");

}
if(amt >= sum){
int b = amt - sum;
System.out.println("your change is : P"+b+"\n\n\t Thank You ! Please come again ");
break;
}

case 7:
System.out.println("Dessert - P25.00");
System.out.print("quantity : ");
q2 = a.nextInt();
total1 = q2 * 25;
sum = total + total1;
System.out.println("your total bill is : "+"P"+sum);

System.out.print("Enter Amount: ");
amt = a.nextInt();
if(amt<sum){
System.out.println("invalid");

}
if(amt >= sum){

// java applet//


int b = amt - sum;
System.out.println("your change is : P"+b+"\n\n\t Thank You ! Please come again ");

}



}
break;



case 3:

System.out.println("Combo3 : P85.00");
System.out.print("Quantity : ");
q1 = a.nextInt();
total = q1 * 85;
System.out.println("Your Bill is :"+"P"+total);
System.out.println("Additional Order");
System.out.println(" 4 - Rice P10.00");
System.out.println(" 5 - Viand(Any type) P25.00");
System.out.println(" 6 - Drink P20.00");
System.out.println(" 7 - Dessert P25.00");
System.out.print(" please choose : ");

additional = a.nextInt();
switch(additional){
case 4:

System.out.println("Rice - P10.00");
System.out.print("quantity :");
q2 = a.nextInt();
total1 = q2 * 10;
sum = total + total1;
System.out.println("your total bill is : "+"P"+sum);

System.out.print("Enter Amount: ");
amt = a.nextInt();
if(amt<sum){
System.out.println("invalid");

}
if(amt >= sum){
int b = amt - sum;
System.out.println("your change is : P"+b+"\n\n\t Thank You ! Please come again ");
break;
}
case 5:
System.out.println("Viand(Any type) - P25.00");
System.out.print("quantity : ");
q2 = a.nextInt();
total1 = q2 * 25;
sum = total + total1;
System.out.println("your total bill is : "+"P"+sum);

System.out.print("Enter Amount: ");
amt = a.nextInt();
if(amt<sum){
System.out.println("invalid");

}
if(amt >= sum){
int b = amt - sum;
System.out.println("your change is : P"+b+"\n\n\t Thank You ! Please come again ");
break;
}

case 6: System.out.println("Drink - P20.00");
System.out.print("quantity : ");
q2 = a.nextInt();
total1 = q2 * 20;
sum = total + total1;
System.out.println("your total bill is : "+"P"+sum);

System.out.print("Enter Amount: ");
amt = a.nextInt();
if(amt<sum){
System.out.println("invalid");

}
if(amt >= sum){
int b = amt - sum;
System.out.println("your change is : P"+b+"\n\n\t Thank You ! Please come again ");
break;
}

case 7:
System.out.println("Dessert - P25.00");
System.out.print("quantity : ");
q2 = a.nextInt();
total1 = q2 * 25;
sum = total + total1;
System.out.println("your total bill is : "+"P"+sum);

System.out.print("Enter Amount: ");
amt = a.nextInt();
if(amt<sum){
System.out.println("invalid");

}
if(amt >= sum){
int b = amt - sum;
System.out.println("your change is : P"+b+"\n\n\t Thank You ! Please come again ");

}
break;


}
}
}

}
3.SAMPLE OUTPUT

"Menu"
1.Combo1:Rice+Viand 35.00                                 Ex.Enter choice 1
2.Combo2:Rice+Viand 55.00                                        combo1:  35.00
3.Combo3:Rice+Viand+Drinks 85.00                            Quantity:  2
                                                                                       Additional Order:  2
"Additional"                                                                     Quantity:   2                                              
4.Rice                 10.00                                                 
5.Viand (anytype) 25.00
6.Drink                 20.00                                         Total:  80.00
7.Dessert               25.00                                          Enter amount:      90.00
8.Payment                                                                 Change:    10
9.Exit                                                                         





4.OBSERVATION

 SWITCH STATEMENT


5.CONCLUSION

Therefore I conclude that Switch Statement should be:
  Syntax:
       Switch(expression)
{


case value1:
statements....
break;

case value2:
statements....
break;
case else/default:
statements....
break;
}

                                                    2




1.PROBLEM STATEMENT
Addition,Subtraction,Multiplication,
Division
2.RELEVANT SOURCE CODE
int num1,num2;
int add,sub,mul,div;
int a;
System.out.print/n("Enter number");
     num1=str.next/n + ( );
     num2=str.next/n +( );
System.out.print/n("menu");
System.out.print/n("1add");
System.out.print/n("2sub");
System.out.print/n("3mul");
System.out.print/n("4div");
       a=str.next/n + ( );
   if (a= =1){
   add=num1+num2;
System.out.print/n("add");
}
   else if (a= =2){
   sub=num1-num2;
   print/n(sub);

  else if (a= =3){
  mul=num1*num2;
System.ou
t.
print/n(mul);
   else
System.ou
t.
print/n("Good Bye");

}
}
}


3.SAMPLE OUTPUT
   Enter number:
1+1=2     8/2=4
2*2=4     8-4=4

4.OBSERVATION
SWITCH STATEMENT

5.CONCLUSION
 The program won't run if syntax is incomplete.                              

                                             3

1.PROBLEM STATEMENT
Program that will display first name,last name,full name.
2.RELEVANT SOURCE CODE

import java.util.Scanner;

public class Main {


public static void main(String[] args) {

String firstname,lastname,fullname,name;
int add,sub,mul,div,a;
int num1,num2;
Scanner str = new Scanner(System.in);

System.out.println("Enter First Name");
firstname=str.next();
name=str.next();

System.out.println("Enter Last Name");
lastname=str.next();
fullname=firstname +" "+ name +" "+lastname;

System.out.println(fullname);

}
}


3.SAMPLE OUTPUT

FirstName:Jelyn
LastName:Tabladillo
 You are Jelyn Tabladillo


4.OBSERVATION
Using  import.java.util.Scanner;

5.CONCLUSION
 With out the help of java.util.Scanner in this program it wont run because it is the one help read  and add them.






                                                    4

1.PROBLEM STATEMENT
Develop a program that will identify if the given integer value is Positive-Even,

Positive-Odd,Negetive-Odd,Negative-Even.
2.RELEVANT SOURCE CODE

import java.util.Scanner;

public class lab2 {


public static void main(String[] args) {
Scanner a = new Scanner(System.in);

int num;

System.out.println("Enter Number : ");
num = a.nextInt();

if(num >0 && num %2 == 0){
System.out.println("positive-even");
}

if(num >0 && num %2 != 0 ){
System.out.println("positive-odd");
}

if(num <0 && num %2 != 0 ){
System.out.println("negative-odd");
}

if(num <0 && num %2 == 0 ){
System.out.println("negative-even");
}




}
}
  3.SAMPLE OUTPUT
Enter a number:2
2 is positive-even

4.OBSERVATION


5.CONCLUSION
   
The java.util.Scanner help read integers and add them.







                                                  5


1.PROBLEM STATEMENT
Largest.
2.RELEVANT SOURCE CODE
import javax.swing.JOptionPane;

public class Main {
public static void main(String[] args) {
int num[] = new int[5];

num[0] = Integer.parseInt(JOptionPane.showInputDialog("Enter first number"));
num[1] = Integer.parseInt(JOptionPane.showInputDialog("Enter second number"));
num[2] = Integer.parseInt(JOptionPane.showInputDialog("Enter third number"));
num[3] = Integer.parseInt(JOptionPane.showInputDialog("Enter fourth number"));
num[4] = Integer.parseInt(JOptionPane.showInputDialog("Enter last number"));



int largest = num[0];
for(int x=0;x<5;x++){
if(num[x]>largest){
largest = num[x];
}

}
JOptionPane.showMessageDialog(null,largest);

}

}


3.SAMPLE OUTPUT
1
2
3



4.OBSERVATION

javax.swing.JOptionPane;

5.CONCLUSION

javax.swing.JOptionPane works well in loops.




                                                   6


1.PROBLEM STATEMENT
// java applet//

2.RELEVANT SOURCE CODE
package sampleapplet;
import java.awt.Graphics;
import java.awt.Button;
import java.awt.Choice;
import java.applet.Applet;


public class NewApplet extends Applet {

Button b1=new Button("OK");
Button b2=new Button("CANCEL");


public void paint(Graphics g) {

g.drawString(" This is my Applet",20,100);

add(b1);
add(b2);
Choice ch=new Choice();

ch.add("Single");
ch.add("Married");
ch.add("Divorce");
}

}




3.SAMPLE OUTPUT




4.OBSERVATION




5.CONCLUSION









                                                              7


1.PROBLEM STATEMENT
// compare//
2.RELEVANT SOURCE CODE
package compare;

import java.util.Scanner;

public class Compare {

public static void main(String[] args) {

Scanner num = new Scanner(System.in);

int num1, num2;

System.out.println(" Enter two number: ");
num1 = num.nextInt();
num2 = num.nextInt();
if (num1<num2)
{
System.out.println(num1+"is less than"+num2);
}
else if(num1>num2){
System.out.println(num1+"is greater than"+num2);
}
else{
System.out.println(num1+" "+"equals to"+num2);
}
}
}

3.SAMPLE OUTPUT




4.OBSERVATION




5.CONCLUSION








                                                       8




1.PROBLEM STATEMENT
// highest to lowest num//

2.RELEVANT SOURCE CODE

 package myhi5;
 import java.util.Scanner;


public class Main {


public static void main(String[] args) {

Float num;

Scanner str = new Scanner(System.in);

System.out.println(" \n\nChoices");
System.out.printf(" \n\n1");
System.out.printf(" \n\n2");
System.out.printf(" \n\n3");
System.out.printf(" \n\n4");
System.out.printf(" \n\n5");

System.out.printf("\n\n Enter your number:");

num = str.nextFloat();

if (num < 5)

System.out.println("is not d heighest #");

else if (num == 5)

System.out.println("is d heighest #");

}



3.SAMPLE OUTPUT




4.OBSERVATION




5.CONCLUSION

                                                                   8


1.PROBLEM STATEMENT
CALCULATOR

2.RELEVANT SOURCE CODE


import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
//<applet code=Calculator height=300 width=200></applet>
public class Calculator extends JApplet {
   public void init() {
      CalculatorPanel calc=new CalculatorPanel();
      getContentPane().add(calc);
      }
   }

   class CalculatorPanel extends JPanel implements ActionListener {
      JButton
n1,n2,n3,n4,n5,n6,n7,n8,n9,n0,plus,minus,mul,div,dot,equal;
      static JTextField result=new JTextField("0",45);
      static String lastCommand=null;
      JOptionPane p=new JOptionPane();
      double preRes=0,secVal=0,res;

      private static void assign(String no)
        {
         if((result.getText()).equals("0"))
            result.setText(no);
          else if(lastCommand=="=")
           {
            result.setText(no);
            lastCommand=null;
           }
          else
            result.setText(result.getText()+no);
         }

      public CalculatorPanel() {
         setLayout(new BorderLayout());
         result.setEditable(false);
         result.setSize(300,200);
         add(result,BorderLayout.NORTH);
         JPanel panel=new JPanel();
         panel.setLayout(new GridLayout(4,4));

         n7=new JButton("7");
         panel.add(n7);
         n7.addActionListener(this);
         n8=new JButton("8");
         panel.add(n8);
         n8.addActionListener(this);
         n9=new JButton("9");
         panel.add(n9);
         n9.addActionListener(this);
         div=new JButton("/");
         panel.add(div);
         div.addActionListener(this);

         n4=new JButton("4");
         panel.add(n4);
         n4.addActionListener(this);
         n5=new JButton("5");
         panel.add(n5);
         n5.addActionListener(this);
         n6=new JButton("6");
         panel.add(n6);
         n6.addActionListener(this);
         mul=new JButton("*");
         panel.add(mul);
         mul.addActionListener(this);

         n1=new JButton("1");
         panel.add(n1);
         n1.addActionListener(this);
         n2=new JButton("2");
         panel.add(n2);
         n2.addActionListener(this);
         n3=new JButton("3");
         panel.add(n3);
         n3.addActionListener(this);
         minus=new JButton("-");
         panel.add(minus);
         minus.addActionListener(this);

         dot=new JButton(".");
         panel.add(dot);
         dot.addActionListener(this);
         n0=new JButton("0");
         panel.add(n0);
         n0.addActionListener(this);
         equal=new JButton("=");
         panel.add(equal);
         equal.addActionListener(this);
         plus=new JButton("+");
         panel.add(plus);
         plus.addActionListener(this);
         add(panel,BorderLayout.CENTER);
      }
      public void actionPerformed(ActionEvent ae)
         {
      if(ae.getSource()==n1) assign("1");
      else if(ae.getSource()==n2) assign("2");
      else if(ae.getSource()==n3) assign("3");
      else if(ae.getSource()==n4) assign("4");
      else if(ae.getSource()==n5) assign("5");
      else if(ae.getSource()==n6) assign("6");
      else if(ae.getSource()==n7) assign("7");
      else if(ae.getSource()==n8) assign("8");
      else if(ae.getSource()==n9) assign("9");
      else if(ae.getSource()==n0) assign("0");
      else if(ae.getSource()==dot)
            {
             if(((result.getText()).indexOf("."))==-1)
                result.setText(result.getText()+".");
           }
      else if(ae.getSource()==minus)
             {
             preRes=Double.parseDouble(result.getText());
             lastCommand="-";
             result.setText("0");
             }
      else if(ae.getSource()==div)
             {
             preRes=Double.parseDouble(result.getText());
             lastCommand="/";
             result.setText("0");
             }
      else if(ae.getSource()==equal)
             {
             secVal=Double.parseDouble(result.getText());
             if(lastCommand.equals("/"))
                  res=preRes/secVal;
             else if(lastCommand.equals("*"))
                  res=preRes*secVal;
             else if(lastCommand.equals("-"))
                  res=preRes-secVal;
             else if(lastCommand.equals("+"))
                  res=preRes+secVal;
             result.setText(" "+res);
             lastCommand="=";
             }
      else if(ae.getSource()==mul)
             {
              preRes=Double.parseDouble(result.getText());
              lastCommand="*";
              result.setText("0");
              }
      else if(ae.getSource()==plus)
              {
              preRes=Double.parseDouble(result.getText());
              lastCommand="+";
              result.setText("0");
              }

       }
 }