ICS 111, Introduction to Computer Science
Lab 14 - Creating Objects - November 4
To receive credit for this lab assignment, you must be finished by
Thursday, November 6, at 11:59 p.m. After this time you will not receive any credit. There will be only one lab this week.Instructions:
1. Login to UNIX
2. Create and edit a Java program in Pico to do the following:
- Create an object: The object should be stored in a class called "CardHolder.java" Here are the class skeletons / methods that you should write:
==================================================
public class CardHolder{
// instance variables
private String sName;
private int iCardNum;
private double dBalance;
//constructors
//this first constructor adds by default $20 to the balance //as annual fee.
public CardHolder(String sName, int iCardNum){
} //constructor
//this second constructor does not add an anual fee
public CardHolder(String sName, int iCardNum, double dBalance){
} //constructor
//getMethods
public int getCardNumber( ){ //returns the card number (iCardNum)
}
public String getName( ){ // returns the String sName
}
public double getBalance( ){ // returns the balance dBalance
}
//set methods
public void setCardNumber(int newCardNumber ){ // sets a new CardNumber iCardNumber
}
public void setName(String sName){ // sets a new Name sName
}
public void setBalance(double newBalance){ // sets a new balance dBalance
}
//other methods
//returns a printable version of the object
public String toString( ){
}
//compares two cardholder objects
public boolean equals(CardHolder otherCardHolder){
}
}// class CardHolder
- Then test your code using the following class:
==================================================
import java.io.*;
public class CardTest{
public static void main (String arg[ ]){
int iCardNum = 0;
String sName = "";
String sInput = "";
double dBalance = 0;
//**********************************
sName = "Jane Doe";
iCardNum = 123456;
dBalance = 231.50;
CardHolder ch1 = new CardHolder(sName, iCardNum, dBalance);
System.out.println(ch1.toString( ));
ch1.setBalance(300);
System.out.println(ch1.toString( ));
//**********************************
sName = "John Smith";
iCardNum = 335547;
CardHolder ch2 = new CardHolder(sName, iCardNum);
System.out.println("This should print the balance as $20");
System.out.println("because of the anual fee\n");
System.out.println(ch2.toString( ));
ch2.setName("Johnny Smith");
System.out.println("After the name change:\n"+ch2.toString( ));
//**********************************
CardHolder ch3 = new CardHolder("Johnny Smith", 335547);
System.out.println("This should print the balance as $20");
System.out.println("because of the anual fee\n");
System.out.println(ch3.toString( ));
System.out.println(ch3.toString( ));
//**********************************
if(ch3.equals(ch2)){
System.out.println("your equals method seems to be working");
}
else{
System.out.println("your equals method is not working properly");
System.out.println("ch3 is equal ch2");
}
//**********************************
if(ch2.equals(ch1)){
System.out.println("your equals method is not working properly");
System.out.println("ch1 is not equal to ch2");
}
else{
System.out.println("your equals method seems to be working");
}
} //main method ends
}// end of class
==================================================
- Note: The above code isn't guaranteed to work as it is, and it doesn't follow the Java Coding Standard- it is just a skeleton to get you started. What you turn in MUST follow the Java Coding Standard rules!
- Note: The above driver class (CardTest.java) WILL BE USED TO TEST YOUR CODE. Therefore, you ONLY NEED TO SUBMIT THE FIRST CLASS (CardHolder.java). MAKE SURE YOUR CODE WORKS WITH THE PROVIDED CLASS- do not submit a modified driver class, because your program will be tested out with the class shown above!
- Before you submit your code, make sure that your code follows the rules of the Java Coding Standard. Visit this link to look up Java code formatting rules and be sure that your program adheres to these guidelines. At least follow the rules of proper indenting / spacing, making a comment block for classes, making a comment block for methods (main method for this lab), and do some inline commenting to explain what your program is doing. Not doing ALL of these will result in a deduction of points on your lab.
3. Compile and run the program. Be sure to thoroughly test the program after getting it compiled and running to make sure it meets ALL of the requirements above.
4. Get into Pine, and attach the file to an e-mail
5. Send the e-mail / file(s) to the ics111-lab@hawaii.edu account.
6. Check the webpage to make sure that the e-mail arrived. Note that this will be sent to the ICS 111 lab address (ics111-lab@hawaii.edu). This one is different from the ics111-homework@hawaii.edu. There are two different ones so we can have homework assignments and lab assignments separate.
Here is the URL:
http://www2.hawaii.edu/~tp_200/bmf/ics111-lab.html
7. Once I (TA) get the e-mail in my account AND I can verify it on the web, your lab assignment will be graded. For grading policies on your lab, click here...
Link to the Java API - to look up any methods of any class you would like to use.
Link to the Java Coding Standard - to look up Java code formatting rules.