ComputerScienceExpert

(11)

$18/per page/

About ComputerScienceExpert

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Applied Sciences,Calculus See all
Applied Sciences,Calculus,Chemistry,Computer Science,Environmental science,Information Systems,Science Hide all
Teaching Since: Apr 2017
Last Sign in: 103 Weeks Ago, 3 Days Ago
Questions Answered: 4870
Tutorials Posted: 4863

Education

  • MBA IT, Mater in Science and Technology
    Devry
    Jul-1996 - Jul-2000

Experience

  • Professor
    Devry University
    Mar-2010 - Oct-2016

Category > Programming Posted 24 May 2017 My Price 9.00

Objects and Drivers

There is only one discussion this week, but it requires the creation of two programs, a Class and a Driver class. Notice the grading criterium this week is different than other weeks.

Objects and Drivers

This chapter deals with the idea of objects. An object is ANY entity we want to represent in a computer, either real or imaginary. To describe an object, we actually describe a class of similar objects, giving us the chance of create more than one object with the same description. For example, if we want to represent a chair in Java, we create the class Chair as follows:

public class Chair {
}

Inside this class declaration we need to specify two things: which attributes we want to keep for every chair and which behaviors we want any chair to be able to do.  The attributes are known as the instance variables and the behaviors are known as the methods. For example, for every chair we could specify attributes like its color and the number of legs. Among the behaviors we can care to mention we could have the ability to retrieve the color and the number of legs the chair has (these are called accessor methods) and the ability to change its color and the number of legs it has (these are called mutator methods). All these attributes and behaviors can be described in Java as follows:

public class Chair {

private String color;    // instance variable for color of the Chair
private int numberOfLegs; // instance variable to hold the number of legs

// Accessor Methods

                  public String getColor() {   // method to return the current color
                      return this.color;
                  }                  
                  public int getNumberOfLegs() {   
                                                       // method to return the current number of legs
                      return this.numberOfLegs;
                  }                 

                  // Mutator Methods

                  public void setColor(String newColor) {   // method to change color
                      this.color = newColor;
                  }                 

                 public void setNumberOfLegs(int newLegs) {
                                                              // method to change # of legs
                      this.numberOfLegs = newLegs;
                  }                 

         }

Once we have written this description of a Chair inside the file Chair.java we can actually compile this file, but it will not run. To use it we need another file, known as the driver file. The driver file is another class that will contain a main method. Inside the main method variables of type Chair will be created (instantiated) and used. For example, the following driver class creates two Chairs, set their colors and their number of legs, and prints some of these values:

public class ChairDriver {

  public static void main (String[ ] args) {
     Chair firstChair;          // first Chair declared
     Chair secondChair;     // second Chair declared
    

      firstChair = new Chair();     //Creation of first Chair (instantiation)
      secondChair = new Chair(); //Creation of second Chair (instantiation)

      firstChair.setColor(“red”);             //Setting color of first Chair
      secondChair.setColor(“red”);         //Setting color of second Chair firstChair.setNumberOfLegs(4);     //Setting number of legs for first Chair                   secondChair.setNumberOfLegs(3);      //Setting number of legs for 2nd Chair

      System.out.println(“First chair is ”+firstChair.getColor());
      System.out.println(“Second chair has ”+
      secondChair.getNumberOfLegs()+ “ legs”);
 }

}

Questions

Reply to the following sections:

SECTION 1: Chose any object you want to describe in the computer. Please come up with original objects. Do not copy them from other sources, or select an object somebody else has used in this discussion. There are infinite number of objects, real or imaginary, please be creative. Once you have chosen your object, write a correct class description (as shown above) to represent your object in Java. Give appropriate attributes (at least two) and appropriate behaviors (at least two), all written in correct Java syntax. You may use TextPad to check that the object file compiles.

SECTION 2: Once your class description is finished, write a syntactically correct Java driver class for it. The main method should declare and create objects of the class described in Section 1 and it should use all the methods that were also defined in that Section. This driver must compile and execute in TextPad.

Answers

(11)
Status NEW Posted 24 May 2017 07:05 AM My Price 9.00

-----------

Attachments

file 1495611990-Solutions file 2.docx preview (51 words )
H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly -----------onl-----------ine----------- an-----------d g-----------ive----------- yo-----------u e-----------xac-----------t f-----------ile----------- an-----------d t-----------he -----------sam-----------e f-----------ile----------- is----------- al-----------so -----------sen-----------t t-----------o y-----------our----------- em-----------ail----------- th-----------at -----------is -----------reg-----------ist-----------ere-----------d o-----------n -----------THI-----------S W-----------EBS-----------ITE-----------. ----------- Th-----------ank----------- yo-----------u -----------
Not Rated(0)