Home‎ > ‎ICT Programming‎ > ‎

BlueJ


Java:  Java is a programming language and computing platform first released by Sun Microsystems in 1995. It is the underlying technology that powers state-of-the-art programs including utilities, games, and business applications. Java runs on more than 850 million personal computers worldwide, and on billions of devices worldwide, including mobile and TV devices.

Let's Begin Here:  The JAVA Tutorials


Key Programming Terms

Create definitions for the following terms and place these in your homework section.
Create a heading that says "Glossary" and place the definitions under that.
  • objects
  • classes
  • methods
  • parameters
  • data types
  • fields
  • constructors
  • accessor methods 
  • mutator methods
  • assignment
  • conditional statement
  • local variables
  • instance variables
  • boolean
  • integer
  • string
  • variable
You may work in pairs and you may use any resource you choose such as google searches and/or the textbook chapters (in the handout folder).

Here are some resources to help you begin, use these and other resources to help you complete the assignment.



YouTube Video



Refining Your Understanding of Classes and Objects

When you program in Java, you work constantly with classes and objects. These two ideas are really important.

Close your eyes for a minute and think about what it means for something to be a chair.

A chair has a seat, a back, and legs. Each seat has a shape, a color, a degree of softness, and so on. These are the properties that a chair possesses. What is described is chairness — the notion of something being a chair. In object-oriented terminology, this describes the chair class.

Now take a minute to look around your room. (If you're not sitting in a room right now, fake it.)

Several chairs are in the room, and each chair is an object. Each of these objects is an example of that ethereal thing called the Chair class. So that's how it works — the class is the idea of chairness,and each individual chair is an object.

A class isn't quite a collection of things. Instead, a class is the idea behind a certain kind of thing. When we talk about the class of chairs in your room, we're talking about the fact that each chair has legs, a seat, a color, and so on. The colors may be different for different chairs in the room, but that doesn't matter. When you talk about a class of things, you're focusing on the properties that each of the things possesses.

It makes sense to think of an object as being a concrete instance of a class. In fact, the official terminology is consistent with this thinking. If you write a Java program in which you define a Chair class, each actual chair (the chair that you're sitting on, the empty chair right next to you, and so on) is called an instance of the Chair class.

Here's another way to think about a class. Imagine a table displaying all three of your bank accounts. (See Table 1.)

Table 1 A Table of Accounts

Account Number

Type

Balance

16-13154-22864-7

Checking

174.87

1011 1234 2122 0000

Credit

-471.03

16-17238-13344-7

Savings

247.38

Think of the table's column headings as a class, and think of each row of the table as an object. The table's column headings describe the Account class.

According to the table's column headings, each account has an account number, a type, and a balance. Rephrased in the terminology of object-oriented programming, each object in the Account class (that is, each instance of the Account class) has an account number, a type, and a balance. So, the bottom row of the table is an object with account number 16-17238-13344-7. This same object has type Savings and a balance of 247.38. If you opened a new account, you would have another object, and the table would grow an additional row. The new object would be an instance of the same Account class.


In Class Demonstration


Dog Class:

All dogs have 4 legs, ears, a tail, eyes, nose, fur, canine teeth, etc.

Your dog has:

Colour: ‘Gold’ <- String

Name: ‘Chelsea’ <- String

WeightLBS: 20 <- int

Breed: ‘Lab’ <-String

Male/Female: ‘Female’ <- String

IsNeutered?: No <-Boolean

 

Strings – combination of letters

Integers - numbers

Floating Points – decimal numbers

Boolean – yes or no answer







BlueJ - Week 1 Assignments
Monday

In Class we viewed the Chapter 1 Power Point

Read through Chapter 1 and completed the activities

Analyzed this Dog Class


Began activity 1

Work with a partner. For this lab you will begin to create a class called Student. Here are the

attributes of a student that we are concerned with for the purpose of this project:

 first name

 last name

 student number, e.g. A00012345

 email address

 number of courses completed

 grade, e.g. 78.5

 is the student in good standing? true or false


Choose appropriate data types and descriptive names for the fields (instance variables), and

declare them. Be sure to specify that they are private.


Write a constructor for this class. The constructor will expect all the information to be passed in

as parameters. Choose descriptive names for the parameters but remember that they cannot be

the same as the field names. Use the parameters to initialize the fields.


Test your project by creating a Student object. Use the BlueJ inspector to check the contents of

the fields.


Be sure both partners have a copy of the completed project.

Due end of class on Thursday

Grade 12  Assignment: Guessing Game - due end of class on Thursday


Adding Mutator Methods
An accessor is a method that accesses the contents of an object but does not modify that object. In the simplest case, an accessor just returns the value of one of the fields. In general, an accessor performs some computation using the fields as long as that computation does not modify any of the fields.


A
mutator is a method that can modify an object. In the simplest case, a mutator just assigns a new value to one of the fields. In general, a mutator performs some computation and modifies any number of fields.

Sometimes, accessors are called 'getter' methods and mutators are called 'setter' methods.


Task: add a mutator method to each object in the "Student Class"



Assignment - Cell Phone Class


For this lab you will begin to create a class called CellPhone. Here are the attributes of a cell
phone that we are concerned with for the purpose of this project:

 brand, e.g. Sony Ericson
 model, e.g. W200a
 weight in grams
 megabytes of memory, e.g. 25
 lines of text, e.g. 5 (if this doesn't apply to your phone - add a field for "Does it have a touchscreen?")
 does it have a camera?
 does it have an alarm clock?
 does it have games?
 does it vibrate?

Choose descriptive names for the fields (instance variables) and declare them. Be sure to specify
that they are private.

Write a constructor for this class. The constructor will expect all the information to be passed in
as parameters. Choose descriptive names for the parameters but remember that they cannot be
the same as the field names. Use the parameters to initialize the fields.


Assignment - Customer Class

For this lab you will write a Customer class. Here are the attributes of a customer that we are
concerned with for the purpose of this project:

 first name
 last name
 customer ID, e.g. C000987
 phone number, e.g. 604-345-1234
 email address
 number of reward points accumulated
 is the customer in good standing? true or false

All values must be passed to the constructor as parameters to initialize the fields.
For each field in the class, write both an accessor and a mutator method. 

The accessor methods must start with “get” and the mutator methods with “set”.

Also write a method called displayInfo() that displays all the customer's information on the
screen, like this:

Customer: Andrew Jackson
ID: C000987
Email: ajackson@my.bcit.ca
Phone: 604-345-1234
Reward points: 78
Good standing? true

See completed Customer Class here

Extension

If the customer has more than 100 reward points he/she is elligible to receive a free gift.

You must create an "Accessor Method" that calculates whether the customer has enough points to receive a gift. It will be a boolean data type but the answer will return "true or false" depending on whether the customer has the correct number of points.

Assignment - Fitness Club Member

Due, end of class on Tuesday next week
Work with a partner. A local fitness club needs to model the attributes and behaviors of their
club members. Write a class called Member

Include appropriate comments for each class element.

Here are the relevant attributes of a Member object. These must be the ONLY fields in your
class. All the attributes must be passed to the object constructor and initialized in the constructor.

· first name
· last name
· height (inches)
· weight (pounds)
· year of birth
· year of joining the club
· current year

Here are some of the behaviors of a Member object. Each of these is a method that returns the
specified information when it is invoked.

· getFullName returns the member’s full name, e.g. “Joe Smith”
· getHeightInches returns the member’s height
· getWeightPounds returns the member’s weight
· calculateYearsJoined returns the number of years the person has been a member
· calculateAge returns the calculated age of the member
· calculateAgeAtJoining returns the age the member was when they joined
· getsDiscount returns true if the member qualifies for a discount. 

People who qualify for a discount are either:

- under the age of 12 and members for at least two years
- at least 65 years of age
- members for at least 10 years


BlueJ Final Project

Recommended: Work with a partner

Your client (“Sara's School Supplies”) is a small store that sells a variety of
supplies useful to students. You have been requested to develop an objectoriented
program to help the Sara's School Supplies staff keep track of store
inventory. You begin by creating a prototype for an item to be sold in the store.

  • · Create a new BlueJ project called SchoolSupplies.
  • · Create a class called SchoolItem. Write a summary comment that explains the class.
  • · Define instance variables (fields) to hold the following pieces of data: description, stock code (e.g. 7421 WHI), quantity in stock, quantity sold, purchase price and selling price. Choose descriptive names for your instance variables, and use appropriate data types. 
  • · Create a constructor that has parameters for descriptionstock codepurchase price and selling price. The constructor must check both “price” parameters to be sure they are not below zero. If one of them is negative, set the relevant field to zero. The other numeric fields must be initialized to zero by default. Provide a summary comment for the constructor.
  • · For each field in the class, provide an accessor method that returns the value of the field. These methods must be named to start with “get”, e.g. getQuantityInStock().
  • · Provide a method called restock(). This mutator method takes a parameter that specifies the number of items to add to inventory, and adds that number to the quantity in stock. If the parameter is a negative number it will display an error message and not change the quantity in stock.
  • · Provide a method called sell(). This mutator method subtracts the parameter value from the quantity in stock, but only if the parameter is not negative. Quantity in stock must never be a negative number. If the parameter is negative or too high, an error message will be displayed and the quantity in stock will not change. The sell() method will also change the quantity sold field by the appropriate amount.
  • · Provide a method called setPurchasePrice(). This mutator method accepts the new purchase price as its parameter and changes the appropriate instance variable, but only if the parameter is not below zero.
  • · Provide a method called setSellingPrice(). This mutator method accepts the new selling price as its parameter and changes the appropriate instance variable, but only if the parameter is not below zero.
  • · Add a method called calcProfit() that calculates and returns the profit from selling one item, based on the purchase price and selling price. This method will not display anything on the screen. Comment the calculation.
  • · Add a method called calcTotalProfit() that calculates and returns the profit from all the items that have been sold. This method must invoke calcProfit().
  • · Add a method called calcInventoryValue() that calculates and returns the current value of the quantity in stock, based on purchase price.
  • · Provide a method called printDetails(). This method displays item information on the screen, e.g.
Description: 3-ring binder
Stock code: 1765 BIN
Purchase price: $2.43
Selling price: $5.95
Quantity in stock: 34
Quantity sold: 12
Inventory value: $82.62
Profit on sales: $42.24
  • · Be sure each method has a Javadoc-style descriptive comment above the method header and that the class itself has a descriptive comment that includes your name as author.
Use BlueJ to interactively test your methods as you write them. Write them one
at a time, and test each immediately to be sure it is correct. Testing requires
creating an object and invoking the method. Be sure to test with both valid and
invalid data, i.e. test the “set” methods with positive values to be sure the field is
changed correctly, and test with negative values to be sure the error message is
displayed and the field not changed.
Marks will be given for:
  • · Comments – appropriate and complete.
  • · Style – see the style guide Appendix J of your textbook.
  • · Correctness and completeness – code meets the requirements listed above.
Create a .zip file containing your entire BlueJ project (zip the folder, not the
individual files). Name the .zip file with your name and the assignment number,
e.g. “Susan_Wong_Assign_1.zip” and place it in the hand-in folder by the end of class on Thursday, December 15th
Ċ
Jon Hamlin,
Dec 6, 2011, 1:59 PM
Comments