All Matches
Solution Library
Expert Answer
Textbooks
Search Textbook questions, tutors and Books
Oops, something went wrong!
Change your search query and then try again
Toggle navigation
FREE Trial
S
Books
FREE
Tutors
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
Hire a Tutor
AI Study Help
New
Search
Search
Sign In
Register
study help
computer science
building java programs a back to basics approach
Questions and Answers of
Building Java Programs A Back to Basics Approach
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]
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",
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<> =
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
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
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
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[]
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
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 ();
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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,
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
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
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
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
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.
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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.
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
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
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
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
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
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
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
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
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