Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

CSC238

NAME : MUHAMMAD KHAIRUL IKHWAN BIN


ROSMAN
MATRIX NO : 2018634622
GROUP : RCS1103F
TITLE : LAB ASSIGNMENT 1
Question 1

import java.util.*;
import java.io.*;

class Ftmsk {
public static void main (String[]args){

BufferedReader in = null;
PrintWriter outcs110 = null;
PrintWriter outcs111 = null;
try {

in = new BufferedReader (new FileReader("R:\\FTMSK.txt"));


outcs110 = new PrintWriter (new BufferedWriter (new
FileWriter("R:\\CS110Male.txt")));
outcs111 = new PrintWriter (new BufferedWriter (new
FileWriter("R:\\CS111Male.txt")));
String inData = null;

String [] matNum = new String [100];


String [] name =new String [100];
String[] prog = new String [100];
int [] part = new int [100];
char [] gender = new char [100];
int ct = 0;

int cs110 = 0;
int cs111= 0;

while ((inData = in.readLine()) != null){

StringTokenizer st = new StringTokenizer (inData,";");

matNum[ct] = st.nextToken();
name[ct] = st.nextToken();
prog[ct] = st.nextToken();

String pa = st.nextToken();
part[ct] = Integer.parseInt(pa);

String gen = st.nextToken();


gender[ct] = gen.charAt(0);

ct++;
}

outcs110.println(" Number of Male Students


for CS110 ");
outcs111.println(" Number of Male Students
for CS111 ");
outcs110.println(String.format("%-20s %-20s %-20s ","Matric
Number","Name","Part"));
outcs111.println(String.format("%-20s %-20s %-20s ","Matric
Number","Name","Part"));

for (int i = 0; i<ct;i++){


if (gender[i] == 'M')
{ if (prog[i].equalsIgnoreCase("CS110"))
{

outcs110.println(String.format("%-20s %-20s %-20s ",


matNum[i], name[i], part[i]));
cs110++;
}
if (prog[i].equalsIgnoreCase("CS111"))
{

outcs111.println(String.format("%-20s %-20s %-20s ",


matNum[i] , name[i] , part[i]));
cs111++;
}
}
}
outcs110.println("Number of Male Students for CS110 : "
+ cs110);
outcs111.println("Number of Male Students for CS111 : "
+ cs111);

in.close();
outcs110.close();
outcs111.close();
}
catch ( FileNotFoundException fe)
{
System.out.println(fe.getMessage());
}

catch (IOException iox) {


System.out.println(iox.getMessage());}

catch (Exception e){


System.out.println("Problem : " + e.getMessage());
}
}
}

output
Question 2

import java.util.*;
import java.io.*;

class Customer {

private String custName;


private String custAddress;
private int productID;
private int quantity;
private String pakej;

public Customer (String name,String add,int id,int qua,String


pak){
custName = name;
custAddress = add;
productID = id;
quantity = qua;
pakej = pak;
}

public void setcustName (String name){custName = name;}


public void setcustAddress (String add){custAddress = add;}
public void setproductID (int id){productID = id;}
public void setquantity (int qua){quantity = qua;}
public void setpakej (String pak){pakej = pak;}

public String getcustName(){return custName;}


public String getcustAddress(){return custAddress;}
public int getproductID(){return productID;}
public int getquantity(){return quantity;}
public String getpakej(){return pakej;}

public String toString(){


return " Customer Name : " +getcustName() +"\n Customer
Address : "+getcustAddress() +"\n Product ID : "
+getproductID()+"\n Quantity : "+getquantity()+"\n Customer
Package : " +getpakej();
}}
public class CustomerApp{
public static void main (String [] args){

try {
FileReader fr = new FileReader ("order.txt");
BufferedReader in = new BufferedReader (fr);

FileWriter fwStaff = new FileWriter ("staff.txt");


BufferedWriter outStaff = new BufferedWriter (fwStaff);
PrintWriter printStaff = new PrintWriter (outStaff);

FileWriter fwStud = new FileWriter ("student.txt");


BufferedWriter outStud = new BufferedWriter (fwStud);
PrintWriter printStud = new PrintWriter (outStud);

String custName ,custAddress,custPackage;


int productID,quantity;
double total =0 ,subtotal = 0;
Customer c ;

StringTokenizer input = null;


String data = in.readLine();

while (data != null ) {


input = new StringTokenizer (data,":");
custName = input.nextToken();
custAddress = input.nextToken();

String pID = input.nextToken();


productID = Integer.parseInt(pID);

String qua = input.nextToken();


quantity = Integer.parseInt(qua);

custPackage =input.nextToken();

c = new
Customer(custName,custAddress,productID,quantity,custPackage);

if ( c.getproductID() == 101)
subtotal = 200 * quantity;
else if (c.getproductID() == 102)
subtotal = 170 * quantity;
else if ( c.getproductID() == 103)
subtotal = 350 * quantity ;
else if (c.getproductID() == 104)
subtotal = 500 * quantity;

total = subtotal + 11;

if ( c.getpakej().equalsIgnoreCase("Student"))
{ total = total - (total * 0.1);
printStud.println(c +"\t\t Total : " +total);
}
if (c.getpakej().equalsIgnoreCase("Staff"))
printStaff.println(c +"\t\tTotal: " +total);

data = in.readLine();
}

printStaff.close();
printStud.close();
}
catch (FileNotFoundException fnfe){
System.out.println(fnfe.getMessage());}

catch (IOException iox){

System.out.println("Problem Reading : "


+iox.getMessage());}

catch (Exception e ){
System.out.println("Problem : " + e.getMessage());}

}}

output
Question 3

import java.util.*;
import java.io.*;

class Hfmd{
public static void main (String [] args){

BufferedReader inFile = null;


PrintWriter outFile = null;

try{

String state;
int case2017,case2018,total2017 = 0, total2018 = 0 ;

inFile = new BufferedReader ( new FileReader


("R:\\hfmdcases.txt"));
outFile = new PrintWriter (new BufferedWriter (new
FileWriter("R:\\report.txt")));
String hfmd = null;

outFile.println("\t HFMD CASES FOR 2017 AND 2018 ");


outFile.println(String.format("%-16s %-15s %-11s ",
"\nSTATE" ,"2017" , "2018")) ;

while ((hfmd = inFile.readLine()) != null){

StringTokenizer st = new StringTokenizer(hfmd,";");


state = st.nextToken();

String ca2017 = st.nextToken();


case2017 = Integer.parseInt(ca2017);

String ca2018 = st.nextToken();


case2018 = Integer.parseInt(ca2018);

outFile.println( state +"\t"+case2017+"\t\t"+case2018);


total2017 = total2017 + case2017;
total2018 = total2018 + case2018;

}
outFile.println("Total cases for 2017 is : " +
total2017);
outFile.println("Total cases for 2018 is : " +
total2018);
inFile.close();
outFile.close();
}

catch (FileNotFoundException fe) {


System.out.println(fe.getMessage());}

catch (IOException iox){


System.out.println(iox.getMessage());}

catch (Exception e){


System.out.println("Problem : " +e.getMessage());}
}}

Output

You might also like