MyProgrammingLab - Chapter 8: A Second Look at Classes and Objects (Tony Gaddis)
Write the code for invoking a static method named sendDouble, provided by the DataTransmitter class. There is one double argument for this method. Assume that a double variable called x has already been declared and initialized to some value. Use this variable's value as an argument in your method invocation.
DataTransmitter.sendDouble(x);
Write the code for invoking a static method named sendNumber , provided by the DataTransmitter class. There is one int argument for this method. Invoke this method and use the number 5 as an argument.
DataTransmitter.sendNumber(5);
Assume the availability of class named DataTransmitter that provides a static method, sendSignal that takes no arguments. Write the code for invoking this method.
DataTransmitter.sendSignal();
Assume the availability of class named DateManager that provides a static method, printTodaysDate, that accepts no arguments and returns no value. Write a statement that calls printTodaysDate.
DateManager.printTodaysDate();
Assume the availability of class named Logger that provides a static method, printErrorDescription, that accepts one int argument and returns no value. Write a statement that invokes the method printErrorDescription, passing it the value 14.
Logger.printErrorDescription(14);
Assume the availability of a class named Logger that provides a static method, printLarger that accepts two int arguments and returns no value. Two int variables, sales1 and sales2, have already been declared and initialized. Write a statement that calls printLarger, passing it sales1 and sales2.
Logger.printLarger(sales1, sales2);
A Color class has three integer color component instance variable: red, green, and blue. Write a toString method for this class. It should return a string consisting of the three color components (in the order red, green, blue) within parentheses, separated by commas, with a '#' prefix e.g. #(125, 30, 210)
public String toString() { return "#(" + red + "," + green + "," + blue + ")"; }
Assume the existence of a Window class with integer instance variables width, height, xPos and yPos. Define the method toString for the Window class. The String representation of a Window object should be of the form "A 'width'x'height' window at ('xPos','yPos')" where the values within the '''s are replaced by the actual values of the corresponding instance variables. For example, given a Window object whose width is 80, height is 20, xPos is 0 and yPos is 30, toString should return "A 80x20 window at (0,30)"
public String toString() { return "A " + width + "x" + height + " window at (" + xPos + "," + yPos + ")"; }
Define the method equals for the Window class. Two windows will be considered equal if they are the same size and at the same position
public boolean equals(Window a) { return (a.width == this.width && a.height == this.height && a.xPos == this.xPos && a.yPos == this.yPos); }
Write the definition of a class Counter containing: An instance variable named counter of type int. An instance variable named limit of type int. A static int variable named nCounters which is initialized to 0. A constructor taking two int parameters that assigns the first one to counter and the second one to limit. It also adds one to the static variable nCounters. A method named increment. It does not take parameters or return a value; if the instance variable counter is less than limit, increment just adds one to the instance variable counter. A method named decrement that also doesn't take parameters or return a value; if counter is greater than zero, it just subtracts one from the counter. A method named getValue that returns the value of the instance variable counter. A static method named getNCounters that returns the value of the static variable nCounters.
public class Counter { private int counter; private int limit; private static int nCounters = 0; public Counter(int a, int b) { counter = a; limit = b; nCounters++; } public void increment() { if(counter < limit) counter += 1; } public void decrement() { if(counter > 0) counter -= 1; } public int getValue() { return counter; } public static int getNCounters() { return nCounters; } }
Write the definition of a class Telephone. The class has no constructors, one instance variable of type String called number, and two static variables. One is of type int called quantity, initialized to 250; the other is of type double called total, initialized to 15658.92.
public class Telephone { private String number; private static int quantity = 250; private static double total = 1565.92; }
Assume that two classes 'Temperature' and 'Sensor' have been defined. 'Temperature' has a constructor that accepts a double parameter. 'Sensor' has a method named 'getReading' which returns the sensor's current reading (a double). Write a static method 'create' (that could be added to the 'Temperature' class) that accepts a 'Sensor' object. 'create' gets the value of the current reading of the 'Sensor' object, and returns a new 'Temperature' object that is based on this reading.
public static Temperature create(Sensor a) { return new Temperature(a.getReading()); }
Write the code for invoking a static method named sendTwo, provided by the DataTransmitter class. There are two arguments for this method: a double and an int. Invoke the method with the double value of 15.955 and the int value of 133.
DataTransmitter.sendTwo(15.955,133);
Assume the availability of class named IMath that provides a static method, toThePowerOf is a method that accepts two int arguments and returns the value of the first parameter raised to the power of the second. An int variable cubeSide has already been declared and initialized. Another int variable, cubeVolume, has already been declared. Write a statement that calls toThePowerOf to compute the value of cubeSide raised to the power of 3 and that stores this value in cubeVolume.
cubeVolume = IMath.toThePowerOf(cubeSide, 3);
Assume the availability of a class named Arithmetic that provides a static method, add, that accepts two int arguments and returns their sum. Two int variables, euroSales and asiaSales, have already been declared and initialized. Another int variable, eurasiaSales, has already been declared. Write a statement that calls add to compute the sum of euroSales and asiaSales and that stores this value in eurasiaSales.
eurasiaSales = Arithmetic.add(euroSales,asiaSales);
Write the definition of a class Counter containing: An instance variable named counter of type int. An instance variable named counterID of type int. A static int variable nCounters which is initialized to zero. A constructor that takes an int argument and assigns its value to counter. It also adds one to the static variable nCounters and assigns the result to the instance variable counterID. A method named increment. It does not take parameters or return a value; it just adds one to the instance variable counter. A method named decrement that also doesn't take parameters or return a value; it just subtracts one from the counter. A method named getValue. It returns the value of the instance variable counter. A method named getCounterID: it returns the value of the instance variable counterID.
public class Counter { private int counter; private int counterID; private static int nCounters = 0; public Counter(int a) { counter = a; counterID = ++nCounters; } public void increment() { counter += 1; } public void decrement() { counter -= 1; } public int getValue() { return counter; } public int getCounterID() { return counterID; } }
Write the definition of a class Telephone. The class has no constructors, one instance variable of type String called number, and one static variable of type int called quantity.
public class Telephone { private String number; private static int quantity; }
Write the definition of a class Telephone. The class has no constructors and one static method getFullNumber. The method accepts a String argument and returns it, adding 718- to the beginning of the argument.
public class Telephone { public static String getFullNumber(String a) { return ("718-" + a); } }
Write the definition of a class Telephone. The class has no constructors and one static method printNumber. The method accepts a String argument and prints it on the screen. The method returns nothing.
public class Telephone { public static void printNumber(String s) { System.out.print(s); } }
Write the definition of a class Telephone. The class has no constructors, one instance variable of type String called number, and two static variables. One is of type int called quantity; the other is of type double called total. Besides that, the class has one static method makeFullNumber. The method accepts two arguments, a String containing a telephone number and an int containing an area code. The method concatenates the two arguments in the following manner: First comes the area code, then a dash, then the telephone number. The method returns the resultant string.
public class Telephone { private String number; private static int quantity; private static double total; public static String makeFullNumber(String a, int b) { return (b + "-" + a); } }