Question

It's been too long since I have done any Java programming. Can anyone help me with...

It's been too long since I have done any Java programming. Can anyone help me with this one? Thank you

6.5 Write the declaration for a class named C that declares: (1) a private int instance variable named mX; (2) a private int class variable named mY initialized to 0; (3) a private int class constant named A which is equivalent to 100; (4) a public int class constant named B which is equivalent to 200; (5) public accessor and mutator methods for mX named getX() and setX(); (6) public accessor and mutator methods for mY named getY() and setY(); (7) a constructor that has one int input parameter named pX which calls setX() to initialize mX to pX; (8) a default constructor that calls C(int) to initialize mX to -1.

6.6 Continuing with the previous exercise, write the declaration for a class named Main that implements the main() method. Within main() suppose we wish to instantiate a C object named cObj1 calling the default constructor. Write the code to do this.

6.7 Continuing, write the code to instantiate another C object named cObj2 calling the second constructor to initialize the mX instance variable to 10.

6.8 Continuing, within main(), are the following statements legal, i.e., do they compile? If so, explain what happens when

the statement is executed. If not, explain why the statement is illegal.

(a) int a1 = C.mX;

(b) int a2 = C.mY;

(c) int a3 = C.A;

(d) int a4 = C.B;

(e) cObj1.C(20);

(f) int a5 = cObj1.getX();

(g) cObj1.setX(20);

(h) cObj2.setX(cObj1.getX());

(i) int a6 = C.getX();

(j) C.setX(20);

(k) int a7 = cObj1.getY();

(l) cObj1.setY(20);

(m) int a8 = C.getY();

(n) C.setY(20);

6.9 Continuing, suppose we add these two methods to the declaration of class C. For each assignment statement, if it is legal (compiles) explain what happens when the statement is executed. For each assignment statement that is illegal (syntax error) explain why the code is illegal.

     public void f() {
         mX = 0;

mY = 0; }

     public static void g() {
         mX = 0;

mY = 0; }

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

Complete Solution:

// 6.5
// File: C.java
public class C
{
private int mX;
private static int mY;
private static final int A = 100;
public static final int B = 200;

public C()
{
  this(-1);
}

public C(int px)
{
  setX(px);
}


public int getX()
{
  return mX;
}

public void setX(int pX)
{
  mX = pX;
}


public int getY()
{
  return mY;
}

public void setY(int newY)
{
  mY = newY;
}

// 6.9
   public void f()
    {
       mX = 0;
        mY = 0;
    }
    // It is a legal method declaration.
    // It sets mX to 0 and mY to 0 when this method is executed.
    // A static variable can be accessed in both static and non-static methods.
   

    public static void g()
    {
        //mX = 0;
        mY = 0;
    }
    // It is an illegal method declaration.
    // It sets mY to 0 but cannot set mX to 0.
    // The variable mX is a non-static variable and the method g() is a static method.
    // A non-static variable cannot be accessed in a static method.
}

-----------------------------------------------------------------------------------------------------------------------

// File: Main.java
public class Main
{
public static void main(String[] args)
{
  // 6.6
  C cObj1 = new C();
  
  // 6.7
  C cObj2 = new C(10);
  
  // 6.8
  // (a)
  // int a1 = C.mX;
  // It is an illegal statement.
  // The mX is a private variable in class C.
  // A private variable cannot be accessed using its class name in another class.
  
  // (b)
  // int a2 = C.mY;
  // It is an illegal statement.
  // The mY is a private variable in class C.
  // A private variable cannot be accessed using its class name in another class.
  
  // (c)
  // int a3 = C.A;
  // It is an illegal statement.
  // The A is a private and static constant variable in class C.
  // A private variable cannot be accessed using its class name in another class.
  
  // (d)
  int a4 = C.B;
  // It is a legal statement.
  // The B is a public and static constant variable in class C.
  // A public and static variable can be accessed using its class name in another class.
  // It stores the value of B into the variable a4.
  System.out.println("(d): a4 = " + a4);
  
  // (e)
  // cObj1.C(20);
  // It will be legal statement when you create a method in class C with the following heading
  // public void C(int i)
  // But it is not a good practice to write method name as same as the class name.
  
  // (f)
  int a5 = cObj1.getX();
  // It is a legal statement.
  // It stores the current value of cObj1's mX into the variable a5.
  System.out.println("(f): a5 = " + a5);
  
  // (g)
  cObj1.setX(20);
  // It is a legal statement.
  // It sets cObj1's mX to 20.
  System.out.println("(g): cObj1's mX = " + cObj1.getX());
  
  // (h)
  cObj2.setX(cObj1.getX());
  // It is a legal statement.
  // It sets cObj2's mX to cObj1's mX
  System.out.println("(h): cObj2's mX = " + cObj2.getX());
  
  // (i)
  // int a6 = C.getX();
  // It is an illegal statement.
  // The getX method is not a static method.
  // A non-static method cannot be accessed using its class name in another class.
  
  // (j)
  // C.setX(20);
  // It is an illegal statement.
  // The setX method is not a static method.
  // A non-static method cannot be accessed using its class name in another class.
  
  // (k)
  int a7 = cObj1.getY();
  // It is a legal statement.
  // It stores the current value of cObj1's mY into the variable a7.
  System.out.println("(k): a7 = " + a7);
  
  // (l)
  cObj1.setY(20);
  // It is a legal statement.
  // It sets cObj1's mY to 20.
  System.out.println("(l): cObj1's mY = " + cObj1.getY());
  
  // (m)
  // int a8 = C.getY();
  // It is an illegal statement.
  // The getY method is not a static method.
  // A non-static method cannot be accessed using its class name in another class.
  
  // (n)
  // C.setY(20);
  // It is an illegal statement.
  // The setY method is not a static method.
  // A non-static method cannot be accessed using its class name in another class.
}
}

-----------------------------------------------------------------------------------------------------------------------

Sample Output:

Add a comment
Know the answer?
Add Answer to:
It's been too long since I have done any Java programming. Can anyone help me with...
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
  • I need help with my java code i am having a hard time compiling it //...

    I need help with my java code i am having a hard time compiling it // Cristian Benitez import java.awt.Color; import java.awt.Graphics; import java.util.ArrayList; import javax.swing.JPanel; Face class public class FaceDraw{ int width; int height; int x; int y; String smileStatus; //default constructor public FaceDraw(){ } // Getters and Setters for width,height,x,y public int getWidth(){ return width; } public void setWidth(int width){ this.width=width; } public int getHeight(){ return height; } public void setHeight(int height){ this.height=height; } public int getX(){ return...

  • (JAVA NetBeans) Write programs in java Example 10.21-24 //Animal.java /** Animal class * Anderson. Franceschi */...

    (JAVA NetBeans) Write programs in java Example 10.21-24 //Animal.java /** Animal class * Anderson. Franceschi */ import java.awt.Graphics; public abstract class Animal { private int x; // x position private int y; // y position private String ID; // animal ID /** default constructor * Sets ID to empty String */ public Animal( ) { ID = ""; } /** Constructor * @param rID Animal ID * @param rX x position * @param rY y position */ public Animal( String...

  • In Java please! Problem You will be using the point class discussed in class to create...

    In Java please! Problem You will be using the point class discussed in class to create a Rectangle class. You also need to have a driver class that tests your rectangle. Requirements You must implement the 3 classes in the class diagram below and use the same naming and data types. Following is a description of what each method does. Point Rectangle RectangleDriver - x: int - top Left: Point + main(args: String[) - y: int - width: int +...

  • 1a. Create a class named Inventory - Separate declaration from implementation (i.e. Header and CPP files)...

    1a. Create a class named Inventory - Separate declaration from implementation (i.e. Header and CPP files) 1b. Add the following private member variables - a int variable named itemNumber - an int variable named quantity - a double variable named cost - a double variable named totalCost. Total cost is defined as quantity X cost. - updateTotalCost() which takes no arguments and sets the values of the variable totalCost. 1c. Add the following public functions: - Accessor and Mutators for...

  • I have done a decent amount of coding... but I need you to help me understand...

    I have done a decent amount of coding... but I need you to help me understand what I do not. Please, post comments so that I can learn from you. Here are the instructions for this portion of the project: Create the Monkey class, using the specification document as a guide. The Monkey class must do the following: Inherit from the RescueAnimal class Implement all attributes with appropriate data structures Include accessors and mutators for all implemented attributes Here is...

  • Java Programming --- complete the given classes DeLand Space Car Dealer needs a new program for...

    Java Programming --- complete the given classes DeLand Space Car Dealer needs a new program for their inventory management system. The program needs the following classes: A main class (SpaceCarDealer.java) that includes the main method and GUI. Two instantiable classes: CarDealer.java o Variables to store the dealer name, and the carsOnLot array in Car type. o A constructor to initialize the name variable with the given dealer name. o An accessor method to return the name of the dealer. o...

  • Java Help 2. Task: Create a client for the Point class. Be very thorough with your...

    Java Help 2. Task: Create a client for the Point class. Be very thorough with your testing (including invalid input) and have output similar to the sample output below: ---After declaration, constructors invoked--- Using toString(): First point is (0, 0) Second point is (7, 13) Third point is (7, 15) Second point (7, 13) lines up vertically with third point (7, 15) Second point (7, 13) doesn't line up horizontally with third point (7, 15) Enter the x-coordinate for first...

  • In JAVA Algorithm Workbench 1. Write the first line of the definition for a Poodle class....

    In JAVA Algorithm Workbench 1. Write the first line of the definition for a Poodle class. The class should extend the Dog class. 2. Look at the following code, which is the first line of a class definition: public class Tiger extends Felis In what order will the class constructors execute? 3. Write the statement that calls a superclass constructor and passes the arguments x, y, and z. 4. A superclass has the following method: public void setValue(int v) value...

  • Greetings, everybody I already started working in this program. I just need someone to help me...

    Greetings, everybody I already started working in this program. I just need someone to help me complete this part of the assignment (my program has 4 parts of code: main.cpp; Student.cpp; Student.h; StudentGrades.cpp; StudentGrades.h) Just Modify only the StudentGrades.h and StudentGrades.cpp files to correct all defect you will need to provide implementation for the copy constructor, assignment operator, and destructor for the StudentGrades class. Here are my files: (1) Main.cpp #include <iostream> #include <string> #include "StudentGrades.h" using namespace std; int...

  • The purpose of this assignment is to write a class Checker that can be used as...

    The purpose of this assignment is to write a class Checker that can be used as a part of a checker game program. Open an New Project in Qt Creator, and name it ChGame. Right click on the name of the class, and add a new C++ class named Checker. This will create the new les Checker.h and Checker.cpp. You will need to modify both Checker.h and Checker.cpp for this assignment. (The main function I used for testing my functions...

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