Вы находитесь на странице: 1из 6

COE618 Lab 1: An Introduction to Netbeans

General Lab Rules


1. Each lab must have its own directory (folder). For the first lab, the directory is lab1, for the second lab2, etc. 2. To submit a lab, create a zip file of the entire directory and sent it as an attachment to your lab instructor. 3. The deadline for submission is 24 hours before the beginning of the next lab. For example, if you start lab 1 (a 2-week lab) at 10 AM Tuesday Jan 16, you have to submit lab1 by 10 AM Monday, January 29. REQUIRED EXERCISE To get started you must perform the following operations. 1. Create your course directory with the command: mkdir coe618 2. Change to your course directory with the command: cd coe618 3. Create your lab1 sub-directory with the command: mkdir lab1 4. Change to your lab1 directory with the command: cd lab1

Part A: Netbeans tutorial


1. Do the http://www.netbeans.org/kb/55/quickstart.html tutorial. This tutorial takes you through the steps of creating the standard hello world application. (Note that you do not need to include this application in your lab1 directory or in your submission.) 2. Do the junit tutorial from the link http://www.netbeans.org/kb/55/javase-intro.html

Part B: Exercise 1. (30 marks)


1. In the Netbeans program, click on Project > New Project and save it as "Exercise1" on your lab1 directory. 2. Create a new class called Invoice: - Assume that this Invoice class is one that a store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables - a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (type double).

- The Invoice class should have a constructor that takes four arguments (part number, part description, quantity of the item being purchased and price per item) and initializes the four instance variables. - Provide setter and getter methods for each instance variable. - The quantity of the item being purchased and the price per item should always be greater or equal to zero. You need to enforce this restriction in the constructor and the relevant setter methods: if negative values are provided for quantity of item and the price per item, then set their values to zero. - Provide a method named getInvoiceAmount that calculates the invoice amount (i.e. multiplies the quantity by the price per item), then returns the amount as a double value. - Implement the toString method for this class that returns a string containing the values of the four instance variables. Create a new class called InvoiceApplication that creates an instance of Invoice class and invokes various methods on it: - Copy the following code in the InvoiceApplication class and implement the body of the main method as indicated by the comments.

public class InvoiceApplication { public static void main(String[] args) { //Create first invoice invoice1 with the //following data //part number: 123A, part description: calculator, // quantity of item: 3, price per item: 25.00 //Call the relevant setter method to set the //quantity of items to 8. //Print the invoice by invoking toString //Print the invoice amount (use getInvoiceAmount) } }

Exercise 2. (40 marks)


In the Netbeans program click on Project>New Project and save it as "Exercise2" on your lab1 directory. Now, click on the button that says "New Class". Create new classes. Save these classes to your directory and study it to see how it works.

Consider the following hierarchy of classes, with various details to be filled in: // Person.java: abstract base class public abstract class Person { public abstract double getWages(); // abstract, so omit body }
2

// Employee.java: the class Employee public class Employee extends Person { protected String name; // in the form "Last, First" protected String address; // constructor public Employee(String n, String a) { // first name, then address // Fill in for Part a. } // convert the point into a String representation public String toString() { return "[" + name + ", " + address + "]"; } public double getWages() { // must be either faculty or staff for wages return 0.0; } }

// Faculty.java: the class Faculty public class Faculty extends Employee { // inherits from Employee protected double salary; // Constructor public Faculty(double s, String n, String a) { // salary, name, address // Fill in for Part b. } // convert the Faculty class to a String public String toString() { // Fill in for Part c. } public double getWages() { return salary; } } // Staff.java: the class Staff public class Staff // Fill in for Parts d., e., f. }

// Persons.java: test the Person-Employee-Faculty-Staff hierarchy public class Persons { public static void main (String[] args) { Person[] persons = new Person[3]; persons[0] = // Fill in for Part g. persons[1] = // Fill in for Part g. persons[2] = // Fill in for Part g. printPersons(persons); System.out.println(); } public static void printPersons(Person[] s) { for (int i = 0; i < s.length; i++) System.out.println(s[i]); } } a. Fill in the constructor for Employee above. b. Fill in the constructor for Faculty above, using the Employee constructor with super. c. Fill in the toString method for Faculty above, using the Employee toString with super. d. Write a Staff class that inherits from Employee (that is, extends Employee). The Staff class should have data fields double hourlyRate and int hoursWorked. getWages method of the Staff class will return hourlyRate*hoursWorked. e. Write a constructor for Staff that takes parameters the two new ones above, followed by the name and address. f. Write a toString method for Staff above, using the Employee toString with super. g. In the Persons class above, create new objects of each of the three kinds: Employee (with name "Nobody, Nonce", and address "120 Worry Lane") Faculty (with salary 2020, name "Blow, Joe", and address "222 Center St.") Staff (with hourly rate 7.5, hours worked 40, name "Bonkers, Bruce", and address "1 Prime Ave").

Exercise 3. (30 marks)


TwoDShape class is an abstract class that has two instance variables: x and y. The variables x and y represents the x-coordinate and y-coordinate of the top left corner of a two dimensional shape object. public abstract class TwoDShape { private int x; private int y; /**
4

* Constructor * @param x the x-coordinate of the top left corner * @param y the y-coordinate of the top left corner */ public TwoDShape( int x, int y ){ this.x = x; this.y = y; } public int getX() { return x;} public int getY(){ return y;} public void setX(int x){ this.x = x;} public void setY(int y){ this.y = y;} public String toString (){ return (x+ + y);} public abstract void scale( double factor ); } a) Create a new class called Circle as a subclass of TwoDShape. It has one instance variable: radius. Provide a constructor that initializes the instance variables. It takes three parameters: x, y, radius. If the specified radius is negative, then it should throw an IllegalArgumentException. Implement the scale(double factor) method. If the specified factor is positive, then this method should scale its radius by a factor of factor. Otherwise, it should throw an IllegalArgumentException. Provide getter method for radius. b) Write a unit test method called scaleTest that test method scale of Circle class. Please test the exceptional cases, for e.g. invoke the scale method with a negative argument and check if it really throws an IllegalArgumentException. (Use JUnit framework for implementing the test class).

Submitting your lab


(1) Zip your lab1 assignment within your coe618 directory zip r lab1 lab1 (2) Attach your lab1.zip file and send e-mail to your lab instructor, subject of your e-mail must be lab1 followed by your name, student id and section #. Mr. Kiu Kwan Leung: kleung@ee.ryerson.ca

Вам также может понравиться