Java Programming Lab

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 .

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

SOLUTION
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;

} }

20727
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 constructor that takes two int arguments and assigns the first one to counter and the second one to limit
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 . It also does not 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 .

SOLUTION
public class Counter {
private int counter;
private int limit;
public Counter (int counter, int limit) {
this.counter = counter;
this.limit = limit;
} public void increment () {
if (counter < limit) { counter++; } } public void decrement () { if (counter > 0) {
counter–;
} } public int getValue () {
return counter;} }

****Corrected Solutions****

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;
} }


20730
Write a class named Acc1 containing no constructors, methods, or instance variables. (Note, the last character or the classname is “one” not “ell”.)

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.

SOLUTION
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

SOLUTION
public class Accumulator{
private int sum = 0;
public int getSum(){
return sum;
} public void add(int number){
sum += number; } }


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

SOLUTION
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 = (doubdouble)sum/count; return avg;
}

}

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 amount instance variable is increased by the value of the parameter. A method named useGas that accepts a parameter of type double. The value of the amount instance variable is decreased by the value of the parameter. A method named getGasLevel that accepts no parameters. getGasLevel returns the value of the amount instance variable.

SOLUTION
public class GasTank {
private double amount = 0;
public void addGas(double x) {
amount += x;
}
public void useGas(double y) {
amount -= y;
if (amount < 0)
{
amount = 0;
}
}
public boolean isEmpty(){
if (amount < 0.1)
{
return true;
}
else
{
return false;
}
}
public double getGasLevel(){
return amount;
}

}

20736
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 amount instance variable is increased by the value of the parameter. A method named useGas that accepts a parameter of type double. The value of the amount instance variable is decreased by the value of the parameter. however, if the value of the amount is decreased below 0, amount is set to 0. A method named isEmpty that accepts no parameters. isEmpty returns a boolean value: true if the value of amount is less than 0.1, and false otherwise. A method named getGas Level that accepts no parameters. getGasLevel returns the value of the amount instance variable.

SOLUTION
public class GasTank {
private double amount = 0;
public void addGas(double n1)
{
amount = amount + n1;
}
public void useGas(double n2)
{
if ((amount – n2) > 0 )
{
amount = amount – n2;
}
else
{
amount = 0;
}

}
public boolean isEmpty()
{
if (amount < 0.1)
return true;
else
return false;
}
public double getGasLevel()
{
return amount;
}

}

20737
Write a class named GasTank containing: An instance variable named amount of type double, initizlied to 0. An instance variable named capacity of type double. A constructor that accepts a parameter of type double. The value of the parameter is used to initialize the value of capacity. A method named addGas that accepts a parameter of type double. The value of the amount instance variable is increased by the value of the parameter. However, if the value of the amount is increased beyond the value of capacity, amount is set to capacity. A method named useGas that accepts a parameter of type double. The value of the amount instance variable is decreased by the value of the parameter. However, if the value of amount is decreased below 0, amount is set to 0. A method named isEmpty that accepts no parameters. isEmpty returns a boolean value: true if the value of amount is less than 0.1, and false otherwise. A method named isFull that accepts no parameters. isFull returns a boolean value: true if the value of amount is great that capacity – 0.1, and false otherwise. A method named getGasLevel that accepts no parameters. getGasLevel returns the value of the amount instance variable.

SOLUTION
public class GasTank {
private double amount = 0;
private double capacity;
public GasTank(double x) {
capacity = x;
}
public void addGas(double a){
amount += a;
if(amount > capacity){
amount = capacity;
}
}
public void useGas(double b) {
amount -= b;
if(amount < 0)
amount = 0;
}
public boolean isEmpty(){
if(amount < 0.1) return true; else return false; } public boolean isFull(){ if(amount > capacity – 0.1)
return true;
else
return false;
}
public double getGasLevel() {
return amount;
}
}



20738
Write a class named GasTank containing: An instance variable named amount of type double, initizlied to 0. An instance variable named capacity of type double. A constructor that accepts a parameter of type double. The value of the parameter is used to initialize the value of capacity. A method named addGas that accepts a parameter of type double. The value of the amount instance variable is increased by the value of the parameter. However, if the value of the amount is increased beyond the value of capacity, amount is set to capacity. A method named useGas that accepts a parameter of type double. The value of the amount instance variable is decreased by the value of the parameter. However, if the value of amount is decreased below 0, amount is set to 0. A method named isEmpty that accepts no parameters. isEmpty returns a boolean value: true if the value of amount is less than 0.1, and false otherwise. A method named isFull that accepts no parameters. isFull returns a boolean value: true if the value of amount is great that capacity – 0.1, and false otherwise. A method named getGasLevel that accepts no parameters. getGasLevel returns the value of the amount instance variable. A method named fillUp that accepts no parameters. fillUp increases amount to capacity and returns the difference between the value of capacity and the original value of amount (that is, the amount of gas that is needed to fill the tank to capacity).

SOLUTION
public class GasTank {
private double amount = 0;
private double capacity;
public GasTank(double x) {
capacity = x;
}
public void addGas(double a){
amount += a;
if(amount > capacity)
amount = capacity;
}
public void useGas(double b) {
amount -= b;
if(amount < 0)
amount = 0;
}
public boolean isEmpty(){
if(amount < 0.1) return true; else return false; } public boolean isFull(){ if(amount > capacity – 0.1)
return true;
else
return false;
}
public double getGasLevel() {
return amount;
}
public double fillUp() {
double difference = capacity-amount;
amount = capacity;
return difference;
}

}


20741
Write a class named ParkingMeter containing:
An instance variable named timeLeft of type int , initialized to 0.
A method named add that accepts an integer parameter . If the value of the parameter is equal to 25, the value of timeLeft is increased by 30; otherwise no increase is performed. add returns a boolean value : true if timeLeft was increased , false otherwise.
A method named tick that accepts no parameters and returns no value . tickdecreases the value of timeLeft by 1, but only if the value of timeLeft is greater than 0.
A method named isExpired that accepts no parameters . isExpired returns a boolean value : true if the value of timeLeft is equal to 0; false otherwise.

SOLUTION
public class ParkingMete{
private int timeLeft = 0;
public boolean add (int x) {
if (x == 25)
{
timeLeft += 30;
return true;
}
else
{
return false;
}
}
public void tick() {
if (timeLeft > 0) {
timeLeft–; }
}
public boolean isExpired(){
if (timeLeft == 0) {
return true; }
else
{
return false;
}
}

}

20742
Write a class named ParkingMeter containing:
Two instance variables named timeLeft and maxTime of type int . The value of timeLeft should be initialized to 0.
A constructor accepting a single integer parameter whose value is used to initialize the maxTimeinstance variable .
A method named add that accepts an integer parameter . If the value of the parameter is equal to 25, the value of timeLeft is increased by 30; otherwise no increase is performed. Furthermore, the increase occurs only if the value of timeLeft will not exceed the value of maxTime . add returns a boolean value : true if timeLeft was increase , false otherwise.
A method named tick that accepts no parameters and returns no value . tickdecreases the value of timeLeft by 1, but only if the value of timeLeft is greater than 0.
A method named isExpired that accepts no parameters . isExpired returns a boolean value : true if the value of timeLeft is equal to 0; false otherwise.

SOLUTION
public class ParkingMeter{
private int timeLeft = 0 ;
private int maxTime;
public ParkingMeter(int x) {
this.maxTime=x;
} public boolean add (int y){
if ((y == 25) && (timeLeft < maxTime)) { timeLeft += 30; return true; } else { return false; } } public void tick() { if (timeLeft > 0) {
–timeLeft;
} } public boolean isExpired(){
if(timeLeft==0) {
return true;
} else {
return false;
}
}

}

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.

SOLUTION
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;
}

}

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.

SOLUTION
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;
}

}

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.

SOLUTION
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;
}

}

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.

SOLUTION
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;
}
}


20787
A String variable , fullName, contains a name in one of two formats:
last name , first name (comma followed by a blank), or
first name last name (single blank)
Extract the first name into the String variable firstName and the last name into the String variable lastName. Assume the variables have been declared and fullNamealready initialized . You may also declare any other necessary variables.

SOLUTION
if (fullName.indexOf(“, “) != -1) {
lastName = fullName.substring(0, fullName.indexOf(“, “));
firstName = fullName.substring(fullName.indexOf(“, “) + 2, fullName.length());
} else {
firstName = fullName.substring(0, fullName.indexOf(” “));
lastName = fullName.substring(fullName.indexOf(” “) + 1, fullName.length());
}

Write an expression that evaluates to true if and only if the integer x is greater than the integer y .

3 thoughts on “Java Programming Lab”

  1. On 20727 there is an error – you have :

    20727
    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 constructor that takes two int arguments and assigns the first one to counter and the second one to limit
    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 . It also does not 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 .

    SOLUTION
    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;
    } }

    The solutions has an error – On line 13: you have “counter-;”
    The correction is that it should have “counter–;”

    ****Corrected Solutions****
    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;
    } }

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

    SOLUTION
    public class Acc2 {
    private int sum=0;
    public int getSum() {
    return sum; }

    Real SOLUTION
    public class Acc2 {
    private int sum = 0;
    public int getSum() {
    return sum; } }

Comments are closed.

%d bloggers like this: