You are on page 1of 4

StudentMarks.

java

package lab2;

import java.util.Scanner; // For the Scanner class


import java.io.*; // For file I/O classes

import javax.swing.JOptionPane; // For the JOptionPane class

public class StudentMarks {

public static void main(String[] args) throws IOException


{
// TODO Auto-generated method stub
final int num_Students = 20;
String file_Name;
double marks_Total;
double marks_Average;

// Get the file name.


file_Name = getFileName();

// Get the total marks from the file.


marks_Total = getTotalMarks(file_Name);

// Calculate the average marks.


marks_Average = marks_Total/num_Students;

// Display the result of total and average value.


displayResults(marks_Total,marks_Average);

System.exit(0);

}
public static String getFileName()
{
String file; // To hold file name

// Prompt user to enter file name.


// use JOptionPane.showInputDialog and store in file
file = JOptionPane.showInputDialog("Enter file name");

// Return the name.


return file;
}

public static double getTotalMarks(String file_Name)throws IOException


{
double total = 0.0;
double marks;

// Open the file.


File file = new File(file_Name);
// Use Scanner to read from file
Scanner read = new Scanner(file);

// loop processes the lines read from the file until end of the file
// use while & hasNext
while(read.hasNextLine())
{
// Read from file and store in marks
marks = read.nextDouble();

// Get total marks


total = total + marks;
}

// Close the file.


read.close();

// Return the total marks.


return total;
}

public static void displayResults(double total, double avg)


{
// Display the total marks and average marks.
// use JOptionPane.showMessageDialog
JOptionPane.showMessageDialog(null, "The total student marks is
"+total+" and average marks is " +avg);
}

AccountTest.java

package lab2;

import javax.swing.JOptionPane; // For the JOptionPane class

public class AccountTest {

public static void main(String[] args)


{
// TODO Auto-generated method stub
String input; // To hold user input

// Get Account starting balance.


// use JOptionPane.showInputDialog and store in input
input = JOptionPane.showInputDialog("Enter starting balance : ");

// Create a PrepaidAccount object.


PrepaidAccount a = new PrepaidAccount(input);

// Get the amount to reload.


// use JOptionPane.showInputDialog and store in input
input = JOptionPane.showInputDialog("Enter the amount to reload : ");

// Deposit the reload amount into the account.


// Use in methods.
a.in(input);

// Display the new account balance.


// use JOptionPane.showMessageDialog & getBalance
JOptionPane.showMessageDialog(null, "New account balance :
RM"+a.getBalance());

// Get the amount to use


// use JOptionPane.showInputDialog and store in input
input = JOptionPane.showInputDialog("Amount to use : ");

// Subtract the amount from the account balance.


// Use out methods.
a.out(input);

// Display the new account balance


// use JOptionPane.showMessageDialog & getBalance
JOptionPane.showMessageDialog(null, "New account balance :
RM"+a.getBalance());

System.exit(0);

PrepaidAccount.java

package lab2;

public class PrepaidAccount


{
private double balance; // Account balance

// Constructor to set the starting balance


public PrepaidAccount()
{
balance = 0.0;
}

// Constructor to set starting balance - using double type argument


public PrepaidAccount(double startBalance)
{
balance = startBalance;
}

// Constructor to set starting balance - using String type argument


public PrepaidAccount(String str)
{
balance = Double.parseDouble(str);
}

// Method to add amount to balance - using double type argument


public void in(double amount)
{
balance += amount;
}

// Method to add amount to balance - using String type argument


public void in(String str)
{
balance += Double.parseDouble(str);
}

// Method to subtract amount from the balance field - using double type
argument
public void out(double amount)
{
balance -= amount;
}

// Method to subtract amount from the balance field - using String type
argument
public void out(String str)
{
balance -= Double.parseDouble(str);
}

// Method to returns the account balance.


public double getBalance()
{
return balance;
}

You might also like