Java – Chapter 7

java - chapter 720638

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 .

ANSWER

public class Telephone

{

private String number;

private static int quantity;

}


20639

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 .

ANSWER

public class Telephone

{

private String number;

private static int quantity = 250;

private static double total = 15658.92;

}


20641

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 .

ANSWER

public class Telephone

{

public static String getFullNumber(String s)

{

return (“718-” + s);

}

}


20640

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.

ANSWER

public class Telephone

{

public static void printNumber(String number)

{

System.out.println(number) ;

}

}


20753
Write a class definition for an abstract class , Vehicle, that contains: a double instance variable , maxSpeed a protected double instance variable , currentSpeed a constructor accepting a double used to initialize the maxSpeed instance variable an abstract method , accelerate, that accepts no parameters and returns nothing. a method getCurrentSpeed that returns the value of currentSpeed a method getMaxSpeed that returns the value of maxSpeed a method , pedalToTheMetal, that repeatedly calls accelerate until the speed of the vehicle is equal to maxSpeed. pedalToTheMetal returns nothing.

ANSWER

 

 

public abstract class Vehicle {

private double maxSpeed;

protected double currentSpeed;

public Vehicle(double maxSpeed) {

this.maxSpeed = maxSpeed;

}

public abstract void accelerate();

public double getCurrentSpeed() {

return currentSpeed;

}
public double getMaxSpeed() {

return maxSpeed;

}
public void pedalToTheMetal() {

while (currentSpeed < maxSpeed) {

accelerate();

}

}


20773

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.

ANSWER

public static Temperature create(Sensor sensor)

{

double reading = sensor.getReading();

return new Temperature(reading);

}


20642

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 .

ANSWER

public class Telephone

{

private String number;

private static int quantity;

private static double total;

public static String makeFullNumber(String telephone, int area)

{

return (area + “-” + telephone);

}

}


20728
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 .

ANSWER

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++;
}
public void decrement(){
if(counter>0)
counter–;
}
public int getValue()
{
return counter;
}

public static int getNCounters()
{
return nCounters;
}
}

 


 

20729
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 .

ANSWER
public class Counter
{
private int counter;
private int counterID;
private static int nCounters=0;
public Counter(int c)
{
counter=c;
nCounters++;
counterID=nCounters;
}
public void increment()
{
counter++;
}
public void decrement()
{
counter–;
}
public int getValue()
{
return counter;
}
public int getCounterID()
{
return counterID;
}
}


21160
Windows on the desktop are just one of many objects used in a graphical user interface (GUI)– buttons, drop-down list boxes, pop-up menus, are just some of the many others. Regardless of their particular appearance, tasks, and structure, all such GUI components share some common functionality– which is handled in a manner unique to the actual component .

Define an interface, GUIComponent, with the following methods : – onClick– void-returning and accepts a single integer parameter – onCursorFocus — void-returning and accept no parameters – move — 2 overloaded methods : both boolean -returning; one accepts a pair of integer parameters ; the second a single parameter of type Position – resize– 2 overloaded methods ; both boolean -returning; one accepts a pair of integer parameters ; the second a single parameter of type Dimension

ANSWER

 


21161
Regardless of the type of communications device, there must be a way to transmit and receive data.
Define an interface, CommDevice, with two methods : transmit, that accepts two parameters — reference to a Destination object , and a string (in that order), and returns a boolean ; and receive, that accepts a parameter of type Duration, and returns a reference to a String .

ANSWER
interface CommDevice
{
public boolean transmit( Destination d, String s);
public String receive( Duration d );
}


21162
Regardles of its particular nature, all financial accounts provide a way to deposit and withdraw money.
Define an interface Account that has two methods : deposit and withdraw, both of which accept a parameter of type Cash and return a boolean

ANSWER
interface Account
{
public boolean deposit(Cash c);
public boolean withdraw(Cash c);
}


21163
In order to respond to the click of a button, an object must be capable of being notified when the button was clicked. This is achieved by the Button object requiring that any such ‘listener’ provide a method named actionPerformed which can then be called to notify the listening object .

Define an interface, ActionListener, with a single void-returning method , actionPerformed that accepts a single parameter of type ActionEvent.

ANSWER
interface ActionListener
{
public void actionPerformed(ActionEvent e);
}

 


21120
Many systems provide the ability to have alternatives to visual or other sensory attributes (such as color or sound) for the purpose of accessibility (for example a picture to be displayed on a Web page may have a textual alternative that could be read by a text-to-speech peripheral for the usually impaired).

Define an interface AccessibleGUIAttribute that contains a single method , getAlternativeText, that returns a string representing the textual alternative of an object . getAlternatrive accepts no parameters .210)

ANSWER
interface AccessibleGUIAttribute
{
public String getAlternativeText();
}


21103
Define an interface GUIComponent consisting of four method declaration :
open: void-returning, accepts no parameters
close: both accepts no parameters , return boolean indicating whether or not the component is willing to be closed at that point (for example, a text window might have unsaved data and should not be closed)
resize: void-returning accepts new width and height (integers ) in that order
move: void returning, accepts new x and y positions (in that order) respectively

ANSWER
interface GUIComponent
{
public void open();
public boolean close();
public void resize(int width, int height);
public void move(int x, int y);
}


 

20761
Write an interface named ‘Test’ with the following behavior :
a method ‘getDuration’ that returns a ‘Duration’ object .
a method ‘check’ that accepts an integer parameter and returns a ‘Result’ object .
a method ‘getScore’ that returns a double .

ANSWER
public interface Test
{
public abstract Duration getDuration();
public abstract Result check(int arg0);
public abstract double getScore();
}


20755
Write an interface, PointingDevice, containing:
an abstract method , getXCoord that returns an int
an abstract method , getYCoord that returns an int
an abstract method , attentionRequired that returns a boolean
an abstract method , setResolution that accepts a double and returns a double

ANSWER
interface PointingDevice{
int getXCoord();
int getYCoord();
boolean attentionRequired();
double setResolution();
}

{
public class getXCoord(){
return x;
}
public int getYCoord(){
return y;
}
public boolean attentionRequired(){
return a;
}
public double setResolution(double a){
return a;
}

}


 

 

%d bloggers like this: