Sunday, June 16, 2013

Count Character in java

package homework;
import java.io.*;

public class Topic6 {
    public static void main(String[] args) throws IOException{
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int num=0;
        int[] alpha = new int[26];
        char[] ch=null;
        System.out.print("How many character do you want to get(only lower case)? : ");
        num = Integer.parseInt(in.readLine());
        ch = new char[num];
        System.out.print("Characters : ");
        for(int i=0;i<ch.length;i++){
            ch[i]=(char)System.in.read();
        }
        for(int i=0;i<ch.length;i++)
            alpha[ch[i]-65]++;
       
        for(int i=1;i<=alpha.length;i++){
            System.out.print((char)(i+64)+"="+alpha[i-1]+"  ");
            if(i % 3== 0)
                System.out.println();
        }
    }
}

Text Calculator in java

package homework;

import java.io.*;

public class Topic4 {
    public static void main(String[] args) throws IOException{
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        int num1=0,num2=0,total=0;char operator;
        while(true){
            System.out.print("Num1 = ");
            num1=Integer.parseInt(input.readLine());
            do{
                System.out.print("Operator (+, -, *, /, %) = ");
                operator =(char)System.in.read();
                System.in.read();
                System.in.read();
            }while(operator !='+' && operator !='-' && operator !='*' && operator !='/' && operator !='%');
            System.out.print("Num2 = ");
            num2=Integer.parseInt(input.readLine());
            switch(operator){
                case '+':total = num1+num2;break;
                case '-':total = num1+num2;break;
                case '*':total = num1+num2;break;
                case '/':total = num1/num2;break;
                case '%':total = num1%num2;break;
            }
            System.out.println("Result: "+num1+" + "+num2+" ="+total);
           
            System.out.print("Do you want to continue(y/n)?");
            char ch =(char)System.in.read();
            if(ch=='n' || ch=='N')
                break;
            else{
                System.in.read();
                System.in.read();
            }
        }
    }
}

Check Character in java

package homework;

import java.io.*;
public class Topic2 {
    public static void main(String[] args) throws IOException{
            System.out.print("Input one character ");
            char ch = (char)System.in.read();
            if((int)ch >=65 && (int)ch <=90)
                System.out.println(ch +" is upper-case");
            if((int)ch>=97 && (int)ch<=122)
                System.out.println(ch +" is lower-case");

    }
}

Hotel Management in java

-Use one dimensional Array:

package homework;
import java.io.*;
public class Topic8 {
    public static void main(String[] args) throws IOException{
        if(args.length !=1)
            System.exit(0);
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int num = Integer.parseInt(args[0]);
        //int num=Integer.parseInt(in.readLine());
        String[] name = new String[num];
        String temp ="";
        while(true){
            System.out.print("1. Check In, 2. Check Out, 3. Show All, 4. Exit = ");
            int condition= Integer.parseInt(in.readLine());
            if(condition == 1){
                System.out.print("Check In : Room Number ? ");
                int cr = Integer.parseInt(in.readLine());
                if(cr>=0 && cr < name.length){
                    System.out.print("name = ");
                    temp=in.readLine();
                    if(name[cr] != null){
                        System.out.println("This room is busy!");
                    }else{
                        name[cr]=temp;
                    }
                }else{
                    System.out.println("This room "+cr+" is not have.");
                }
            }else if(condition == 2){
                System.out.print("Check Out : Room Number ? ");
                int co = Integer.parseInt(in.readLine());
                if(co>=0 && co < name.length){
                    if(name[co] !=null)
                        name[co]=null;
                    else
                        System.out.println("This room is already check out!");
                }else{
                    System.out.println("Room "+co+" is not have.");
                }
            }else if(condition == 3){
                for(int i=0;i<name.length;i++)
                    System.out.print("Room No."+i+" = "+name[i]+"\t");
                System.out.println();
            }else if(condition == 4){
                System.out.print("End...");
                System.exit(0);
            }else{
                System.out.println("Please choose that has available in option!");
            }
           
        }
       
    }
}

-Use tow dimensional Array:


package homework;

import java.io.*;

public class Topic10 {
    public static void main(String[] args) throws IOException{
        if(args.length !=2)
            System.exit(0);
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int num = Integer.parseInt(args[0]);//floor
        int num1 = Integer.parseInt(args[1]);//room
        String[][] name = new String[num][num1];
        String temp="";
        while(true){
            System.out.print("1. Check In, 2. Check Out, 3. Show All, 4. Exit = ");
            int condition = Integer.parseInt(in.readLine());
            if(condition == 1){
                System.out.print("Floor number = ");
                int fn = Integer.parseInt(in.readLine());
                if(fn>=0 && fn < num){
                    System.out.print("Room number = ");
                    int rn = Integer.parseInt(in.readLine());
                    if(rn>=0 && rn < num){
                        System.out.print("Customer name = ");
                        temp = in.readLine();
                        if(name[fn][rn] != null){
                            System.out.println("This room is busy!");
                        }else{
                            name[fn][rn]=temp;
                        }
                    }else{
                        System.out.println("This room "+rn+" is not have.");
                    }
                }else{
                    System.out.println("This floor "+fn+" is not have.");
                }
            }else if(condition == 2){
                System.out.print("Floor number = ");
                int fn = Integer.parseInt(in.readLine());
                if(fn>=0 && fn < num){
                    System.out.print("Room number = ");
                    int rn = Integer.parseInt(in.readLine());
                    if(rn>=0 && rn < num){
                        System.out.print("Customer name = ");
                        temp = in.readLine();
                        if(name[fn][rn] !=null)
                            name[fn][rn]=null;
                        else
                            System.out.println("This room is already check out!");
                    }else{
                        System.out.println("This room "+rn+" is not have.");
                    }
                }else{
                    System.out.println("This floor "+fn+" is not have.");
                }
            }else if(condition == 3){
                for(int i=0;i<num;i++){
                    System.out.println(i+" floor!");
                    for(int j=0;j<num1;j++){
                        System.out.print("Room No."+j+" : "+name[i][j]+"\t");
                    }
                    System.out.println();
                }
            }else if(condition == 4){
                System.out.print("End...");
                System.exit(0);
            }else{
                System.out.println("Please choose that has available in option!");
            }   
        }
    }
}

Score Management in java

- Use one dimensional Array:
package homework;

import java.io.*;

public class Topic7 {
    public static void main(String[] args) throws IOException{
        if(args.length != 1)
            System.exit(0);
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int number_of_student= Integer.parseInt(args[0]);
        //int number_of_student= Integer.parseInt(in.readLine());
        String[] name = new String[number_of_student];
        int[] kor = new int[number_of_student];
        int[] eng = new int[number_of_student];
        int[] mat = new int[number_of_student];
        int[] tot = new int[number_of_student];
        float[] avg = new float[number_of_student];
        int[] rank = new int[number_of_student];
       
        for(int i=1;i<=number_of_student;i++){
            System.out.println("No. "+i+" Student Info :");
            System.out.print("Name ? ");
            name[i-1] = in.readLine();
            System.out.print("kor ? ");
            kor[i-1]=Integer.parseInt(in.readLine());
            System.out.print("eng ? ");
            eng[i-1]=Integer.parseInt(in.readLine());
            System.out.print("mat ? ");
            mat[i-1]=Integer.parseInt(in.readLine());
            tot[i-1]=kor[i-1]+eng[i-1]+mat[i-1];
            avg[i-1]=tot[i-1]/3;
            rank[i-1]=1;
        }

        for(int i=0;i<number_of_student;i++){
            for(int j=0;j<number_of_student;j++){
                if(avg[i]>avg[j]) rank[j]++;
            }
        }
        System.out.println();
        for(int i=0;i<number_of_student;i++){
            System.out.println(name[i]+"\t"+ kor[i] +"\t"+eng[i]+"\t"+mat[i]+"\t"+tot[i]+"\t"+avg[i]+"\t"+rank[i]);

        }
    }
}

-Use tow dimensional Array:


package homework;
import java.io.*;

public class Topic9 {
    public static void main(String[] args) throws IOException{
        if(args.length < 2){
            System.out.println("Please you input argument at least 2: ");
            System.exit(0);
        }
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("How many classes ? ");
        int x = Integer.parseInt(in.readLine());
        String[][] name = new String[x][];
        float[][][] sub = new float[x][][];
        float[][]tot=new float[x][];
        int[][]crank=new int[x][];
        int[][]rank=new int[x][];
        float[][] avg = new float[x][];
        for(int i=0;i<x;i++){
            System.out.print(i+"th class: number of students? ");
            int num_of_student_per_class=Integer.parseInt(in.readLine());
            name[i] = new String[num_of_student_per_class];
            sub[i]=new float[name[i].length][args.length];
            tot[i]=new float[name[i].length];
            crank[i]=new int[name[i].length];
            rank[i]=new int[name[i].length];
            avg[i]=new float[name[i].length];
        }
        for(int i=0;i<x;i++){
            for(int j=0;j<name[i].length;j++){
                System.out.print("class"+i+": students"+j+"'s name = ");
                name[i][j]=in.readLine();
               
                for(int k=0;k<args.length;k++){
                    do{
                        System.out.print(args[k]+" = ");
                        sub[i][j][k]=Integer.parseInt(in.readLine());
                    }while(sub[i][j][k]<0 || sub[i][j][k]>100);
                    tot[i][j] +=sub[i][j][k];
                }
                crank[i][j]=1;rank[i][j]=1;avg[i][j]=(tot[i][j]/(args.length));
            }
        }
        for(int i=0;i<name.length;i++){
            for(int j=0;j<name[i].length;j++){
                for(int k=0;k<name[i].length;k++)
                    if(tot[i][j]>tot[i][k]) crank[i][k]++;
            }
        }
       
        for(int i=0;i<avg.length;i++)
            for(int j=0;j<avg[i].length;j++)
                for(int m=0;m<avg.length;m++)
                    for(int k=0;k<avg[m].length;k++)
                        if(avg[i][j]>avg[m][k]) rank[m][k]++;
                   

        System.out.println();
        for(int i=0;i<x;i++){
            for(int j=0;j<name[i].length;j++){
                System.out.print(name[i][j]+"\t");
                for(int k=0;k<sub[i][j].length;k++){
                    System.out.print(sub[i][j][k]+"\t");
                }
                System.out.print(tot[i][j]+"\t"+crank[i][j]+"\t"+rank[i][j]+"\t"+avg[i][j]+"\t");
                System.out.println();
            }
        }
   
    }
}