ankara üniversitesi bilgisayar mühendisliği blm240 programlama

advertisement
ANKARA ÜNİVERSİTESİ
BİLGİSAYAR MÜHENDİSLİĞİ
BLM240 PROGRAMLAMA DİLLERİ
LAB # 4
1. (40 pts ) Aşağıda verilen Account classının konstructor, deposit ve withdraw methodlarını
modifiye ederek ilk açılan hesabın balancının 0 dan büyük olmasını, para çekerken
balancedan fazla olmamasını, para yatırma işlemi sırasında negatif değer kabul etmemesini
sağlayın ve her hatada uygun mesaj gösterin.
//********************************************************************
// Account.java
Author: Lewis/Loftus
//
// Represents a bank account with basic services such as deposit
// and withdraw.
//********************************************************************
import java.text.NumberFormat;
public class Account
{
private final double RATE = 0.035;
// interest rate of 3.5%
private long acctNumber;
private double balance;
private String name;
//----------------------------------------------------------------// Sets up the account by defining its owner, account number,
// and initial balance.
//----------------------------------------------------------------public Account (String owner, long account, double initial)
{
name = owner;
acctNumber = account;
balance = initial;
}
//----------------------------------------------------------------// Deposits the specified amount into the account. Returns the
// new balance.
//----------------------------------------------------------------public double deposit (double amount)
{
balance = balance + amount;
return balance;
}
//----------------------------------------------------------------// Withdraws the specified amount from the account and applies
// the fee. Returns the new balance.
//----------------------------------------------------------------public double withdraw (double amount, double fee)
{
balance = balance - amount - fee;
return balance;
}
//----------------------------------------------------------------// Adds interest to the account and returns the new balance.
//----------------------------------------------------------------public double addInterest ()
{
balance += (balance * RATE);
return balance;
}
//----------------------------------------------------------------// Returns the current balance of the account.
//----------------------------------------------------------------public double getBalance ()
{
return balance;
}
//----------------------------------------------------------------// Returns a one-line description of the account as a string.
//----------------------------------------------------------------public String toString ()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
return (acctNumber + "\t" + name + "\t" + fmt.format(balance));
}
}
2. (40 pts) Aşağıda verilen Fahrenheit programını üzerinde gerekli değişiklikler yaparak
kullanıcının butona basmasıyla dönüştürme işlemini yapmasını sağlayın.
//********************************************************************
// Fahrenheit.java
Author: Lewis/Loftus
//
// Demonstrates the use of text fields.
//********************************************************************
import javax.swing.JFrame;
public class Fahrenheit
{
//----------------------------------------------------------------// Creates and displays the temperature converter GUI.
//----------------------------------------------------------------public static void main (String[] args)
{
JFrame frame = new JFrame ("Fahrenheit");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
FahrenheitPanel panel = new FahrenheitPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
//********************************************************************
// FahrenheitPanel.java
Author: Lewis/Loftus
//
// Demonstrates the use of text fields.
//********************************************************************
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FahrenheitPanel extends JPanel
{
private JLabel inputLabel, outputLabel, resultLabel;
private JTextField fahrenheit;
//----------------------------------------------------------------// Constructor: Sets up the main GUI components.
//----------------------------------------------------------------public FahrenheitPanel()
{
inputLabel = new JLabel ("Enter Fahrenheit temperature:");
outputLabel = new JLabel ("Temperature in Celsius: ");
resultLabel = new JLabel ("---");
fahrenheit = new JTextField (5);
fahrenheit.addActionListener (new TempListener());
add
add
add
add
(inputLabel);
(fahrenheit);
(outputLabel);
(resultLabel);
setPreferredSize (new Dimension(300, 75));
setBackground (Color.yellow);
}
//*****************************************************************
// Represents an action listener for the temperature input field.
//*****************************************************************
private class TempListener implements ActionListener
{
//-------------------------------------------------------------// Performs the conversion when the enter key is pressed in
// the text field.
//-------------------------------------------------------------public void actionPerformed (ActionEvent event)
{
int fahrenheitTemp, celsiusTemp;
String text = fahrenheit.getText();
fahrenheitTemp = Integer.parseInt (text);
celsiusTemp = (fahrenheitTemp-32) * 5/9;
resultLabel.setText (Integer.toString (celsiusTemp));
}
}
}
3. (20 pts) Take Home Question
Trafik lambasını simüle eden bir Java uygulaması yazın. Butona basılmasıyla sırasıyla
kırmızı, sarı ve yeşil yanmasını sağlayın.
Download