Advertisement
syad28

Lab 08 Programming

Nov 30th, 2015
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.96 KB | None | 0 0
  1. Lab 8 Question 1
  2.  
  3. package lab;
  4. import java.util.*;
  5. import java.io.*;
  6. public class Number{
  7.     Random rn = new Random();
  8.     int i;
  9.     public Number(){
  10.         int[] num = new int[10];
  11.         for(i=0;i<10;i++)
  12.             num[i]=rn.nextInt(100);
  13.         display(num);
  14.     }
  15.     public Number(int n){
  16.         int[] num = new int[n];
  17.         for(i=0;i<n;i++)
  18.             num[i]=rn.nextInt(100);
  19.         display(num);
  20.     }
  21.     public Number(int n,int l){
  22.         int[] num = new int[n];
  23.         for(i=0;i<n;i++)
  24.             num[i]=rn.nextInt(l);
  25.         display(num);
  26.     }
  27.     public static void display(int[] number){
  28.         System.out.print("\n\nList of generated number : ");
  29.         list(number);
  30.         System.out.print("\nMax : ");
  31.         MAX(number);
  32.         System.out.print("\nMin : ");
  33.         MIN(number);
  34.         System.out.print("\nPrime number : ");
  35.         PrimeNumber(number);
  36.         System.out.print("\nSquared number : ");
  37.         Square(number);
  38.         System.out.print("\nAverage : ");
  39.         Average(number);
  40.     }
  41.  
  42.     public static void list(int[] number){
  43.         for(int i=0;i<number.length;i++){
  44.             System.out.print(number[i]+" ");
  45.         }
  46.     }
  47.     public static void MAX(int[] number){
  48.         int MAX=0;
  49.         for(int i=0;i<number.length;i++)
  50.             if(number[i]>MAX)
  51.                 MAX=number[i];
  52.         System.out.print(MAX);
  53.     }
  54.     public static void MIN(int[] number){
  55.         int MIN=100;
  56.         for(int i=0;i<number.length;i++)
  57.             if(number[i]<MIN)
  58.                 MIN=number[i];
  59.         System.out.print(MIN);
  60.     }
  61.     public static void PrimeNumber(int[] number){
  62.         boolean stat;
  63.        
  64.         for(int i=0;i<number.length;i++){
  65.         stat=true;    
  66.             for(int j=1;j<number[i];j++){
  67.                 if(number[i]%j==0){
  68.                     stat=false;
  69.                     break;
  70.                 }
  71.             }
  72.             if(stat==true){
  73.                 System.out.print(number[i]);
  74.             }
  75.         }
  76.     }
  77.     public static void Square(int[] number){
  78.         int[] square = new int[number.length];
  79.         for(int i=0;i<number.length;i++){
  80.             square[i]=(int) Math.pow(number[i], 2);
  81.             System.out.print(square[i]+" ");
  82.         }
  83.     }
  84.     public static void Average(int[] number){
  85.         float Average,sum=0;
  86.         for(int i=0;i<number.length;i++){
  87.             sum+=number[i];
  88.         }
  89.         Average=sum/number.length;
  90.         System.out.print(Average);
  91.     }
  92.         public static void main(String[] args) {
  93.     Number a = new Number();
  94.     Number b = new Number(5);
  95.     Number c = new Number(4,50);
  96.     }
  97. }
  98.  
  99. Lab 8 Question 2
  100. //TESTER CLASS
  101. package tester;
  102. import java.util.*;
  103. public class Tester {
  104.     public static void main(String[] args) {
  105.         Scanner kb = new Scanner(System.in);
  106.         System.out.print("Please enter your name : ");
  107.         String name = kb.nextLine();
  108.         System.out.print("Please enter your IC/Passport : ");
  109.         String IC = kb.nextLine();
  110.         System.out.print("Enter deposit : ");
  111.         float bal = kb.nextFloat();
  112.         BankAccount a = new BankAccount(name,IC,bal);
  113.         System.out.println("");
  114. //        BankAccount a = new BankAccount("irsyad","huhu",200);
  115.         System.out.println("Name : "+a.getName());
  116.         System.out.println("IC : "+a.getIC());
  117.         System.out.printf("Balance : %.2f",a.getBal());
  118.         System.out.print("Enter withdrawal amount : ");
  119.         bal = kb.nextFloat();
  120.         a.Withdraws(bal);
  121.         System.out.printf("Balance : %.2f",a.getBal());
  122.         System.out.print("Enter deposit amount : ");
  123.         bal = kb.nextFloat();
  124.         a.Deposit(bal);
  125.         System.out.printf("Balance : %.2f",a.getBal());
  126.     }
  127.        
  128. }
  129.  
  130. //BANKACCOUNT CLASS
  131. package tester;
  132. import java.util.*;
  133. public class BankAccount {
  134.     private String name,ic;
  135.     private float deposit,balance;
  136.     public BankAccount(String a,String b,float c){
  137.         name = a;
  138.         ic = b;
  139.         balance = c;
  140.     }
  141.     public BankAccount(){
  142.         name = null;
  143.         ic = null;
  144.         balance = 0;
  145.     }
  146.    
  147.     public String getName(){
  148.         return name;
  149.     }
  150.     public String getIC(){
  151.         return ic;
  152.     }
  153.     public void Deposit(float a){
  154.         balance+=a;
  155.     }
  156.     public void Withdraws(float a){
  157.         balance-=a;
  158.     }
  159.     public float getBal(){
  160.         return balance;
  161.     }
  162. }
  163.  
  164.  
  165. Lab 8 Question 3
  166.  
  167. package lab;
  168. import java.util.*;
  169. import java.io.*;
  170. public class WeightCalculator{
  171.     WeightCalculator(int age,float height){
  172.         System.out.print("\nYour age : "+age);
  173.         System.out.print("\nYour height : "+height+" m");
  174.         recWeight(age,height);
  175.     }
  176.    
  177.     public static void recWeight(int age,float height){
  178.         float weight;
  179.         weight = (float) ((height-100+age/10)*0.9);
  180.         System.out.print("\nYour recommended weight : "+weight+" kg");
  181.     }
  182.    
  183.     public static void main(String[] args) {
  184.         WeightCalculator a = new WeightCalculator(17,170);
  185.         System.out.println("");
  186.        
  187.     }
  188. }
  189.  
  190. Lab 8 Question 4
  191.  
  192. /*Define a class Fraction. The class has an input method that accepts the numerator and the
  193. denominator from the user. Use the mutator method to set the numerator and denominator and
  194. the accessor method to get the value of numerator and denominator. This class also has a
  195. method to display the fraction reduced to lowest terms. (find the greatest common divisor
  196. for the numerator and denominator. Create a Tester class to test the program.
  197. */
  198.  
  199. package lab;
  200. import java.util.*;
  201. import java.io.*;
  202. public class Fraction{
  203.     private static int num,den;
  204.     Scanner kb=new Scanner(System.in);
  205.     public Fraction(){
  206.        
  207.     }
  208.       public Fraction(int n,int d){
  209.            num = n;
  210.            den = d;
  211.        }
  212.        
  213.     public static void main(String[] args) {
  214.         Fraction a = new Fraction();
  215.         a.setNum();
  216.         a.setDen();
  217.         a.Disp();
  218.         int gcd;
  219.         if(num>den)
  220.             gcd=GCD(num,den);
  221.         else
  222.             gcd=GCD(den,num);
  223.         System.out.print("GCD : "+gcd);
  224.         LOWEST(num,den,gcd);
  225.     }
  226.     public void setNum(){   //mutator
  227.         System.out.print("Enter numerator : ");
  228.         num=kb.nextInt();
  229.     }
  230.     public void setDen(){ //mutator
  231.         System.out.print("Enter denumerator : ");
  232.         den=kb.nextInt();
  233.     }
  234.     public int getNum(){
  235.         return num;
  236.     }
  237.     public int getDen(){
  238.         return den;
  239.     }
  240.     public void Disp(){ //accessor
  241.         System.out.print(num+"/");
  242.         System.out.println(den);
  243.     }
  244.         public static int GCD(int large,int small){
  245.     int val;
  246.         val=large%small;
  247.         if(val!=0){
  248.         large=small;
  249.         small=val;
  250.         GCD(large,small);
  251.         }
  252.         return small;
  253.    }
  254.         public static void LOWEST(int num,int den,int gcd){
  255.             if((num%gcd)>0||(den%gcd)>0)
  256.                 System.out.println("\nLowest fraction : "+num+" / "+den);
  257.             else
  258.             System.out.println("\nLowest fraction : "+(num/gcd)+" / "+(den/gcd));
  259.         }
  260. }
  261.  
  262. Lab 8 Question 5
  263.  
  264. /*Define a class Game. The class has a constructor that accept player name.
  265. Besides, the class contains a method move that roll the dice.
  266. Create a Tester class to test the program with two players and
  267. the player that reach 100 or more win the game.
  268. */
  269.  
  270. package lab;
  271. import java.util.*;
  272. import java.io.*;
  273. public class Game{
  274.     Random rand=new Random();
  275.     Scanner kb=new Scanner(System.in);
  276.     private String player1,player2;
  277.    
  278.     public Game(){
  279.         System.out.print("Enter player 1 name : ");
  280.         player1=kb.next();
  281.         System.out.print("Enter player 2 name : ");
  282.         player2=kb.next();
  283.         MOVE(player1,player2);
  284.     }
  285.     public static void main(String[] args) {
  286.         Game a = new Game();
  287.     }
  288.     public void MOVE(String a,String b){
  289.         int dice1=0,dice2=0;
  290.         while((dice1<100)&&(dice2<100)){
  291.         dice1 += rand.nextInt(6)+1;
  292.         System.out.println(player1+" : "+dice1+"/100");
  293.         if(dice1>=100)
  294.             break;
  295.         dice2 += rand.nextInt(6)+1;
  296.         System.out.println(player2+" : "+dice2+"/100");
  297.         }
  298.         if(dice1>dice2){
  299.             System.out.print(player1+" WIN !");
  300.         }
  301.         else
  302.             System.out.print(player2+" WIN ! ");
  303.     }
  304. }
  305.  
  306. Lab 8 Question 6
  307.  
  308. //TESTER CLASS
  309.  
  310. package tester;
  311. import java.util.*;
  312. public class Tester {
  313.     public static void main(String[] args) {
  314.         Burger a = new Burger("Irsyad",400);
  315.         a.BurgerSold(30);
  316.         a.Display();
  317.         Burger b = new Burger("huhu",40);
  318.         b.Display();
  319.         System.out.println("Total burgers sold : "+(a.Total()+b.Total()));
  320.     }
  321. }
  322.  
  323. //BURGER CLASS
  324.  
  325. package tester;
  326. import java.util.*;
  327. public class Burger {
  328.     private int BurgerAmount;
  329.     private String stall;
  330.     public Burger(){
  331.         BurgerAmount = 0;
  332.     }
  333.     public Burger(String a,int b){
  334.         stall = a;
  335.         BurgerAmount = b;
  336.     }
  337.     public void BurgerSold(int a){
  338.         BurgerAmount+=a;
  339.     }
  340.     public void ID(String a){
  341.         stall = a;
  342.     }
  343.     public void Display(){
  344.         System.out.println("Stall : "+stall);
  345.         System.out.println("Burger Sold : "+BurgerAmount);
  346.     }
  347.     public int Total(){
  348.         return BurgerAmount;
  349.     }
  350.    
  351. }
  352.  
  353. Lab 8 Question 7
  354.  
  355. package lab;
  356. import java.util.*;
  357. import java.io.*;
  358. public class Money{
  359.     Random rand=new Random();
  360.    
  361.     private static double m;
  362.     public Money(double duit){
  363.         m=duit;
  364.     }
  365.     public static void main(String[] args) {
  366.         System.out.print("Enter money : ");
  367.         double test;
  368.         Scanner kb=new Scanner(System.in);
  369.         test=kb.nextDouble();
  370.         Money a = new Money(test);
  371.         double R = RoundOff(m);
  372.         System.out.println("Rounded off money = "+R);
  373.         Calculate(R);
  374.     }
  375.     public static double RoundOff(double m){
  376.         double rounded=m*100.0;
  377.         if(rounded%10<3)
  378.             rounded-=rounded%10;
  379.         else if(rounded%10<8)
  380.             rounded=((int)(rounded/10)*10)+5;
  381.         else if(rounded%10<=9)
  382.             rounded+=10-(rounded%10);
  383.         rounded/=100;
  384.         return rounded;
  385.     }
  386.    
  387.     public static void Calculate(double m){
  388.         int coins[]={10000,5000,1000,500,100,50,20,10,5,1},i,piece,rm;
  389.                 m=(m*100.0);
  390.                 rm=(int)m;
  391.                 for(i=0;i<coins.length;i++)
  392.                 {
  393.                         piece=rm/coins[i];
  394.                         if(piece==0)
  395.                                 continue;
  396.                         if(coins[i]>=100)
  397.                         System.out.printf("\nRM %d = %d notes",coins[i]/100,piece);
  398.                         else
  399.                         System.out.printf("\n%d cents = %d pieces",coins[i],piece);
  400.                         rm-=(coins[i]*piece);
  401.                 }
  402.     }
  403.    
  404. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement