New Semester
Started
Get
50% OFF
Study Help!
--h --m --s
Claim Now
Question Answers
Textbooks
Find textbooks, questions and answers
Oops, something went wrong!
Change your search query and then try again
S
Books
FREE
Study Help
Expert Questions
Accounting
General Management
Mathematics
Finance
Organizational Behaviour
Law
Physics
Operating System
Management Leadership
Sociology
Programming
Marketing
Database
Computer Network
Economics
Textbooks Solutions
Accounting
Managerial Accounting
Management Leadership
Cost Accounting
Statistics
Business Law
Corporate Finance
Finance
Economics
Auditing
Tutors
Online Tutors
Find a Tutor
Hire a Tutor
Become a Tutor
AI Tutor
AI Study Planner
NEW
Sell Books
Search
Search
Sign In
Register
study help
computer science
building java programs a back to basics approach
Building Java Programs A Back To Basics Approach 5th Edition Stuart Reges, Marty Stepp - Solutions
Write a method called scaleByK that takes an ArrayList of integers as a parameter and replaces every integer of value with copies of itself. For example, if the list stores the values [4, 1, 2, 0, 3] before the method is called, it should store the values [4, 4, 4, 4, 1, 2, 2, 3, 3, 3] after the
Write a method called doubleList that takes an ArrayList of strings as a parameter and replaces every string with two of that same string. For example, if the list stores the values ["how", "are", "you?"] before the method is called, it should store the values ["how", "how", "are", "are", "you?",
Write a method called removeEvenLength that takes an ArrayList of strings as a parameter and removes all of the strings of even length from the list.
Which of the following is the correct syntax to construct an ArrayList to store integers?a. ArrayList list = new ArrayList();b. ArrayList[int] list = new ArrayList[int]();c. ArrayList list<> = new ArrayList();d. ArrayList list = new ArrayList();e. ArrayList list = new ArrayList<>();
Write a method called swapPairs that switches the order of values in an ArrayList of strings in a pairwise fashion. Your method should switch the order of the first two values, then switch the order of the next two, then the next two, and so on. If the number of values in the list is odd, the
Write a method called averageVowels that takes an ArrayList of strings as a parameter and returns the average number of vowel characters (a, e, i, o, u) in all Strings in the list. If your method is passed an empty ArrayList, it should return 0.0.
What is an ArrayList ? In what cases should you use an ArrayList rather than an array?
Consider writing a program to be used to manage a collection of movies. There are three kinds of movies in the collection: dramas, comedies, and documentaries. The collector would like to keep track of each movie’s title, the name of its director, and the year the movie was made. Some operations
Consider the following abstract class and its subclass. What state and behavior do you know for sure will be present in the subclass? How do you know? public abstract class Ordered { private String[] data; public void getElement (int i) { return data [i]; public abstract void arrange (); public
What is an abstract class? How is an abstract class like a normal class, and how does it differ? How is it like an interface?
Declare a method called getSideCount in the Shape interface that returns the number of sides that the shape has. Implement the method in all shape classes. A circle is defined to have 0 sides.
Modify the Point class from Chapter 8 so that it implements the Colored interface and Points have colors. (You may wish to create a ColoredPoint class that extends Point .)
What’s wrong with the code for the following interface? What should be changed to make a valid interface for objects that have colors? public interface Colored { private Color color; public Color getColor() { return color;
Consider the following interface and class:What must be true about the code for class C in order for that code to compile successfully? public interface I { public void m1 (); public void m2 (); public class c implements I { // code for class C
What is the difference between implementing an interface and extending a class?
In Section 9.2 we discussed adding functionality for dividend payments to the Stock class. Why was it preferable to create a DividendStock class rather than editing the Stock class and adding this feature directly to it?
Imagine that you are going to write a program to play card games. Consider a design with a Card class and 52 subclasses, one for each of the unique playing cards (for example, NineOfSpades and JackOfClubs). Is this a good design? If so, why? If not, why not, and what might be a better design?
Imagine a Rectangle class with objects that represent twodimensional rectangles. The Rectangle has width and height fields with appropriate accessors and mutators, as well as getArea and getPerimeter methods.You would like to add a Square class into your system. Is it a good design to make Square a
Declare an interface called Incrementable which represents items that store an integer that can be incremented in some way. The interface has a method called increment that increments the value and a method called getValue that returns the value. Once you have written the interface, write two
What is the difference between an is-a and a has-a relationship? How do you create a has-a relationship in your code?
Write a class named Dodecagon whose objects represent regular dodecagons (12-sided polygons). Your class should implement the Shape interface defined in this chapter. A Dodecagon object is defined by its side length as passed to its constructor. The common formulas for the area and perimeter of a
Suppose that the following variables referring to the classes from the previous problem are declared:Pond var1 = new Bay();Object var2 = new Ocean();Which of the following statements produce compiler errors? For the statements that do not produce errors, what is the output of each statement?((Lake)
Write a class named Hexagon whose objects represent regular hexagons (6-sided polygons). Your class should implement the Shape interface defined in this chapter.
Assume that the following classes have been defined:What output is produced by the following code fragment? 1 public class Bay extends Lake { public void method1 () { 3 System.out.print ("Bay 1 "); 4 super.method2 (); public void method2 () { 7 System.out.print ("Bay 2 "); } 1 public class Pond {
Write a class named Octagon whose objects represent regular octagons (eight-sided polygons). Your class should implement the Shape interface defined in this chapter, including methods for its area and perimeter. An Octagon object is defined by its side length. (You may need to search online to find
Using the classes from the previous problem, write the output that is produced by the following code fragment:Data from Previous ProblemAssume that the following classes have been defined:What output is produced by the following code fragment? public static void main (String[] args) { Seacreature[]
Assume that the following classes have been defined:What output is produced by the following code fragment? 1 public class Mammal extends SeaCreature { public void methodi () { System.out.println ("warm-blooded"); 3. 1 public class SeaCreature { public void methodl () { System.out.println
Add an equals method to the Cash class introduced in this chapter. Two cash objects are considered equal if they represent the same amount of money.
Using the classes from the previous problem, write the output that is produced by the following code fragment.Data from Previous ProblemAssume that the following classes have been defined:What is the output produced by the following code fragment? public static void main (String [] args) { Moo []
Add an equals method to the TimeSpan class introduced in Chapter 8. Two time spans are considered equal if they represent the same number of hours and minutes.
Assume that the following classes have been defined:
FilteredAccount . A cash processing company has a class called Account used to process transactions:Account objects interact with Transaction objects, which have many methods including.Design a new class called FilteredAccount whose instances can be used in place of normal accounts but which
Using the A , B , C , and D classes from this section, what is the output of the following code fragment? public static void main (String [] args) { A[] elements {new B(), new D(), new A (), new C()}; for (int i = 0; i < elements.length; i++) { elements [i].method2 () ; System.out.println (elements
DiscountBill . Suppose a class GroceryBill keeps track of a list of items being purchased at a market:Grocery bills interact with Item objects, each of which has the public methods that follow. A candy bar item might cost 1.35 with a discount of 0.25 for preferred customers, meaning that preferred
Consider the following classes:public class Vehicle {...}public class Car extends Vehicle {...}public class SUV extends Car {...}Which of the following are legal statements?a. Vehicle v = new Car();b. Vehicle v = new SUV();c. Car c = new SUV();d. SUV s = new SUV();e. SUV s = new Car();f. Car c =
MinMaxAccount . A company has written a large class BankAccount with many methods including:Design a new class MinMaxAccount whose instances can be used in place of a bank account but include new behavior of remembering the minimum and maximum balances ever recorded for the account. The class
Suppose the Truck code from the previous problem changes to the following:Using the same variables from the previous problem, what is the output from the following statements?System.out.println(mytruck);mytruck.m1();mytruck.m2();Data from Previous ProblemConsider the following two automobile
Implement a class called StudentAdvanceTicket to represent tickets purchased in advance by students. A student advance ticket is constructed with a ticket number and with the number of days in advance that the ticket was purchased. Student advance tickets purchased 10 or more days before the event
Consider the following two automobile classes:Given the following declared variables, what is the output from the following statements? public class Car { public void ml () { System.out.println ("car 1"); } public void m2 () { System.out.println ("car 2"); public String toString () { return
Implement a class called AdvanceTicket to represent tickets purchased in advance. An advance ticket is constructed with a ticket number and with the number of days in advance that the ticket was purchased. Advance tickets purchased 10 or more days before the event cost $30, and advance tickets
Write a version of the setAge method in the UndergraduateStudent class that not only sets the age but also increments the year field’s value by one.
Implement a class called WalkupTicket to represent a walk-up event ticket. Walk-up tickets are also constructed by number, and they have a price of $50.
Write a constructor for the UndergraduateStudent class that accepts a name as a parameter and initializes the UnderGraduateStudent ’s state with that name, an age value of 18, and a year value of 0.
For the next three problems, consider the following class:Also consider the following partial implementation of a subclass of Student to represent undergraduate students at a university:Can the code in the UndergraduateStudent class access the name and age fields it inherits from Student ? Can it
Write an inheritance hierarchy to model items at a library. Include books, magazines, journal articles, videos, and electronic media such as CDs. Include in a superclass and/or interface common information that the library must have for every item, such as a unique identification number and title.
Write a class MonsterTruck that relates to the Car and Truck classes from Self-Check Problems 9 and 10 and whose methods have the following behavior. Whenever possible, use inheritance to reuse behavior from the superclasses.Data from Self Problem 9Suppose the Truck code from the previous problem
Explain the difference between the this keyword and the super keyword. When should each be used?
Write an inheritance hierarchy that stores data about sports players. Create a common superclass and/or interface to store information common to any player regardless of sport, such as name, number, and salary. Then create subclasses for players of your favorite sports, such as basketball, soccer,
Write a class HarvardLawyer to accompany the other law firm classes described in this chapter. Harvard lawyers are like normal lawyers, but they make 20% more money than a normal lawyer, they get 3 days more vacation, and they have to fill out four of the lawyer’s forms to go on vacation. That
Which of the following is the correct syntax to indicate that class A is a subclass of B?a. public class B extends A {b. public class A : super B {c. public A(super B) {d. public class A extends B {e. public A implements B {
Write a set of classes that define the behavior of certain animals. They can be used in a simulation of a world with many animals moving around in it. Different kinds of animals will move in different ways (you are defining those differences). As the simulation runs, animals can “die” when two
Write a class Janitor to accompany the other law firm classes described in this chapter. Janitors work twice as many hours per week as other employees (80 hours/week), they make $30,000 ($10,000 less than general employees), they get half as much vacation as other employees (only 5 days), and they
What is the difference between overloading and overriding a method?
Write an inheritance hierarchy of three-dimensional shapes. Make a top-level shape interface that has methods for getting information such as the volume and surface area of a three-dimensional shape. Then make classes and subclasses that implement various shapes such as cubes, rectangular prisms,
Write the class Marketer to accompany the other law firm classes described in this chapter. Marketers make $50,000 ($10,000 more than general employees) and have an additional method called advertise that prints "Act now, while supplies last!" Make sure to interact with the superclass as
What is code reuse? How does inheritance help achieve code reuse?
Add accessor methods to the Stock class to return the stock’s symbol, total shares, and total cost.
Why didn’t we choose to put the console I/O code into the Stock class?
What is cohesion? How can you tell whether a class is cohesive?
How does encapsulation allow you to change the internal implementation of a class?
Add methods called setFirstName, setMiddleInitial, and setLastName to your Name class. Give the parameters the same names as your fields, and use the this keyword in your solution.Data from Problem 26(You must complete Self-Check Problem 7 before answering this question.)Encapsulate the Name class.
Encapsulate the Name class. Make its fields private and add appropriate accessor methods to the class.Data from Problem 7Create a class called Name that represents a person’s name. The class should have fields representing the person’s first name, last name, and middle initial. (Your class
Add methods named setX and setY to the Point class that allow clients to change a Point object’s x- and -coordinates, respectively.
When fields are made private, client programs cannot see them directly. How do you allow classes access to read these fields’ values, without letting the client break the object’s encapsulation?
What is the difference between the public and private keywords? What items should be declared private?
Add the following method to your Rectangle class:public boolean contains(Rectangle rect)Returns whether the given other rectangle lies entirely within the bounds of this rectangle.
What is abstraction? How do objects provide abstraction?
Add the following method to your Rectangle class:public Rectangle intersection(Rectangle rect)Returns a new Rectangle that represents the largest rectangular region completely contained within both this Rectangle and the given other Rectangle. If the Rectangles do not intersect at all, returns a
Add a constructor to the Point class that accepts another Point as a parameter and initializes this new Point to have the same (x, y) values. Use the keyword this in your solution.
Add the following method to your Rectangle class:public Rectangle union(Rectangle rect)Returns a new Rectangle that represents the area occupied by the tightest bounding box that contains both this Rectangle and the given other Rectangle .
What is the meaning of the keyword this? Describe three ways that the keyword can be used.
Add the following accessor methods to your Rectangle class:public boolean contains(int x, int y)public boolean contains(Point p)Returns whether the given Point or coordinates lie inside the bounds of this Rectangle .
Add a constructor to the Name class that accepts a first name, middle initial, and last name as parameters and initializes the Name object’s state with those values.Data from Problem 7Create a class called Name that represents a person’s name. The class should have fields representing the
Add the following constructor to your Rectangle class:public Rectangle(Point p, int width, int height)Constructs a new Rectangle whose top-left corner is specified by the given Point and with the given width and height .
What are two major problems with the following constructor? public void Point (int initialX, int initialY) { int x = initialX; int y = initialY;
Write a class called Rectangle that represents a rectangular twodimensional region. Your Rectangle objects should have the following methods:public Rectangle(int x, int y, int width, int height)Constructs a new Rectangle whose top-left corner is specified by the given coordinates and with the given
What is a constructor? How is a constructor different from other methods?
Add the following accessor method to your Line class:public boolean isCollinear(Point p)Returns true if the given Point is collinear with the Points of this Line—that is, if, when this Line is stretched infinitely, it would eventually hit the given Point. Points are collinear if a straight line
Finish the following client code so that it constructs two Point objects, translates each, and then prints their coordinates.
Add the following constructor to your Line class:public Line(int x1, int y1, int x2, int y2)Constructs a new Line that contains the given two Points .
Write a toString method for the Name class that returns a String such as "John Q. Public".Data from Problem 7Create a class called Name that represents a person’s name. The class should have fields representing the person’s first name, last name, and middle initial. (Your class should contain
Add the following accessor method to your Line class:public double getSlope()Returns the slope of this Line . The slope of a line between points (x1, y1) and (x2, y2) is equal to (y2 − y1) / (x2 − x1). If x2 equals x1 the denominator is zero and the slope is undefined, so you may throw an
Write a class called Line that represents a line segment between two Points. Your Line objects should have the following methods:public Line(Point p1, Point p2)Constructs a new Line that contains the given two Points.public Point getP1()Returns this Line ’s first endpoint.public Point
The Point class in the java.awt package has a toString method that returns a String in the following format:java.awt.Point[x=7,y=2]Write a modified version of the toString method on our Point class that returns a result in this format.
Add a transfer method to the BankAccount class from the previous exercises. Your method should move money from the current bank account to another account. The method accepts two parameters: a second BankAccount to accept the money, and a real number for the amount of money to transfer. There is a
The following println statement (the entire line) is equivalent to what?Point p1 = new Point();...System.out.println(p1);a. System.out.println(toString(p1));b. p1.toString();c. System.out.println(p1.toString());d. System.out.println(p1.string());e. System.out.println(Point.toString());
Add a toString method to the BankAccount class from the previous exercise. Your method should return a string that contains the account’s name and balance separated by a comma and space. For example, if an account object named yana has the name "Yana" and a balance of 3.03, the call
How do you write a class whose objects can easily be printed on the console?
Suppose the following BankAccount class has been created:Add a field to the BankAccount class named transactionFee for a real number representing an amount of money to deduct every time the user withdraws money. The default value is $0.00, but the client can change the value. Deduct the transaction
Add two new methods to the Name class:public String getNormalOrder()Returns the person’s name in normal order, with the first name followed by the middle initial and last name. For example, if the first name is "John", the middle initial is "Q", and the last name is "Public", returns "John Q.
Add the following mutator method to the Stock class:public void clear()Resets this Stock ’s number of shares purchased and total cost to 0.
Add a new method to the Point class we developed in this chapter:public double distance(Point other)Returns the distance between the current Point object and the given other Point object. The distance between two points is equal to the square root of the sum of the squares of the differences of
Add the following mutator method to the TimeSpan class:public void scale(int factor)Scales this time span by the given factor. For example, 1 hour and 45 minutes scaled by 2 equals 3 hours and 30 minutes.
Suppose we have written a class called BankAccount with a method inside it, defined as:public double computeInterest(int rate)If the client code has declared a BankAccount variable named acct , which of the following would be a valid call to the above method?a. double result = computeInterest(acct,
Add the following mutator method to the TimeSpan class:public void subtract(TimeSpan span)Subtracts the given amount of time from this time span.
What is the difference between an accessor and a mutator? What naming conventions are used with accessors and mutators?
Add the following mutator method to the TimeSpan class:public void add(TimeSpan span)Adds the given amount of time to this time span.
Create a class called Name that represents a person’s name. The class should have fields representing the person’s first name, last name, and middle initial. (Your class should contain only fields for now.)
Add the following accessor method to the Point class:public boolean isCollinear(Point p1, Point p2)Returns whether this Point is collinear with the given two other Points. Points are collinear if a straight line can be drawn that connects them. Two basic examples are three points that have the same
Explain the differences between a field and a parameter. What is the difference in their syntax? What is the difference in their scope and the ways in which they may be used?
Showing 500 - 600
of 1041
1
2
3
4
5
6
7
8
9
10
11
Step by Step Answers