View this PageEdit this Page (locked)Attachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide

Questions: Which Method? Class or Object?

Ask your questions here(?????????? see below)

Fellow Class/TeamMates/Teacher/Teacher Assistants
Please read and check the responses to Homework assignment 6/2/04
of Chapter 3-16
Are they correct? Are the explanations sufficient?

Case 1: Reading a file and create student objects from the info in the file
Best Method 1: Class method declared by using "static"
Explanation 1: the counter that keeps track of each record of name, info in Student1, Student2, Student3 objects will be able to count "the same way" for all the students.
Barbara Ericson
If you are reading a file of student information you don't have a student object that you are working on until after you read the information from the file and use the information to create the student objects. This should be a class (static) method.
=========================================
Case 2: To get the gpa for each student
Best Method 2: Object method
Explanation 2: the gpa should be something unique to each object Student1, Student2, Student3. If class method approach is chosen then all gpas will be the same.
Barbara Ericson This can not be done in a class method. The gpa is for a student object and class methods do not have access to any object data. Since the method operates on a student object's data
it should be an object method.
=========================================
Case 3: To get the number of student objects created
Best Method 3: Object method???????????
Explanation 3: get Student.count is enough without static
however the student counter was set to
static.?????????????

=====================================
Case 4: To print the information in a student object
Best Method 4: Object method
Explanation 4: printing information for each record is unique
=====================================

From Sheria(Benjamin E. Mays High)

Here is some code from AP class regarding using API to get constructors:

import java.lang.reflect.;
Class stringClass = "Test".getClass();
Class[] classArray = new Class[1];
classArray[0] = stringClass;
Constructor con = stringClass.getConstructor(classArray);
Object[] objArray = new Object[1];
objArray[0] = "hi";
String test = (String) con.newInstance(objArray);
test
"hi"

=================================
6/4/04 Assignment: Slide3-38 : by Sheria Enahora

================================
6-4-04 Assignment: Constant Exercise Slide 3-41 : by Sheria Enahora
 
/**
 * Class that represents a person.  A person has a first name,
 * last name, and a file with their picture in it
 */
public class Person 
{
  //Mr E constants
  
  ////////////// fields //////////////////////////////
  //Mr E constants
  public static final int UNKNOWN =0;
  public static final int FEMALE = 1;
  public static final int MALE = 2;
 
  private int gender = UNKNOWN; //default
  
  
  
  
  
  /** first name of this person */
  private String firstName;
  
  /** last name of this person */
  private String lastName;
  
  /** the name of a file that has a picture of the person */
  private String pictureFile;
  
  //////////////// constructors //////////////////////////
  
  /** 
   * No argument constructor.  All fields have their default values 
   */
  public Person() {}
  
  /**
   * Constructor that takes the first and last names
   * @param firstName the first name to use
   * @param lastName the last name to use
   */
  public Person(String firstName, String lastName)
  {
    // set the first name and last name
    this.firstName = firstName;
    this.lastName = lastName;
  }
  
  /**
   * Constructor that takes all the data
   * @param firstName the first name to use
   * @param lastName the last name to use
   * @param pictureFile the name of the picture file
   */
  public Person(String firstName, String lastName, String pictureFile)
  {
    // call the other constructor that takes the first and last names
    this(firstName,lastName);
    
    // set the picture file
    this.pictureFile = pictureFile;
  }
  
  ////////////////////////// methods //////////////////////////////
  //MrE insert gender methods
  public int getGender(){ return gender;}
  public void setGender(int gender){
    this.gender=gender;
  
  }
  /** 
   * Method to get the first name
   * @return the first name
   */
  public String getFirstName() { return firstName; }
  
  /**
   * Method to get the last name
   * @return the last name
   */
  public String getLastName() { return lastName; }
  
  /**
   * Method to get the picture file name 
   * @return the name of the file with a picture of the person in it
   */
  public String getPictureFile() { return pictureFile; }
  
  /**
   * Method to set the first name
   * @param firstName the first name to use
   */
  public void setFirstName(String firstName) { this.firstName = firstName; }
  
  /**
   * Method to set the last name
   * @param lastName the last name to use
   */
  public void setLastName(String lastName) { this.lastName = lastName; }
  
  /**
   * Method to set the picture file name
   * @param fileName the file name of the picture of this person
   */
  public void setPictureFile(String fileName) { pictureFile = fileName; }
  
  /**
   * Method to return a string with information about this person
   */
  public String toString()//implicit this.toString()
  {
    return firstName + " " + lastName+" "+gender;// implicit this.gender
  }
  
  /**
   * Main method for testing
   */
  public static void main(String[] argv) 
  {
    Person sally = new Person("Sally","Summers",null);
     sally.setGender(UNKNOWN);
    
    System.out.println(sally);
    
    
    sally.setGender(FEMALE);
    
    System.out.println(sally);
     sally.setGender(MALE);
    
    System.out.println(sally);
    
  }
  
}
Welcome to DrJava.
> java Person

Sally Summers 0

Sally Summers 1

Sally Summers 2

>


> here is Schram's Exposure Java Site http://www.schram.org

A good web page http://www.edhsonline.org/other/ap/index.html

If you are wondering what is the price for the Lap Top, here is
your answer:Lowest price: $1719.00
Manufacturer: Dell Computer Corp.
Part number: 100L


Link to this Page