Question

There is only one discussion this week, but it requires the creation of two programs, a...

There is only one discussion this week, but it requires the creation of two programs, a Class and a Driver class. Notice the grading criteria 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. Show also a screen capture of the driver running your programs.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the code for the following scenario. The Object taken here is Watch.As watch is a object used daily. Here we have taken three attributes,based on that code is written.

public class Watch{

private String color;
private String type;
private String brand;


public String getColor() { // method to return the current color
return this.color;
}
public String getType() { // method to return the current type
return this.type;
}
public String getBrand() { // method to return the current brand
               return this.brand;
}

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

public void setType(String newType) { // method to change type
this.type = newType;
}
public void setBrand(String newBrand) { // method to change brand
               this.brand = newBrand;
}
public static void main (String[ ] args) {
Watch firstWatch; // first Watch declared
Watch secondWatch; // second Watch declared


firstWatch = new Watch(); //Creation of first Watch (instantiation)
secondWatch = new Watch(); //Creation of second Watch (instantiation)

firstWatch.setColor("red");
secondWatch.setColor("red");
secondWatch.setType("Digital");
secondWatch.setBrand("Fossils");
System.out.println("First Watch is " +firstWatch.getColor());
System.out.println("Second Watch is " + secondWatch.getType());
System.out.println("Second Watch is " + secondWatch.getBrand());

}
}

Add a comment
Know the answer?
Add Answer to:
There is only one discussion this week, but it requires the creation of two programs, a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • fisrt one is bicycle.java second code is bicycleapp.java can anyone help this??? please save my life...

    fisrt one is bicycle.java second code is bicycleapp.java can anyone help this??? please save my life Before you begin, DOWNLOAD the files “Lab8_Bicycle.java” and “Lab8_BicycleApp.java” from Canvas to your network drive (not the local hard drive or desktop). Once you have the files saved, RENAME THE FILES Bicycle.java and BicycleApp.java then open each file. 1) Look at the Bicycle class. Two of the many data properties we could use to define a bike are its color and the gear it...

  • no buffered reader. no try catch statements. java code please. And using this super class: Medialtemjava...

    no buffered reader. no try catch statements. java code please. And using this super class: Medialtemjava a Create the "middle" of the diagram, the DVD and ForeignDVD classes as below: The DVD class will have the following attributes and behaviors: Attributes (in addition to those inherited from Medialtem): • director:String • rating: String • runtime: int (this will represent the number of minutes) Behaviors (methods) • constructors (at least 3): the default, the "overloaded" as we know it, passing in...

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

  • I need help writing my main method**** Computer Science 111 Introduction to Algorithms and Programming: Java...

    I need help writing my main method**** Computer Science 111 Introduction to Algorithms and Programming: Java Programming Project #4 – Classes and Objects (20 Points) You will create 3 new classes for this project, two will be chosen from the list below and one will be an entirely new class you invent.Here is the list: Shirt Shoe Wine Book Song Bicycle VideoGame Plant Car FootBall Boat Computer WebSite Movie Beer Pants TVShow MotorCycle Design First Create three (3) UML diagrams...

  • Write a class named Octagon (Octagon.java) that extends the following abstract GeometricObject class and implements the...

    Write a class named Octagon (Octagon.java) that extends the following abstract GeometricObject class and implements the Comparable and Cloneable interfaces. //GeometricObject.java: The abstract GeometricObject class public abstract class GeometricObject { private String color = "white"; private boolean filled; private java.util.Date dateCreated; /** Construct a default geometric object */ protected GeometricObject() { dateCreated = new java.util.Date(); } /** Construct a geometric object with color and filled value */ protected GeometricObject(String color, boolean filled) { dateCreated = new java.util.Date(); this.color = color;...

  • this is for java programming Please Use Comments I am not 100% sure if my code...

    this is for java programming Please Use Comments I am not 100% sure if my code needs work, this code is for the geometric if you feel like you need to edit it please do :) Problem Description: Modify the GeometricObject class to implement the Comparable interface and define a static max method in the GeometricObject class for finding the larger (in area) of two GeometricObject objects. Draw the UML and implement the new GeometricObject class and its two subclasses...

  • This assignment attempts to model the social phenomenon twitter. It involves two main classes: Tweet and...

    This assignment attempts to model the social phenomenon twitter. It involves two main classes: Tweet and TweetManager. You will load a set of tweets from a local file into a List collection. You will perform some simple queries on this collection. The Tweet and the TweetManager classes must be in separate files and must not be in the Program.cs file. The Tweet Class The Tweet class consist of nine members that include two static ones (the members decorated with the...

  • Concepts Tested in this Program: Class Design Constructors Objects Inheritance Program:   Design a class named Person...

    Concepts Tested in this Program: Class Design Constructors Objects Inheritance Program:   Design a class named Person and its two subclasses, Student and Employee. Make Faculty and Staff subclasses of Employee. A Person object has a name, address, phone number, and email address (all Strings). A Student Object has a class status (freshman, sophomore, junior, or senior). Define the status as a final String variable. An Employee Object has an office number, salary (both ints ), and a date hired. Use...

  • Given the follawing tent,TALKSOW DAT um? ?,iLon ㆀ01 09/15/1974 nd te elowing Hot iava class anower she questions that follaw: buffer.append(" BIRTHDAY:"birthday) return buffer.toStrin...

    Given the follawing tent,TALKSOW DAT um? ?,iLon ㆀ01 09/15/1974 nd te elowing Hot iava class anower she questions that follaw: buffer.append(" BIRTHDAY:"birthday) return buffer.toString ) 1. Write a Java class HmWk14. Create the method readTalkshow(list) in the HmWk14 class that that reads the file TALKSHOW.DAT. Each line in the data file will be stored into a Host Java class object and added to a list of Host objects. Make a note that the date in the file needs to be...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT