Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
4th Edition
ISBN: 9780134787961
Author: Tony Gaddis, Godfrey Muganda
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Textbook Question
Chapter 6, Problem 3AW
Consider the following class declaration:
public class Square
{
private double sideLength;
public double getArea()
{
return sideLength * sideLength;
}
public double getSideLength()
{
return sideLength;
}
}
- a. Write a no-arg constructor for this class, it should assign the sideLength field the value 0.0.
- b. Write an overloaded constructor for this class. It should accept an argument that is copied into the sideLength field.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Consider the following class definition:
class circle { public: void print() const;
void setRadius(double);
void setCenter(double, double);
void getCenter(double&, double&);
double getRadius();
double area();
circle();
circle(double, double, double);
double, double);
private: double xCoordinate;
double yCoordinate;
double radius; }
class cylinder: public circle { public: void print() const;
void setHeight(double);
double getHeight();
double volume();
double area();
cylinder();
cylinder(double, double, private: double height; }
Suppose that you have the declaration: cylinder newCylinder;
Write the definitions of the member functions of the classes circle and cylinder. Identify the member functions of the class cylinder that overrides the member functions of the class circle
public class Cat
{
private int age;
public Cat (int a)
{
age =
а;
}
public int getAge()
{
return a;
}
}
Which of the following best explains why the getAge method will NOT work as
intended?
The instance variable age should be returned instead of a, which is local to the constructor.
The variable age is not declared inside the getAge method.
The getAge method should be declared as private.
The getAge method should have at least one parameter.
Java - Calculator Class
Chapter 6 Solutions
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Ch. 6.1 - What does an object use its fields for?Ch. 6.1 - Prob. 6.2CPCh. 6.1 - How is a class like a blueprint?Ch. 6.1 - You have programs that create Scanner, Random, and...Ch. 6.1 - Prob. 6.5CPCh. 6.1 - What values do reference variables hold?Ch. 6.1 - Prob. 6.7CPCh. 6.2 - You hear someone make the following comment: A...Ch. 6.2 - Prob. 6.9CPCh. 6.2 - When a variable is said to reference an object,...
Ch. 6.2 - A string literal, such as Joe, causes what type of...Ch. 6.2 - Prob. 6.12CPCh. 6.2 - Prob. 6.13CPCh. 6.2 - Prob. 6.14CPCh. 6.2 - Prob. 6.15CPCh. 6.2 - What is a stale data item?Ch. 6.3 - Assume that r1 and r2 are variables that reference...Ch. 6.4 - How is a constructor named?Ch. 6.4 - What is a constructors return type?Ch. 6.4 - Prob. 6.20CPCh. 6.7 - Prob. 6.21CPCh. 6.7 - What is a methods signature?Ch. 6.7 - Look at the following class: public class...Ch. 6.7 - How many default constructors may a class have?Ch. 6.9 - Prob. 6.25CPCh. 6.9 - When designing an object-oriented application, who...Ch. 6.9 - How do you identify the potential classes in a...Ch. 6.9 - What are a classs responsibilities?Ch. 6.9 - What two questions should you ask to determine a...Ch. 6.9 - Will all of a classs actions always be directly...Ch. 6 - This is a collection of programming statements...Ch. 6 - A class is analogous to a(n) _______. a. house b....Ch. 6 - An object is a(n) ________. a. blueprint b....Ch. 6 - This is a class member that holds data. a. method...Ch. 6 - This key word causes an object to be created in...Ch. 6 - This is a method that gets a value from a classs...Ch. 6 - This is a method that stores a value in a field or...Ch. 6 - When the value of an item is dependent on other...Ch. 6 - This is a method that is automatically called when...Ch. 6 - When a local variable has the same name as a...Ch. 6 - This is automatically provided for a class if you...Ch. 6 - Prob. 12MCCh. 6 - Prob. 13MCCh. 6 - A classs responsibilities are __________. a. the...Ch. 6 - True or False: The new operator creates an...Ch. 6 - True or False: Each instance of a class has its...Ch. 6 - True or False: When you write a constructor for a...Ch. 6 - True or False: A class may not have more than one...Ch. 6 - True or False: To find the classes needed for an...Ch. 6 - Find the error in the following class: public...Ch. 6 - Prob. 2FTECh. 6 - The following statement attempts to create a...Ch. 6 - Find the error in the following class: public...Ch. 6 - Find the error in the following class: public...Ch. 6 - Design a class named Pet, which should have the...Ch. 6 - Look at the following partial class definition,...Ch. 6 - Consider the following class declaration: public...Ch. 6 - Look at the following description of a problem...Ch. 6 - The bank offers the following types of accounts to...Ch. 6 - Assume that you are writing an application that...Ch. 6 - What is the difference between a class and an...Ch. 6 - A contractor uses a blueprint to build a set of...Ch. 6 - What is an accessor method? What is a mutator...Ch. 6 - is it a good idea to make fields private? Why or...Ch. 6 - If a class has a private field, what has access to...Ch. 6 - Prob. 6SACh. 6 - Assume a program named MailList.java is stored in...Ch. 6 - Why are constructors useful for performing...Ch. 6 - Under what circumstances does Java automatically...Ch. 6 - What do you call a constructor that accepts no...Ch. 6 - Prob. 11SACh. 6 - How does method overloading improve the usefulness...Ch. 6 - Employee Class Write a class named Employee that...Ch. 6 - Car Class Write a class named Car that has the...Ch. 6 - Personal Information Class Design a class that...Ch. 6 - Retail Item Class Write a class named RetailItem...Ch. 6 - Payroll Class Design a Payroll class that has...Ch. 6 - TestScores Class Design a TestScores class that...Ch. 6 - Circle Class Write a Circle class that has the...Ch. 6 - Temperature Class Write a Temperature class that...Ch. 6 - Days in a Month Write a class named MonthDays, The...Ch. 6 - A Game of Twenty-One For this assignment, you will...Ch. 6 - Freezing and Boiling Points The following table...Ch. 6 - SavingsAccount Class Design a SavingsAccount class...Ch. 6 - Deposit and Withdrawal Files Use Notepad or...Ch. 6 - Patient Charges Write a class named Patient that...Ch. 6 - Dice Game Write a program that uses the Die class...Ch. 6 - Roulette Wheel Colors On a roulette wheel, the...Ch. 6 - Coin Toss Simulator Write a class named Coin. The...Ch. 6 - Tossing Coins for a Dollar For this assignment you...Ch. 6 - Fishing Game Simulation For this assignment, you...
Additional Engineering Textbook Solutions
Find more solutions based on key concepts
Suppose your program contains the following type definitions: struct Box { string name; int number; Box next; }...
Problem Solving with C++ (10th Edition)
Rock, Paper, Scissors Game Write a program that lets the user play the game of Rock, Paper, Scissors against th...
Starting Out with Python (4th Edition)
Use the following tables for your answers to questions 3.7 through 3.51 : PET_OWNER (OwnerID, OwnerLasst Name, ...
Database Concepts (8th Edition)
In Exercises 41 through 46, identify the errors. Dima,b,c,dAsDoublea=2b=3c=d=4Istoutput.Items.Add(5((a+b)/(c+d)
Introduction To Programming Using Visual Basic (11th Edition)
The current source in the circuit shown generates the current pulse
Find (a) v (0); (b) the instant of time gr...
Electric Circuits. (11th Edition)
Consider the following skeletal C program: void fun1(void); / prototype / void fun2(void); / prototype / void f...
Concepts Of Programming Languages
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- True or False An interface can be used to define a set of methods that a class must implement.arrow_forwardDesign a LandTract class that has two fields (i.e. instance variables): one for the tract’s length(a double), and one for the width (a double). The class should have: a constructor that accepts arguments for the two fields a method that returns the tract’s area(i.e. length * width) an equals method that accepts a LandTract object as an argument. If the argument object holds the same data (i.e. length and width) as the calling object, this method should return true. Otherwise, it should return false. a toString method (that returns a String representation of the tract’s length, width, and area). Demonstrate the class in a program by creating an array of three LandTract objects initialized with user input. Then, print the info of the LandTract objects stored in the array by invoking the toString method (implicitly or explicity). Also, use the equals method to compare at least two LandTract objects and print wether they are of equal size or not. Sample Output:…arrow_forwardJAVA ENCAPSULATIONarrow_forward
- What is the output of the following code? Convert the abstract class into an interface and modify the codes in all classes that will maintain the same output. (Rewrite the code)arrow_forward/** NoParking models a No Parking sign */ public class NoParking { //-----------Start below here. To do: approximate lines of code = 15 // 1. Write the class so that it works as expected with the unit tester. private String startTime; private String endTime; /** Constructs a NoParking object with a default time */ public NoParking(){ startTime = "0:00"; endTime = "23:59"; } /** Sets the start time @param the start time */ public void setStartTime(String start){ startTime = start; } /** Sets the end time @param the end time */ public void setEndTime(String end){ endTime = end; } /** Reverses the times */ /** Returns the string for no parking @return the string for no parking */ } //-----------------End here. Please do not remove this…arrow_forwardpublic class FoodItem { private String name; private double fat; private double carbs; private double protein; // TODO: Define default constructor // TODO: Define second constructor with parameters to initialize private fields (name, fat, carbs, protein) public String getName() { return name; } public double getFat() { return fat; } public double getCarbs() { return carbs; } public double getProtein() { return protein; } public double getCalories(double numServings) { // Calorie formula double calories = ((fat * 9) + (carbs * 4) + (protein * 4)) * numServings; return calories; } public void printInfo() { System.out.println("Nutritional information per serving of " + name + ":"); System.out.printf(" Fat: %.2f g\n", fat); System.out.printf(" Carbohydrates: %.2f g\n", carbs); System.out.printf(" Protein: %.2f g\n", protein); }} import java.util.Scanner; public class NutritionalInfo {…arrow_forward
- A SuperDie Class Write a class SuperDie that models a single die with an arbitrary number of sides, not just six. A die instantiated with the default constructor has six sides. The methods of this class should be: • roll a die and return its value, • return the number of sides on a die, and • change the number of sides on a die. Include a main(...) method that tests all the methods of your class.arrow_forwardpublic class Artwork { // TODO: Declare private fields - title, yearCreated // TODO: Declare private field artist of type Artist // TODO: Define default constructor // TODO: Define get methods: getTitle(), getYearCreated() // TODO: Define second constructor to initialize // private fields (title, yearCreated, artist) // TODO: Define printInfo() method // Call the printInfo() method in Artist.java to print an artist's information }} public class ArtworkLabel { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String userTitle, userArtistName; int yearCreated, userBirthYear, userDeathYear; userArtistName = scnr.nextLine(); userBirthYear = scnr.nextInt(); scnr.nextLine(); userDeathYear = scnr.nextInt(); scnr.nextLine(); userTitle = scnr.nextLine(); yearCreated =…arrow_forwardIncorrect Question 1 Consider: class Bike { } private String make; public public Bike() } // ... public Bike(String newMake) { // ... } public public Bike (Bike bike) } // ... Bike(Bike bike) is the no-arg constructor Bike() is the no-arg constructor Bike(String newMake) is the no-arg constructor This is not valid class declarationarrow_forward
- Define the following terms with the help of examples: Encapsulation Constructor.arrow_forward7arrow_forwardThe following class declaration has errors. Locate as many as you can. class Circle {private:double diameter; int centerX; int centerY; public:Circle(double d, int x, int y){ diameter = d; centerX = x; centerY = y; } // Overloaded = operatorvoid Circle=(Circle &right) { diameter = right.diameter; centerX = right.centerX; centerY = right.centerY; }... Other member functions follow ...};arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Introduction to Classes and Objects - Part 1 (Data Structures & Algorithms #3); Author: CS Dojo;https://www.youtube.com/watch?v=8yjkWGRlUmY;License: Standard YouTube License, CC-BY