Java: An Introduction To Problem Solving And Programming Plus Mylab Programming With Pearson Etext -- Access Card Package (8th Edition)
8th Edition
ISBN: 9780134710754
Author: Walter Savitch
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Expert Solution & Answer
Chapter 6.4, Problem 33STQ
Explanation of Solution
Program:
Given code:
//Define the method "countPeople()"
public static int countPeople(int numberOfCouples)
{
    //Return the value
    return 2 * numberOfCouples;
}
//Overload the method "countPeople()"
public static double countPeople(int numberOfCouples)
{
    //Return the value
    return 2...
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Define a Static method 'max' with return type as 'int' in class 'ProblemSolution' that take two numbers with type 'int'. Inside the 'max' method, compare the two numbers and return the maximum of the two numbers.
Â
Then, inside the 'main' method.
Define two 'int' type numbers; the first number (num1) is equal to 5 and the second number (num2) is equal to 2.
At the next line, call the 'max' method where its parameter input are the two numbers above, and assign the result of the method to the variable num3 (type 'int').
Finally, on the next line, print out -> "The maximum of " + num1 + " and " + num2 + " is " + num3
Â
Output     The maximum of 5 and 2 is 5
Â
Where,Â
There must be space between all the words and numbers.Â
There should not any space after the last number
Write a class that has three overloaded static methods for calculating the areas of the Âfollowing geometric shapes:
circles
rectangles
cylinders
Here are the formulas for calculating the area of the shapes.
Area of a circle:
Area=Ď€r2
where
π is Math.PI and r is the circle’s radius
Area of a rectangle:
Area=WidthĂ—Length
Area of a cylinder:
Area=Ď€r2hArea=Ď€r2h
where
π is Math.PI, r is the radius of the cylinder’s base, and h is the cylinder’s height
Because the three methods are to be overloaded, they should each have the same name, but different parameter lists. Demonstrate the class in a complete program.
4-what is the output of the following code ( a line by line trace and explanation of methods is required)
class Car{
public double speed;
public String model;
public double price=1000;
public static double sales;
public Car(String model,int speed){
this.speed=speed; this.model=model; sales=sales+price;}
public void increaseSpeed1(double speed){ speed=speed+20;
}
public static void increaseSpeed2(Car myCar){ myCar.speed=myCar.speed+20;
}
}
class Test{
public static void main(String [] arg){ Car car1=new Car("Opel Corsa",130); Car car2=new Car("Matrix",130); car1.increaseSpeed1(car1.speed); Car.increaseSpeed2(car2); System.out.println(car1.speed); System.out.println(car2.speed); System.out.println(car1.sales); System.out.println(car2.sales);
}}
Chapter 6 Solutions
Java: An Introduction To Problem Solving And Programming Plus Mylab Programming With Pearson Etext -- Access Card Package (8th Edition)
Ch. 6.1 - If a class is named Student, what name can you use...Ch. 6.1 - When defining a constructor, what do you specify...Ch. 6.1 - What is a default constructor?Ch. 6.1 - Does every class in Java automatically have a...Ch. 6.1 - In the program PetDemo shown in Listing 6 2, you...Ch. 6.2 - Prob. 6STQCh. 6.2 - Can a class contain both instance variables and...Ch. 6.2 - Can you reference a static variable by name within...Ch. 6.2 - Can you reference an instance variable by name...Ch. 6.2 - Can you reference a static variable by name within...
Ch. 6.2 - Can you reference an instance variable by name...Ch. 6.2 - Is the following valid, given the class...Ch. 6.2 - Prob. 13STQCh. 6.2 - Prob. 14STQCh. 6.2 - Prob. 15STQCh. 6.2 - Is the following valid, given the class...Ch. 6.2 - What values are returned by each of the following?...Ch. 6.2 - Suppose that speed is a variable of type double...Ch. 6.2 - Repeat the previous question, but instead assign...Ch. 6.2 - Suppose that nl is of type int and n2 is of type...Ch. 6.2 - Define a class CircleCalculator that hat only two...Ch. 6.2 - Which of the following statements are legal?...Ch. 6.2 - Write a Java expression to convert the number in...Ch. 6.2 - Consider the variable 5 of type String that...Ch. 6.2 - Repeat the previous question, but accommodate a...Ch. 6.2 - Write Java code to display the largest and...Ch. 6.3 - Prob. 27STQCh. 6.3 - Consider the variable allCents in the method...Ch. 6.3 - What is wrong with a program that starts as...Ch. 6.3 - Prob. 30STQCh. 6.3 - In your definition of the class OutputFormat. In...Ch. 6.4 - Prob. 32STQCh. 6.4 - Prob. 33STQCh. 6.4 - Prob. 34STQCh. 6.4 - Consider the class Species in Listing 5.19 of...Ch. 6.4 - Repeat the previous question for a method...Ch. 6.4 - Still considering the class Species in Listing...Ch. 6.4 - Rewrite the method add in Listing 6.16 so that it...Ch. 6.4 - In Listing 6.16, the set method that has a String...Ch. 6.5 - Give the definitions of three accessor methods...Ch. 6.6 - If cardSuit is an instance of Suit and is assigned...Ch. 6.7 - Suppose you want to use classes in the package...Ch. 6.7 - Prob. 43STQCh. 6.7 - Can a package have any name you might want, or are...Ch. 6.7 - On your system, place the class Pet (Listing 6.1)...Ch. 6.8 - The previous section showed you how to change the...Ch. 6 - Prob. 1ECh. 6 - Prob. 2ECh. 6 - Write a default constructor and a second...Ch. 6 - Write a constructor for the class...Ch. 6 - Consider a class characteristic that will be used...Ch. 6 - Create a class RoomOccupancy that can be used to...Ch. 6 - Write a program that tests the class RoomOccupancy...Ch. 6 - Sometimes we would like a class that has just a...Ch. 6 - Create a program that tests the class Merlin...Ch. 6 - In the previous chapter, Self-Test Question 16...Ch. 6 - Create a class Android whose objects have unique...Ch. 6 - Prob. 12ECh. 6 - Modify the definition of the class Species in...Ch. 6 - Prob. 2PCh. 6 - Using the class Pet from Listing 6.1, write a...Ch. 6 - Do Practice Program 4 from Chapter 5 except define...Ch. 6 - The following class displays a disclaimer every...Ch. 6 - Do Practice Program 5 from Chapter 5 but add a...Ch. 6 - We can improve the Beer class from the previous...Ch. 6 - Define a utility class for displaying values of...Ch. 6 - Write a new class TruncatedDollarFormat that is...Ch. 6 - Complete and fully test the class Time that...Ch. 6 - Complete and fully test the class Characteristic...Ch. 6 - Write a Java enumeration LetterGrade that...Ch. 6 - Complete and fully test the class Per n that...Ch. 6 - Write a Temperature class that represents...Ch. 6 - Repeat Programming Project 8 of the previous...Ch. 6 - Write and fully test a class that represents...Ch. 6 - Write a program that will record the votes for one...Ch. 6 - Repeat Programming Project 10 from Chapter 5, but...Ch. 6 - Create a JavaFX application that displays a button...
Knowledge Booster
Similar questions
- Below is a definition for a Dumbbell class: public class Dumbbell { private double weight; // in pounds public Dumbbell() {  weight = 1.0;  }public Dumbbell(double w) {weight = w;}   public void setWeight(double newWeight) {  weight = newWeight;   }   public double getWeight() {  return weight;  }public lift() {System.out.println("You did 1 rep with the " + weight + " lb dumbbell.");}} Modify the setWeight method to check that the new weight parameter is between 1 and 50 pounds. If the new weight is less than 1 pound, set it to 1 pound instead. If the new weight is greater than 50 pounds, set it to 50 pounds instead. Show the modified method in your post. Add a second lift() method which takes an integer parameter signifying how many reps to do with the dumbbell. Print an appropriate message for the number of reps performed, similar to the message in the existing lift() method. If the number of reps is 20 or more, also print out the statement "Whew, what a workout!"…arrow_forwardbox1 and box2 are instances of the Box class. Attributes length, width, and height of both box1 and box2 are read from input. Then, two strings are read from input. In the Box class, define instance method print_measurements() with self and one string as parameters to output the string parameter in one line, and the following in the next line: The value of attribute length ' by ' The value of attribute width ' by ' The value of attribute heightarrow_forwardWrite a program that calls methods for each of the following mini problems from its main method. Us a single Scanner for the program declared in the class asprivate Static Scanner keyboard = new Scanner(System.in); Repeat the calorie counting program from problem 5 in a new method. This time ask the user to input the string “M” if the user is a man and “W” if the user is a woman.  Use only the male formula to calculate calories if “M” is entered and use only the female formula to calculate calories if “W” is entered. Output the number of chocolate bars to consume as before.    2. For all of the following words, if you move the first letter to the end of the word, and then spell the result backwards you will get the original word:banana dresser grammar potato revive uneven assessWrite a method that reads a word and determines whether it has this property. Continue reading and testing words until you encounter the word quit. Treat uppercase letters the same as lowercase letters.…arrow_forward
- Write static methods: public static double cubeVolume(double h) : h^3 public static double cubeSurface(double h) : 6h^2 public static double cylinderVolume(double r, double h) : hπr^2 public static double cylinderSurface(double r, double h) : 2rπh+2πr^2 that compute the volume and surface area of a cube with height h, and a cylinder with circular base with radius r and height h. Place them into a class named “Geometry.java”. Then write a program with main class called “Lab7_yourID.java” that prompts the user for the values of r and h, calls the four methods, and prints the results. (Optional) Reconsider your previous solution by implementing classes Cube, and Cylinder separately instead of a single class “Geometry” (part A). Which approach is more object-oriented?arrow_forwardThe following technique is used in a game of chess: canMoveTo(int x, int y), boolean. A method in the Piece class returns if the piece may travel to position (x, y). Describe how you would put this method to the test.arrow_forwardAnalyze the following class, then choose the correct returnValueType for the method average. void int String doublearrow_forward
- Help m Complete method printPopcornTime(), with int parameter bagOunces, and void return type. If bagOunces is less than 2, print "Too small". If greater than 10, print "Too large". Otherwise, compute and print 6 * bagOunces followed by " seconds". End with a newline. Example output for ounces = 7: 42 seconds  import java.util.Scanner; public class PopcornTimer {public static void printPopcornTime(int bagOunces) { /* Your solution goes here */ } public static void main (String [] args) {Scanner scnr = new Scanner(System.in);int userOunces; userOunces = scnr.nextInt();printPopcornTime(userOunces);}}l.arrow_forwardSolve below java problem please. You are given a list of Employee objects and you need to implement a method that returns the top n highest-paid employees.  class Employee { private String name; private double salary; public Employee(String name, double salary) { this.name = name; this.salary = salary; } public String getName() { return name; } public double getSalary() { return salary; } }arrow_forwardWrite a program that calls methods for each of the following mini problems from its main method. Us a single Scanner for the program declared in the class asprivate Static Scanner keyboard = new Scanner(System.in); Write a method that reads a one-line sentence as input and then displays the following response: If the sentence ends with a question mark (?) and the input contains an even number of characters, display the word Yes. If the sentence ends with a question mark and the input contains an odd number of characters, display the word No. If the sentence ends with an exclamation point (!), display the word Wow. In all other cases, display the words You always say followed by the input string enclosed in quotes. Your output should all be on one line. Be sure to note that in the last case, your output must include quotation marks around the echoed input string. In all other cases, there are no quotes in the output. Your program does not have to check the input to see that the user…arrow_forward
- in class Song, write the calculation in method getEstEarnings of class Song to evaluate estimated earnings. Note that while in the real world, other methods would be used to estimate earnings, in this simplified model, the estimated earnings will be the length of the title, multiplied by the a value extracted by the ministry using the method getSongPartEst(hint.. the singer keeps a reference to the ministry. You can observe how the reference is used in method getClaimableEarnings). public int getEstEarnings() {    return  0;//Q2. Perform this calculation  }  public int getClaimableEarnings() {    int est = getEstEarnings();    if (est > singer.getMinistry().getMinClaimable())       return est;    else       return 0;  }arrow_forwardclass Param3 { public int x; private void increase(int p) { x = x*p; } public void calculateX(int y) { increase(y); } public int getX() { return x; } } // in another class Param3 q3 = new Param3(); q3.x = 5; q3.calculateX(7); System.out.println(q3.getX()); what would be the answer for the last two lines ? also above were x = x*p do both x in here are the fields? wouldn't that be cnofusing?arrow_forwardclass TenNums {private:  int *p; public:  TenNums() {    p = new int[10];     for (int i = 0; i < 10; i++) p[i] = i;   }  void display() { for (int i = 0; i < 10; i++) cout << p[i] << " "; }};int main() {  TenNums a;    a.display();    TenNums b = a;    b.display();    return 0;} Continuing from Question 4, let's say I added the following overloaded operator method to the class. Which statement will invoke this method? TenNums TenNums::operator+(const TenNums& b) { TenNums o; for (int i = 0; i < 10; i++) o.p[i] = p[i] + b.p [i]; return o;} Group of answer choices TenNums a; TenNums b = a; TenNums c = a + b; TenNums c = a.add(b);arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,