20626.
Write the definition of a class Simple . The class has no constructors , methods or instance variables
ANSWER
public class Simple
{
}
---------------------------------------------------------------------------------------------------------------------
20627.
Write the definitions of two classes Day and Night . Both classes have no constructors , methods or instance variables .
Note: For this exercise, please do not declare your classes using the public visibility modifier.
ANSWER
class Day
{
}
class Night
{
}
-------------------------------------------------------------------------------------------------------------------
20628.
Write the definition of a class Clock . The class has no constructors and one instance variable of type int called hours .
ANSWER
public class Clock
{
private int hours;
}
--------------------------------------------------------------------------------------------------------------------
20629.
Write the definition of a class Clock . The class has no constructors and two instance variables . One is of type int called hours and the other is of type boolean called isTicking .
ANSWER
public class Clock
{
private int hours;
private boolean isTicking;
}
---------------------------------------------------------------------------------------------------------------------
20630.
Write the definition of a class Clock . The class has no constructors and three instance variables . One is of type int called hours , another is of type boolean called isTicking , and the last one is of type Integer called diff
ANSWER
public class Clock
{
private int hours;
private boolean isTicking;
private Integer diff;
}
-------------------------------------------------------------------------------------------------------------------
20631.
Write the definition of a class Clock . The class has no constructors and three instance variables . One is of type int called hours , initialized to 12 , another is of type boolean called isTicking , and the last one is of type Integer called diff .
ANSWER
public class Clock
{
private int hours=12;
private boolean isTicking;
private Integer diff;
}
------------------------------------------------------------------------------------------------------------------
20632.
Write the definition of a class Clock . The class has no constructors and three instance variables . One is of type int called hours , initialized to 12 , another is of type boolean called isTicking , initialized to true , and the last one is of type Integer called diff , initialized to 5 .
ANSWER
public class Clock
{
private int hours = 12;
private boolean isTicking = true;
private Integer diff = 5;
}
-----------------------------------------------------------------------------------------------------------------
20744.
Write the definition of a class Player containing:
An instance variable name of type String , initialized to the empty String .
An instance variable score of type int , initialized to zero.
A method called setName that has one parameter , whose value it assigns to the instance variable name .
A method called setScore that has one parameter , whose value it assigns to the instance variable score .
A method called getName that has no parameters and that returns the value of the instance variable name .
A method called getScore that has no parameters and that returns the value of the instance variable score .
No constructor need be defined.
ANSWER
public class Player
{
private String name="";
private int score = 0;
public void setName(String nm)
{name = nm;}
public void setScore(int sc)
{score = sc;}
public String getName()
{return name;}
public int getScore()
{return score;}
}
----------------------------------------------------------------------------------------------------------------
20746.
Write the definition of a class PlayListEntry containing:
An instance variable title of type String , initialized to the empty String .
An instance variable artist of type String , initialized to the empty String .
An instance variable playCount of type int , initialized to 0.
In addition, your PlayListclass definition should provide an appropriately named "get" method and "set" method for each of these.
ANSWER
public class PlayListEntry
{
private String title = "";
private String artist = "";
private int playCount = 0;
public void setTitle(String title){
this.title = title;
}
public String getTitle(){
return title;
}
public void setArtist(String artist){
this.artist = artist;
}
public String getArtist(){
return artist;
}
public void setPlayCount(int playCount){
this.playCount = playCount;
}
public int getPlayCount(){
return playCount;
}
}
-----------------------------------------------------------------------------------------------------------------------
20725.
Write the definition of a class Counter containing:
An instance variable counter of type int , initialized to 0.
A method called increment that adds one to the instance variable counter . It does not accept parameters or return a value .
A method called getValue that doesn't accept any parameters . It returns the value of the instance variable counter .
ANSWER
public class Counter
{
private int counter=0;
public void increment()
{
counter++;
}
public int getValue()
{
return counter;
}
}
-------------------------------------------------------------------------------------------------------------------
20726
Write the definition of a class Counter containing:
An instance variable named counter of type int .
A constructor that takes one int argument and assigns its value to counter
A method named increment that adds one to counter . It does not take parameters or return a value .
A method named decrement that subtracts one from counter . It also does not take parameters or return a value .
A method named getValue that returns the value of the instance variable counter .
ANSWER
public class Counter
{
private int counter;
public Counter(int ct)
{
counter = ct;
}
public void increment()
{
counter++;
}
public void decrement()
{
counter--;
}
public int getValue()
{
return counter;
}
}
-------------------------------------------------------------------------------------------------------------------------
20747
Write the definition of a class WeatherForecast that provides the following behavior (methods ):
A method called setSkies that has one parameter , a String .
A method called setHigh that has one parameter , an int .
A method called setLow that has one parameter , an int .
A method called getSkies that has no parameters and that returns the value that was last used as an argument in setSkies .
A method called getHigh that has no parameters and that returns the value that was last used as an argument in setHigh .
A method called getLow that has no parameters and that returns the value that was last used as an argument in setLow .
No constructor need be defined. Be sure to define instance variables as needed by your "get"/"set" methods -- initialize all numeric variables to 0 and any String variables to the empty string .
ANSWER
public class WeatherForecast
{
private String skies="";
private int high;
private int low;
public void setSkies(String skies)
{
this.skies = skies;
}
public void setHigh(int high)
{
this.high = high;
}
public void setLow(int low)
{
this.low=low;
}
public String getSkies()
{
return skies;
}
public int getHigh()
{
return high;
}
public int getLow()
{
return low;
}
}
-----------------------------------------------------------------------------------------------------------------------------
20745
Write the definition of a class ContestResult containing:
An instance variable winner of type String , initialized to the empty String .
An instance variable secondPlace of type String , initialized to the empty String .
An instance variable thirdPlace of type String , initialized to the empty String .
A method called setWinner that has one parameter , whose value it assigns to the instance variable winner .
A method called setSecondPlace that has one parameter , whose value it assigns to the instance variable secondPlace .
A method called setThirdPlace that has one parameter , whose value it assigns to the instance variable thirdPlace .
A method called getWinner that has no parameters and that returns the value of the instance variable winner .
A method called getSecondPlace that has no parameters and that returns the value of the instance variable secondPlace .
A method called getThirdPlace that has no parameters and that returns the value of the instance variable thirdPlace .
No constructor need be defined.
ANSWER
public class ContestResult
{
private String winner="";
private String secondPlace="";
private String thirdPlace="";
public void setWinner(String winner)
{
this.winner = winner;
}
public void setSecondPlace(String secondPlace)
{
this.secondPlace = secondPlace;
}
public void setThirdPlace(String thirdPlace)
{
this.thirdPlace = thirdPlace;
}
public String getWinner()
{
return winner;
}
public String getSecondPlace()
{
return secondPlace;
}
public String getThirdPlace()
{
return thirdPlace;
}
}
----------------------------------------------------------------------------------------------------------------------------------------------
20734
Write a class named Averager containing:
An instance variable named sum of type integer , initialized to 0.
An instance variable named count of type integer , initialized to 0.
A method named getSum that returns the value of sum .
A method named add that accepts an integer parameter . The value of sum is increased by the value of the parameter and the value of count is incremented by one.
A method named getCount that accepts no parameters . getCount returns the value of the countinstance variable , that is, the number of values added to sum .
A method named getAverage that accepts no parameters . getAverage returns the average of the values added to sum . The value returned should be a value of type double (and therefore you must cast the instance variables to double prior to performing the division).
ANSWER
public class Averager
{
private int sum=0;
private int count=0;
public int getSum()
{
return sum;
}
public void add(int val)
{
sum += val;
count ++;
}
public int getCount ()
{
return count;
}
public double getAverage ()
{
double avg = (double)sum/count; return avg;
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------
20730
Write a class named Acc1 containing no constructors , methods , or instance variables . (Note, the last character of the classname is "one" not "ell".)
ANSWER
public class Acc1
{
}
--------------------------------------------------------------------------------------------------------------------------------------------------
20731
Write a class named Acc2 containing:
An instance variable named sum of type integer , initialized to 0.
A method named getSum that returns the value of sum .
ANSWER
public class Acc2
{
private int sum=0;
public int getSum()
{
return sum;
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------
20732
Write a class named Accumulator containing:
An instance variable named sum of type integer , initialized to 0.
A method named getSum that returns the value of sum .
A method named add that accepts an integer parameter . The value of sum is increased by the value of the parameter .
ANSWER
--------------------------------------------------------------------------------------------------------------------------------------------------
20735
Write a class named GasTank containing:
An instance variable named amount of type double , initialized to 0.
A method named addGas that accepts a parameter of type double . The value of the amountinstance variable is increased by the value of the parameter .
A method named useGas that accepts a parameter of type double . The value of the amountinstance variable is decreased by the value of the parameter .
A method named getGasLevel that accepts no parameters . getGasLevel returns the value of the amountinstance variable .
ANSWER
public class GasTank
{
private double amount=0;
public void addGas(double gallon)
{
amount+= gallon;
}
public void useGas(double value)
{
amount -= value;
}
public double getGasLevel()
{
return amount;
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------
20651
Write the definition of a method printDottedLine , which has no parameters and doesn't return anything. The method prints to standard output a single line (terminated by a newline) consisting of five periods.
ANSWER
public static void printDottedLine ()
{
System.out.println(".....");
}
-------------------------------------------------------------------------------------------------------------------------------------------------------
20652
Write the definition of a method printGrade , which has a char parameter and returns nothing. The method prints on a line by itself the message string Grade: followed by the char parameter (printed as a character ) to standard output . Don't forget to put a new line character at the end of your line. Thus if the value of the parameter is 'A', the method prints out
Grade: A
ANSWER
public static void printGrade
( char x )
{
System.out.println("Grade: " + x);
}
------------------------------------------------------------------------------------------------------------------------------------------------------
20655
Write the definition of a method twice , which receives an integer parameter and returns an integer that is twice the value of the parameter .
ANSWER
public int twice (int x)
{
return 2*x;
}
------------------------------------------------------------------------------------------------------------------------------------------------------
20656
Write the definition of a method add , which receives two integer parameters and returns their sum .
ANSWER
public int add (int x,int y)
{
return x+y;
}
----------------------------------------------------------------------------------------------------------------------------------------------------
20840
Write a method , makeSubjectLine, that gets a String argument and returns the same String but with "Subject: " in front of it. So if the argument is "Are you going to the show?" the method returns "Subject: Are you going to the show?".
ANSWER
public String makeSubjectLine (String aString)
{
return "Subject: " + aString;
}
-----------------------------------------------------------------------------------------------------------------------------------------------------
20841
Write a method , getEmailUserName, that is passed a String argument that is an email address and returns the user-name part. So if "mozart@salzberg.de" is passed, the method returns "mozart". Assume that the argument will always be a correctly formatted email address.
ANSWER
public String getEmailUserName (String aString)
{
return aString.substring(0, aString.indexOf("@"));
}
-----------------------------------------------------------------------------------------------------------------------------------------------------
20842
Write a method , getEmailDomain, that is passed a String argument that is an email address and returns the domain name part. So if "mozart@salzberg.de" is passed, the method returns "salzberg.de". Assume that the argument will always be a correctly formatted email address.
ANSWER
public String getEmailDomain (String aString)
{
return aString.substring(aString.indexOf("@") + 1, aString.length());
}
-------------------------------------------------------------------------------------------------------------------------------------------------------
20843
Write a method , makeEmailAddress, that is passed two String arguments : the first is a username, like "leadbelly" and the second is a domain name , like "blues.com". The method returns an email address formed from joining these with an "@": "leadbelly@blues.com".
ANSWER
public String makeEmailAddress (String aString, String bString)
{
return aString + "@" + bString;
}
-------------------------------------------------------------------------------------------------------------------------------------------------------
20844
Write a method , getFirstLine, that is passed a String argument and that returns the first line. (Recall that lines are terminated with the "\n" character .) Assume that the argument contains at least one complete, newline-terminated line.
ANSWER
public String getFirstLine (String aString)
{
return aString.substring(0, aString.indexOf("\n"));
}
-------------------------------------------------------------------------------------------------------------------------------------------------------
20845
Write a method , getSecondLine, that is passed a String argument and that returns the second line, without its newline character . (Recall that lines are terminated with the "\n" character .) Assume that the argument contains at least two complete, newline-terminated lines.
ANSWER
public String getSecondLine (String aString)
{
return aString.substring(aString.indexOf("\n") + 1,
aString.indexOf("\n") + 1 + aString.substring(aString.indexOf("\n") + 1, aString.length()).indexOf("\n"));
}
-------------------------------------------------------------------------------------------------------------------------------------------------------
Answer for 20732
ReplyDeletepublic class Accumulator{
private int sum = 0;
public int getSum(){
return sum;
}
public void add(int number){
sum += number;
}
}