Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Lab 8 Question 1
- package lab;
- import java.util.*;
- import java.io.*;
- public class Number{
- Random rn = new Random();
- int i;
- public Number(){
- int[] num = new int[10];
- for(i=0;i<10;i++)
- num[i]=rn.nextInt(100);
- display(num);
- }
- public Number(int n){
- int[] num = new int[n];
- for(i=0;i<n;i++)
- num[i]=rn.nextInt(100);
- display(num);
- }
- public Number(int n,int l){
- int[] num = new int[n];
- for(i=0;i<n;i++)
- num[i]=rn.nextInt(l);
- display(num);
- }
- public static void display(int[] number){
- System.out.print("\n\nList of generated number : ");
- list(number);
- System.out.print("\nMax : ");
- MAX(number);
- System.out.print("\nMin : ");
- MIN(number);
- System.out.print("\nPrime number : ");
- PrimeNumber(number);
- System.out.print("\nSquared number : ");
- Square(number);
- System.out.print("\nAverage : ");
- Average(number);
- }
- public static void list(int[] number){
- for(int i=0;i<number.length;i++){
- System.out.print(number[i]+" ");
- }
- }
- public static void MAX(int[] number){
- int MAX=0;
- for(int i=0;i<number.length;i++)
- if(number[i]>MAX)
- MAX=number[i];
- System.out.print(MAX);
- }
- public static void MIN(int[] number){
- int MIN=100;
- for(int i=0;i<number.length;i++)
- if(number[i]<MIN)
- MIN=number[i];
- System.out.print(MIN);
- }
- public static void PrimeNumber(int[] number){
- boolean stat;
- for(int i=0;i<number.length;i++){
- stat=true;
- for(int j=1;j<number[i];j++){
- if(number[i]%j==0){
- stat=false;
- break;
- }
- }
- if(stat==true){
- System.out.print(number[i]);
- }
- }
- }
- public static void Square(int[] number){
- int[] square = new int[number.length];
- for(int i=0;i<number.length;i++){
- square[i]=(int) Math.pow(number[i], 2);
- System.out.print(square[i]+" ");
- }
- }
- public static void Average(int[] number){
- float Average,sum=0;
- for(int i=0;i<number.length;i++){
- sum+=number[i];
- }
- Average=sum/number.length;
- System.out.print(Average);
- }
- public static void main(String[] args) {
- Number a = new Number();
- Number b = new Number(5);
- Number c = new Number(4,50);
- }
- }
- Lab 8 Question 2
- //TESTER CLASS
- package tester;
- import java.util.*;
- public class Tester {
- public static void main(String[] args) {
- Scanner kb = new Scanner(System.in);
- System.out.print("Please enter your name : ");
- String name = kb.nextLine();
- System.out.print("Please enter your IC/Passport : ");
- String IC = kb.nextLine();
- System.out.print("Enter deposit : ");
- float bal = kb.nextFloat();
- BankAccount a = new BankAccount(name,IC,bal);
- System.out.println("");
- // BankAccount a = new BankAccount("irsyad","huhu",200);
- System.out.println("Name : "+a.getName());
- System.out.println("IC : "+a.getIC());
- System.out.printf("Balance : %.2f",a.getBal());
- System.out.print("Enter withdrawal amount : ");
- bal = kb.nextFloat();
- a.Withdraws(bal);
- System.out.printf("Balance : %.2f",a.getBal());
- System.out.print("Enter deposit amount : ");
- bal = kb.nextFloat();
- a.Deposit(bal);
- System.out.printf("Balance : %.2f",a.getBal());
- }
- }
- //BANKACCOUNT CLASS
- package tester;
- import java.util.*;
- public class BankAccount {
- private String name,ic;
- private float deposit,balance;
- public BankAccount(String a,String b,float c){
- name = a;
- ic = b;
- balance = c;
- }
- public BankAccount(){
- name = null;
- ic = null;
- balance = 0;
- }
- public String getName(){
- return name;
- }
- public String getIC(){
- return ic;
- }
- public void Deposit(float a){
- balance+=a;
- }
- public void Withdraws(float a){
- balance-=a;
- }
- public float getBal(){
- return balance;
- }
- }
- Lab 8 Question 3
- package lab;
- import java.util.*;
- import java.io.*;
- public class WeightCalculator{
- WeightCalculator(int age,float height){
- System.out.print("\nYour age : "+age);
- System.out.print("\nYour height : "+height+" m");
- recWeight(age,height);
- }
- public static void recWeight(int age,float height){
- float weight;
- weight = (float) ((height-100+age/10)*0.9);
- System.out.print("\nYour recommended weight : "+weight+" kg");
- }
- public static void main(String[] args) {
- WeightCalculator a = new WeightCalculator(17,170);
- System.out.println("");
- }
- }
- Lab 8 Question 4
- /*Define a class Fraction. The class has an input method that accepts the numerator and the
- denominator from the user. Use the mutator method to set the numerator and denominator and
- the accessor method to get the value of numerator and denominator. This class also has a
- method to display the fraction reduced to lowest terms. (find the greatest common divisor
- for the numerator and denominator. Create a Tester class to test the program.
- */
- package lab;
- import java.util.*;
- import java.io.*;
- public class Fraction{
- private static int num,den;
- Scanner kb=new Scanner(System.in);
- public Fraction(){
- }
- public Fraction(int n,int d){
- num = n;
- den = d;
- }
- public static void main(String[] args) {
- Fraction a = new Fraction();
- a.setNum();
- a.setDen();
- a.Disp();
- int gcd;
- if(num>den)
- gcd=GCD(num,den);
- else
- gcd=GCD(den,num);
- System.out.print("GCD : "+gcd);
- LOWEST(num,den,gcd);
- }
- public void setNum(){ //mutator
- System.out.print("Enter numerator : ");
- num=kb.nextInt();
- }
- public void setDen(){ //mutator
- System.out.print("Enter denumerator : ");
- den=kb.nextInt();
- }
- public int getNum(){
- return num;
- }
- public int getDen(){
- return den;
- }
- public void Disp(){ //accessor
- System.out.print(num+"/");
- System.out.println(den);
- }
- public static int GCD(int large,int small){
- int val;
- val=large%small;
- if(val!=0){
- large=small;
- small=val;
- GCD(large,small);
- }
- return small;
- }
- public static void LOWEST(int num,int den,int gcd){
- if((num%gcd)>0||(den%gcd)>0)
- System.out.println("\nLowest fraction : "+num+" / "+den);
- else
- System.out.println("\nLowest fraction : "+(num/gcd)+" / "+(den/gcd));
- }
- }
- Lab 8 Question 5
- /*Define a class Game. The class has a constructor that accept player name.
- Besides, the class contains a method move that roll the dice.
- Create a Tester class to test the program with two players and
- the player that reach 100 or more win the game.
- */
- package lab;
- import java.util.*;
- import java.io.*;
- public class Game{
- Random rand=new Random();
- Scanner kb=new Scanner(System.in);
- private String player1,player2;
- public Game(){
- System.out.print("Enter player 1 name : ");
- player1=kb.next();
- System.out.print("Enter player 2 name : ");
- player2=kb.next();
- MOVE(player1,player2);
- }
- public static void main(String[] args) {
- Game a = new Game();
- }
- public void MOVE(String a,String b){
- int dice1=0,dice2=0;
- while((dice1<100)&&(dice2<100)){
- dice1 += rand.nextInt(6)+1;
- System.out.println(player1+" : "+dice1+"/100");
- if(dice1>=100)
- break;
- dice2 += rand.nextInt(6)+1;
- System.out.println(player2+" : "+dice2+"/100");
- }
- if(dice1>dice2){
- System.out.print(player1+" WIN !");
- }
- else
- System.out.print(player2+" WIN ! ");
- }
- }
- Lab 8 Question 6
- //TESTER CLASS
- package tester;
- import java.util.*;
- public class Tester {
- public static void main(String[] args) {
- Burger a = new Burger("Irsyad",400);
- a.BurgerSold(30);
- a.Display();
- Burger b = new Burger("huhu",40);
- b.Display();
- System.out.println("Total burgers sold : "+(a.Total()+b.Total()));
- }
- }
- //BURGER CLASS
- package tester;
- import java.util.*;
- public class Burger {
- private int BurgerAmount;
- private String stall;
- public Burger(){
- BurgerAmount = 0;
- }
- public Burger(String a,int b){
- stall = a;
- BurgerAmount = b;
- }
- public void BurgerSold(int a){
- BurgerAmount+=a;
- }
- public void ID(String a){
- stall = a;
- }
- public void Display(){
- System.out.println("Stall : "+stall);
- System.out.println("Burger Sold : "+BurgerAmount);
- }
- public int Total(){
- return BurgerAmount;
- }
- }
- Lab 8 Question 7
- package lab;
- import java.util.*;
- import java.io.*;
- public class Money{
- Random rand=new Random();
- private static double m;
- public Money(double duit){
- m=duit;
- }
- public static void main(String[] args) {
- System.out.print("Enter money : ");
- double test;
- Scanner kb=new Scanner(System.in);
- test=kb.nextDouble();
- Money a = new Money(test);
- double R = RoundOff(m);
- System.out.println("Rounded off money = "+R);
- Calculate(R);
- }
- public static double RoundOff(double m){
- double rounded=m*100.0;
- if(rounded%10<3)
- rounded-=rounded%10;
- else if(rounded%10<8)
- rounded=((int)(rounded/10)*10)+5;
- else if(rounded%10<=9)
- rounded+=10-(rounded%10);
- rounded/=100;
- return rounded;
- }
- public static void Calculate(double m){
- int coins[]={10000,5000,1000,500,100,50,20,10,5,1},i,piece,rm;
- m=(m*100.0);
- rm=(int)m;
- for(i=0;i<coins.length;i++)
- {
- piece=rm/coins[i];
- if(piece==0)
- continue;
- if(coins[i]>=100)
- System.out.printf("\nRM %d = %d notes",coins[i]/100,piece);
- else
- System.out.printf("\n%d cents = %d pieces",coins[i],piece);
- rm-=(coins[i]*piece);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement