Recent Question/Assignment

BN108/BN108D Introduction to Programming
Assignment Part 2: Java development
Submission Due: Tuesday, 7 October 2014 by 7 pm submit via the link on Moodle (note separate links for Melbourne students and for Sydney students).
Total Marks = 30 (15% of total assessment)
Submission instructions
• Create a folder and name it with your student ID (MITnnnnnn, where nnnnnn are digits of your student number)
• Copy your 2 .java and 2 .class files into that folder.
Note: only .java and .class files into the folder.
• Zip the folder.
o Right-click on the folder
o In the pop-up menu select Send-to, Compressed (zipped)Folder
Note: you MUST create a .zip archive.
If you submit .rar, it will be ignored and will not be marked. Late re-submission will be treated as late and relevant penalty will be applied.
Purpose of the assessment
The purpose of this assignment is to demonstrate skills in the object-oriented program development using Java.
Description of the assessment
TASK 1: Modifying a class / Completing partial implementation [8 marks]
TASK 2: Completing user interface design and completing functionality [22 marks]
Problem Description
A small car yard dealing in second hand cars needs an application to keep records of cars in stock. Details of each car shall include make and model (e.g. Toyota Camry; this can be maintained as one String attribute), year of manufacturing, price paid for the car (i.e. purchase price) and selling price.
Selling price is calculated as old price plus mark-up of 30%. For example, Ford Transit bought for $20,000 will have the selling price of 20000 * (1 + 0.3) = 26000.
The car yard does not buy cars manufactured before 2009.
Task Requirements
Below is a partial implementation of the class Car developed by a team member who left the project. You are given the task to take over this work and complete it.
1. Insert missing implementation where specified by comments in the code.
Add your name as a comment.
Make sure the class name contains your student ID (MITnnnnnn should be replaced with your student ID). Make sure that it compiles before proceeding to the next task.
(8 marks)
public class MITnnnnnnCar {

private String makeModel;
private int year;
private double purchasePrice=0;
private double sellingPrice;
private static final double MARK_UP=0.3;
public MITnnnnnnCar(String carModel, int carYear, double pPrice) {
makeModel = carModel;
//calcSellingPrice(); - not needed since it is calculated in setPurchasePrice()

public void calcSellingPrice() {
sellingPrice = //insert the formula here (1 mark)
public void setMakeModel(String newMakeModel) {
makeModel = newMakeModel;

public void setYear(int newYear) {
/* insert if statement here to validate newYear as per the business rule that the car yard does not buy cars manufactured before 2009. If true, assign the value to the attribute. (3 marks) */
public void setPurchasePrice(double newPrice) {
/* insert if statement here to validate newPrice as 0. If true, assign the value to the attribute and call the method calculating selling price (Note selling price must be updated if purchase price is changed. (4 marks) */
public String getMakeModel() {
return makeModel;
public int getYear() {
return year;

public double getPurchasePrice() {
return purchasePrice;
public double getSellingPrice() {
return sellingPrice;

} //end of class
2. The same team member whose task you inherited produced partial CarDetailsTest class shown below. You need to understand the code below. Copy it in a separate file, rename the class as MITnnnnnnCarDetailsTest class (MITnnnnnn is your student ID) and save Add your name as a comment. Check that after renaming the appropriate places in the code, it compiles. Do NOT proceed if the code does not compile!
(1 mark)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MITnnnnnnCarDetailsTest extends JFrame implements ActionListener {
Car car; /*Remember your Car class has student ID as part of its name, so adjust class name */
JTextField jtfMakeModel, jtfYear, jtfPurchPrice; /* text boxes for entering car make/model, year of manufacturing and purchase price */
JButton jbtnDisplay, jbtnClear;
JLabel jlblMakeModel, jlblYear, jlblSellPrice; /* labels to display car make/model, year of manufacturing and calculated sale price */

public CarDetailsTest() {
setSize(500, 210);
setTitle(-Purchased car details-);
setLayout(new GridLayout(7,2));

add(new JLabel(- Enter car make and model: -));
jtfMakeModel = new JTextField();
add(new JLabel(- Enter year of manufacturing: -));
jtfYear = new JTextField();
add(new JLabel(- Enter purchase price: -));
//add a text field here for user to enter purchase price – task 4
//check text field name in the declarations at the top

jbtnDisplay = new JButton(-Display-);
jbtnClear = new JButton(-Clear all-);
//make sure both buttons are registered as event sources
//add both buttons to the window here – task 5

//display car
add(new JLabel(- Car make and model: -));
jlblMakeModel = new JLabel(--);
add(new JLabel(- Year of manufacturing: -));
jlblYear = new JLabel(-0000-);
add(new JLabel(- Sale price: -));
jlblSellPrice = new JLabel(-$0.00-);

public static void main(String[] args) {
CarDetailsTest carWin = new CarDetailsTest();
//make sure that the carWin is showing – task 3


public void actionPerformed(ActionEvent ae) {
String str = ae.getActionCommand();
if (str.equals(-Display-)) {
/* get the value from the text field and put it into the variable carYear */
int carYear = Integer.parseInt(jtfYear.getText());
if (carYear =2009) {
/* if year is valid, get the value of purchase price and from the text field and
check for valid purchase price as 0 */
double pPrice = Double.parseDouble(jtfPurchPrice.getText());
if (pPrice 0) {

/* Proceed with object creation since both year and purchase price values are valid – task 6.
Create a new object from class MITnnnnnnCar as declared at the top of this code.
Remember to use variables after validation where necessary, not text field values. */

/* Task 7 and 8 – Display the object's attributes: car make and model, year of manufacturing and re-sale price using appropriate JLabel objects. Remember JLabel works with String only */
else //pPrice =0 - invalid
JOptionPane.showMessageDialog(null, -Invalid purchase price, please re-enter-);
} //carYear =2009
else //invalid carYear
JOptionPane.showMessageDialog(null, -Invalid year of manufacturing, please re-enter-);
} //if Display
//not Display button, then check if it’s Clear all button
if (str.equals(-Clear all-)) {
//remove text from all text fields – task 10b

//clear labels – task 10a
/* set text of labels showing year of manufacturing and sale price to 0 as it was in the initial screen */
} // actionPerformed
} //end of class
3. Add the line of code to the main() method that shows the window. Ensure that both .class files are in the same folder. Run the code.
(1 mark)
4. Add a text field to accept purchase price where the comment requests it.
(1 mark)
5. Make sure that both buttons are registered as event sources. Add both buttons to the window where the comment requests it. Compile and run the code. Do NOT proceed if the code does not compile!
(4 marks)
6. Create a new MITnnnnnnnCar object where the comment requests it. Name this object car (note it has been declared at the top of the class). Consult the MITnnnnnnnCar class source code. Remember to use the value from the text field for the make/model and the validated values for year and purchase price. Compile and run the code. Do NOT proceed if the code does not compile!
(4 marks)
7. Remove the comment from the line “jlblMakeModel.setText(car.getMakeModel());”. Compile and run the code. Do NOT proceed if the code does not compile!
Enter some values into text boxes (e.g. Toyota Camry as make and model, 2013 as year of manufacturing, 1000 as purchase price) and click on Display.
(This task - 0 marks)
8. Add similar code to display year of manufacturing and selling price (NOT purchase price). Compile and run the code. Do NOT proceed if the code does not compile!
(4 marks)
9. Test your code with the purchase price entered as 0.
Now test your code with year of manufacturing below 2009 (e.g. 2000).
In the appropriate place add a comment to your code explaining what the effect of the code “JOptionPane. showMessageDialog()” is.
(1 mark)
10. Complete functionality of the “Clear all” button:
a. Add code to replace text in labels showing year and selling price with 0000 and $0.00 respectively. Compile and run the code. Do NOT proceed if the code does not compile! Test that the “Clear all” button removes text (car details) from all 3 labels.
(3 marks)
b. Add code to remove text from all text fields. Compile and run the code. Enter a sample car data and click on Display. Then click “Clear all”. All text fields and labels showing data should now be cleared.
(3 marks)

Looking for answers ?