Concept explainers
Consider the following class declaration:
public class Thing
{
private int x;
private int y;
private static int z = 0;
public Thing ()
{
x = z;
y = z;
}
public static void putThing(int a)
{
z = a;
}
}
Assume a program containing the class declaration defines three Thing objects with the following statements:
Thing one = new Thing ();
Thing two = new Thing ();
Thing three = new Thing ();
- a. How many separate instances of the x member exist?
- b. How many separate instances of the y member exist?
- c. How many separate instances of the z member exist?
- d. What value will be stored in the x and y members of each object?
- e. Write a statement that will call the putThing method.
Want to see the full answer?
Check out a sample textbook solutionChapter 6 Solutions
MyLab Programming with Pearson eText -- Access Card -- for Starting Out with Java: Early Objects (Myprogramminglab)
Additional Engineering Textbook Solutions
Computer Science: An Overview (12th Edition)
Starting Out with Java: From Control Structures through Objects (6th Edition)
C How to Program (8th Edition)
Java How To Program (Early Objects)
Concepts of Programming Languages (11th Edition)
Database Concepts (7th Edition)
- Complete the partial declaration of Class Car: public class Car { // declare three private instance variables: color, speed, model // define a constructor with two parameters: c , m which will assign values to color and model. //define set and get method for all instance variables //define a method named calspeed( ) with int return type, the method will increase the speed by 50 mile per hour. } Complete the application below. public class CarApp { public static void main() { string color = blue; string model = C2021;i int speed = 80; // declare necessary object matching to the constructor definition // display the color and model on the screen // call the calspeed( ) method and display the new speed after calculation. } }arrow_forwardcomplete the following Horse Class with the guidelines provided below public class Horse { // Fields of class Horse // Constructor of class Horse /** * Constructor for objects of class Horse */ public Horse(char horseSymbol, String horseName, double horseConfidence) { } // Other methods of class Horse public void fall() { } public double getConfidence() { } public int getDistanceTravelled() { } public String getName() { } public char getSymbol() { } public void goBackToStart() { } public boolean hasFallen() { } public void moveForward() { } public void setConfidence(double newConfidence) { } public void setSymbol(char newSymbol) { } }arrow_forwardUse the class diagram below to create a money class for an object-oriented parking system. Write your code using java. Classes should contain properties and method implementations. N.B. Try to make your code readabale.arrow_forward
- The following code is in Java: public class Vehicle // Superclass { private double cost; public Vehicle(double c) { cost = c; } (Other methods . . .) // assume these are given and they’re OK. } public class Car extends Vehicle // Subclass { private int passengers; public Car(int p) { passengers = c; } (Other methods . . .) // assume these are given and they’re OK. } 1) What problems are in the code above?2) What is needed to fix the issue of the code?arrow_forwardConsider the following class: 1.Write its default constructor 2.Write a code to declare an object from this class using the default constructor public class Dog { String name; String color; void barking() { } void hungry() { } void sleeping() { } }arrow_forwardType a Java method constructor for the "book" Class, which takes values as parameters for all of these attributes.The" book " Class has the following properties: title (string), author (string), year (int), pages (int).arrow_forward
- language: Java Class Inheritance with an abstract class Define a class Employee with the following fields: _name, _empId, _department, _position (_title), _yearlySalary, _fullTime (a boolean: true/false) , _month , _year (time of hire) Add constructor and properties Define an abstract method GetBiMonthlySalary() (if the employee is paid twice a month) Define an abstract method GetVacationTime() that returns the number of vacation time this employee would have based on time of hire Override the ToString() method to return a string that contains the information pertaining to an employee such as name, empId, department, and where full time or not. Define a class HourlyEmployee that inherits from Employee. This class should have 2 fields of its own: _hours (hours worked in half a month) and _wage (hourly wage) Provide necessary constructor, properties and methods Override the ToString() to also return the additional fields Gets 3 days of vacation after first year if the…arrow_forwardExplain the following Java Inheritance code in details.public class Point extends Object{protected int x, y;public Point(){x = 0;y = 0;System.out.println( "Point constructor: " + this );}public Point( int xCoordinate, int yCoordinate ){x = xCoordinate;y = yCoordinate;System.out.println( "Point constructor: " + this );}protected void finalize(){System.out.println( "Point finalizer: " + this );}public String toString(){return "[" + x + ", " + y + "]";}}public class Circle extends Point{protected double radius;public Circle(){radius = 0;System.out.println( "Circle constructor: " + this );}public Circle( double circleRadius, int xCoordinate, int yCoordinate){super( xCoordinate, yCoordinate );radius = circleRadius;System.out.println( "Circle constructor: " + this);}protected void finalize(){System.out.println( "Circle finalizer: " + this );}public String toString(){return "Center = " + super.toString() + "; Radius = " + radius;}}public class Test{public static void main( String args[]…arrow_forwardQuestion 2: Recall and apply the concepts of object oriented programming (Java) (iii) We have to calculate the percentage of marks obtained in three subjects (each out of 100) by student A and in four subjects (each out of 100) by student B. Create an abstract class 'Marks' with an abstract method 'getPercentage'. It is inherited by two other classes 'A' and 'B' each having a method with the same name which returns the percentage of the students. The constructor of student A takes the marks in three subjects as its parameters and the marks in four subjects as its parameters for student B. Create an object for eac of the two classes and print the percentage of marks for both the students.arrow_forward
- (programming) A class may be made immutable in a number of ways. with the aid of an illustrationarrow_forwardJAVA CLASS AND OBJECT JAVA JAVA JAVA Write a java program/code of the given problems below: abstract class Animal { // abstract methods abstract void move(); abstract void eat(); // concrete method void label() { System.out.println("Animal's data:"); } } 1.) Modify the abstraction example code above by adding another class “TestCarabao” with the necessary methods “label, move, and eat”. 2.) Write a program that creates a car object, and displays the year and name with one method “speed”. Example output: Year: 2021 Model: Toyota Speed: 200km/h 3.) Write the main class of the given class Animal below that set the number of legs to “4” and display (get) the number of legs. class Animal { private int numberOfLegs; public void setNumberOfLegs(int numberOfLegs) { this.numberOfLegs = numberOfLegs; } public int getNumberOfLegs() { return numberOfLegs; } }arrow_forwardJava program: The class diagram with four classes Mammal, Human, Student and Doctor is given. The Mammal class is given as well. Write down the remaining classes as described in the class diagram. The fields and methods for each class is given in the attachment. //in Mammel.java Public interface Mammal{ public double hairColor(): } 1. Class Human: Fields: age(int), weight(double), height(double) Methods: getAge, getWeight, getHeight 2. Class Student: Fields: major (String), gpa (double), creditHours (int) methods: getMajor: returns major, getGpa: returns gpa, getYear: returns freshman, sophmore, junior, senior as determined by earned credit hours Freshman : less than 32 credit hours sophmore: at least 32 but less than 64 credit hours junior: at least 64 credit hours but less than 96 credit hours senior: at least 96 credit hours 3.Class Doctor: Fields: years(int), Speciality (String) methods: getYears: returns years, getSpeciality: returns speciality, getSalary: calculates…arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage