Question

use java follow my code: public class q1 {    // Instance Variables    int currentFloor;...

use java

follow my code:

public class q1 {
   // Instance Variables
   int currentFloor;
   double maximumWeight;
   int topFloor;
  
   // Constructor Declaration of Class
   public q1 (int currentFloor, double maximumWeight, int topFloor)
   {
   this.currentFloor = currentFloor;
   this.maximumWeight = maximumWeight;
   this.topFloor = topFloor;
   }
  
   // Property to get value of currentFloor instance variable
   public int getCurrentFloor()
   {
   return currentFloor;
   }
  
   // Property to get value of maximumWeight instance variable
   public double getMaximumWeight()
   {
   return maximumWeight;
   }
  
   // Property to get value of topFloor instance variable
   public int getTopFloor()
   {
   return topFloor;
   }
   }

Instantiate a couple of

different Elevator

objects

Set their data members

Use Information Hiding

Labwork on Arrays Northwestern Polytechnic University Dr. Nels Vander Zanden 3

Add Access Specifiers

Write get/set methods

Add a second constructor

Add methods

upOneFloor -- note cannot

exceed the top floor

downOneFloor – cannot go

below floor one

isAtTopFloor -- return true if

on the top floor, false

otherwise

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

public class q1 {
// Instance Variables
int currentFloor;
double maximumWeight;
int topFloor;
  
// Constructor Declaration of Class
public q1 (int currentFloor, double maximumWeight, int topFloor)
{
this.currentFloor = currentFloor;
this.maximumWeight = maximumWeight;
this.topFloor = topFloor;
}

// Copy constructor
public q1(Elevator el)
{
   this.currentFloor = el.getCurrentFloor();
   this.maximumWeight = el.getMaximumWeight();
   this.topFloor = el.getTopFloor();
}
  
// Property to get value of currentFloor instance variable
public int getCurrentFloor()
{
return currentFloor;
}
  
// Property to get value of maximumWeight instance variable
public double getMaximumWeight()
{
return maximumWeight;
}
  
// Property to get value of topFloor instance variable
public int getTopFloor()
{
return topFloor;
}

// Property to set the value of the currentFloor
   public void setCurrentFloor(int currentFloor)
   {
       if(currentFloor >=1 && currentFloor <= top_floor)
           this.currentFloor = currentFloor;
   }
  
   // Property to set the value of the maximumWeight
   public void setMaximumWeight(double maximumWeight)
   {
       if(maximumWeight > 0)
           this.maximumWeight = maximumWeight;
   }
  
   // Property to set the value of topFloor
   public void setTopFloor(int topFloor)
   {
       if(topFloor > 1)
           this.topFloor = topFloor;
   }
  
   // method to make the elevator go up by 1 floor, if possible
   public void upOneFloor()
   {
       if(currentFloor < topFloor) // if not reached the top floor
           currentFloor++;
   }
  
   // method to make the elevator go down by 1 floor, if possible
   public void downOneFloor()
   {
       if(currentFloor > 1)
           currentFloor--;
   }
  
   // method to check if the elevator is at top floor
   public boolean isAtTopFloor()
   {
       return(currentFloor == currentFloor < topFloor);
   }
  
  
   public static void main(String[] args) {
      
       Elevator ele1 = new Elevator(1,150,10); // create object using parameterized constructor
       Elevator ele2 = new Elevator(ele1); // create object using copy constructor
       // change the values of the data members
       ele2.setCurrentFloor(3);
       ele2.setMaximumWeight(120);
       ele2.setTopFloor(5);

   }

}

Add a comment
Know the answer?
Add Answer to:
use java follow my code: public class q1 {    // Instance Variables    int currentFloor;...
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
  • We have written the following Node class: class Node { // instance variables private int value;...

    We have written the following Node class: class Node { // instance variables private int value; private Node next; // constructor public Node(int value) { this.value = value; } // get the 'next' variable public Node getNext() { return this.next; } // get the 'value' variable public int getValue() { return this.value; } // set the 'next' variable public void setNext(Node next) { this.next = next; } } TASK: Create a class called List that has the following properties: It...

  • We have written the following Class1 class: class Class1 implements SecretInterface { // instance variable private...

    We have written the following Class1 class: class Class1 implements SecretInterface { // instance variable private int x = 0; // constructor public Class1(int x) { this.x = x; } // instance methods public double myMethod1(double x, double y) { return x - y; } public void myMethod2(int x) { System.out.println(x); } public String myMethod3(String x) { return "hi " + x; } } We have also written the following Class2 class: class Class2 implements SecretInterface { // instance variable...

  • Java template public class GiftCard { // Instance Variables // TODO: balance // Constructors /** *...

    Java template public class GiftCard { // Instance Variables // TODO: balance // Constructors /** * GiftCard(double initialBalance) * * Initialize a GiftCard object with a pre-defined amount of money. * * @param initialBalance double The desired starting balance */ public GiftCard(double initialBalance) { setBalance(initialBalance); } /** * GiftCard() * * Initialize a GiftCard object with a balance of $0.00. */ public GiftCard() {} // Instance Methods /** * setBalance * * This should set the balance of the gift...

  • Read through the code of the class Player, noting it has two instance variables, name and rank, which are of type String and three further instance variables won, drew and lost which are of type int....

    Read through the code of the class Player, noting it has two instance variables, name and rank, which are of type String and three further instance variables won, drew and lost which are of type int. There is also an attribute of the class, points, which does not have a corresponding instance variable. Also note the constructor and methods of the class and what they do. TournamentAdmin class code: public class RankAdmin {    /**     * Constructor for objects of class...

  • Step 1 Develop the following class: Class Name: Bag Access Modifier: public Instance variables Name: name...

    Step 1 Develop the following class: Class Name: Bag Access Modifier: public Instance variables Name: name Access modifier: private Data Type: String Name: currentWeight Access modifier: private Data Type: double Name: maximumWeight Access modifier: private Data Type: double Constructors Name: Bag Access modifier: public Parameters: none (default constructor) Task: sets the value of the instance variable name to the empty string sets the value of the instance variable currentWeight to 0.0 sets the value of the instance variable maximumWeight to...

  • Step 1 Develop the following class: Class Name: CollegeDegree Access Modifier: public Instance variables Name: major...

    Step 1 Develop the following class: Class Name: CollegeDegree Access Modifier: public Instance variables Name: major Access modifier: private Data type: String Name: numberOfCourses Access modifier: private Data type: int Name: courseNameArray Access modifier: private Data type: String [ ] Name: courseCreditArray Access modifier: private Data type: int [ ] Static variables Name: maximumNumberOfCourses Access modifier: private Data type: int Initial Value: 40 Constructor Name: CollegeDegree Access modifier: public Parameters: none (default constructor) Task: sets major to the empty string...

  • Java Programming: The following is my code: import java.util.Arrays; public class KWArrayList<E> {    // Data...

    Java Programming: The following is my code: import java.util.Arrays; public class KWArrayList<E> {    // Data fields    /** The default initial capacity */    private static final int INITIAL_CAPACITY = 10;       /** The underlying data array */    private E[] theData;       /** The current size */    private int size = 0;       /** The current capacity */    private int capacity = 0;       @SuppressWarnings("unchecked")    public KWArrayList() {        capacity...

  • Start a new project in NetBeans that defines a class Person with the following: Instance variables...

    Start a new project in NetBeans that defines a class Person with the following: Instance variables firstName (type String), middleInitial (type char) and lastName (type String) (1) A no-parameter constructor with a call to the this constructor; and (2) a constructor with String, char, String parameters (assign the parameters directly to the instance variables in this constructor--see info regarding the elimination of set methods below) Accessor (get) methods for all three instance variables (no set methods are required which makes...

  • Lab 7 Add three instance variables to your class RightTriangle from lab 5. The first two...

    Lab 7 Add three instance variables to your class RightTriangle from lab 5. The first two are of type int and are called xLoc and yLoc and the third is of type int called ID. Also add a static variable of type double called scaleFactor. This should be initialized to a default value of 1. Update the constructor to set the three new instance variables and add appropriate get and set methods for the four new variables. All set methods...

  • Programming assignment for Java: Do not add any other instance variables to any class, but you...

    Programming assignment for Java: Do not add any other instance variables to any class, but you can create local variables in a method to accomplish tasks. Do not create any methods other than the ones listed below. Step 1 Develop the following class: Class Name: College Degree Access Modifier: public Instance variables Name: major Access modifier: private Data type: String Name: numberOfCourses Access modifier: private Data type: int Name: courseNameArray Access modifier: private Data type: String Name: courseCreditArray Access modifier:...

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
ADVERTISEMENT