Chapter 7 CodeLab

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

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.

class Day { } class Night { }

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.

public class Clock { private int hours = 12; private boolean isTicking = true; private Integer diff = 5; }

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.

public class Clock { private int hours; private boolean isTicking; private Integer diff; }

Write the definition of a class Simple. The class has no constructors , methods or instance variables .

public class Simple { }

You are given a class named Clock that has three instance variables : One of type int called hours, another of type boolean called isTicking, and the last one of type Integer called diff. Write a constructor for the class Clock that takes three parameters -- an int , a boolean , and another int . The constructor should set the instance variables to the values provided.

public Clock (int hours, boolean isTicking, int diff){ this.hours = hours; this.isTicking = isTicking; this.diff = new Integer (diff); }

You are given a class named Clock that has one int instance variable called hours. Write a constructor with no parameters for the class Clock. The constructor should set hours to 12.

public Clock() { hours = 12; }

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

public class Acc1 { }

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 .

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

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 .

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

Write a class named Accumulator containing: An instance variable named sum of type integer . A constructor that accepts an integer parameter , whose value is used to initialize the sum instance variable . 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 .

public class Accumulator{ private int sum; public Accumulator (int sum){ this.sum = sum; } public int getSum(){ return sum; } public void add (int value){ sum += value; } }

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

public class Averager{ private int sum = 0; private int count = 0; public int getSum(){ return sum;} public void add(int in) { sum = sum + in; count ++;} public int getCount(){return count;} public double getAverage(){ return (double)sum/count; } }

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.

public class Clock { private int hours; private boolean isTicking; }

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.

public class Clock { private int hours = 12; private boolean isTicking; private Integer diff; }

Write the definition of a class Clock. The class has no constructors and one instance variable of type int called hours.

public class Clock { private int hours; }

Write the definition of a class Clock. The class has three instance variables : One of type int called hours, another of type boolean called isTicking, and the last one of type Integer called diff. You should also write a constructor that takes three parameters -- an int , a boolean , and another int . The constructor should set the instance variables to the values provided.

public class Clock { private int hours; private boolean isTicking; private Integer diff; public Clock ( int hours, boolean isTicking, int diff) { this.hours = hours; this.isTicking = isTicking; this.diff = diff; } }

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.

public class ContestResult{ private String winner = ""; private String secondPlace = ""; private String thirdPlace = ""; public void setWinner(String win){ winner = win; } public void setSecondPlace(String second){ secondPlace = second; } public void setThirdPlace(String third){ thirdPlace = third; } public String getWinner(){ return winner; } public String getSecondPlace(){ return secondPlace; } public String getThirdPlace(){ return thirdPlace; } }

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.

public class Counter{ private int counter = 0; public void increment(){ counter++; } public int getValue(){ return counter; } }

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.

public class Counter{ private int counter; public Counter (int counter){ this.counter = counter; } public void increment (){ counter++; } public void decrement (){ counter--; } public int getValue (){ return counter; } }

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.

public class Counter{ private int counter; private int limit; public Counter(int a,int b){ counter=a; limit=b; } public void increment(){ if (counter < limit) counter++; } public void decrement(){ if(counter > 0) counter--; } public int getValue(){ return counter; } }

Write a class named GasTank containing: An instance variable named amount of type double , initialized 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 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 greater than capacity-0.1, and false otherwise. A method named getGasLevel that accepts no parameters . getGasLevel returns the value of the amount instance variable .

public class GasTank { private double amount = 0; private double capacity; public GasTank(double _capacity) { capacity = _capacity; } public void addGas(double gas) { if (amount + gas > capacity) { amount = capacity; } else { amount = amount + gas; } } public void useGas(double gasUsed) { if (amount - gasUsed < 0) { amount = 0; } else { amount = amount - gasUsed; } } public boolean isEmpty() { if (amount < 0.1) { return true; } return false; } public boolean isFull() { if (amount > capacity - 0.1) { return true; } return false; } public double getGasLevel() { return amount; } }

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 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 getGasLevel that accepts no parameters . getGasLevel returns the value of the amount instance variable .

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

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 .

public class GasTank{ private double amount = 0; public void addGas (double value){ amount += value; } public void useGas (double value){ amount -= value; } public double getGasLevel (){ return amount; } }

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 PlayList class definition should provide an appropriately named "get" method and "set" method for each of these. No constructor need be defined.

public class PlayListEntry{ private String title = ""; private String artist = ""; private int playCount = 0; public void setTitle(String theTitle){ title = theTitle; } public void setArtist(String theArtist){ artist = theArtist; } public void setPlayCount(int thePlayCount){ playCount = thePlayCount; } public String getTitle(){ return title; } public String getArtist(){ return artist; } public int getPlayCount(){ return playCount; } }

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.

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

Write a class definition of a class named 'Value ' with the following: a constructor accepting a single integer parameter a constructor with no parameters a method 'setVal' that accepts a single parameter , a boolean method , 'wasModified' that returns true if setVal was ever called for the object . a method 'getVal' that returns an integer value as follows: if setVal has ever been called, it getVal returns the last value passed to setVal. Otherwise if the "single int parameter " constructor was used to create the object , getVal returns the value passed to that constructor . Otherwise getVal returns 0.

public class Value { private boolean modified = false; public int val; public Value(int v){ val=v; } public int getVal(){ return val; } public void setVal(int st){ val=st; modified=true; } public boolean wasModified(){ return modified; } }

Write a class definition of a class named 'Value ' with the following: a boolean instance variable named 'modified', initialized to false an integer instance variable named 'val' a constructor accepting a single parameter whose value is assigned to the instance variable 'val' a method 'getVal' that returns the current value of the instance variable 'val' a method 'setVal' that accepts a single parameter , assigns its value to 'val', and sets the 'modified' instance variable to true , and a boolean method , 'wasModified' that returns true if setVal was ever called.

public class Value { private boolean modified = false; private int val; public Value (int val) { this.val = val; } public int getVal() { return val; } public void setVal(int val) { this.val = val; modified = true; } public boolean wasModified() { if (modified == true) { return true; } else return false; } }

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 .

public class WeatherForecast{ private String skies = ""; private int high = 0; private int low = 0; public void setSkies(String theSkies){ skies = theSkies; } public void setHigh(int theHigh){ high = theHigh; } public void setLow(int theLow){ low = theLow; } public String getSkies(){ return skies; } public int getHigh(){ return high; } public int getLow(){ return low; } }


Ensembles d'études connexes

Western Civ Midterm Review-Chapter 3

View Set

Financial Accounting 15th edition Chapter 4-5

View Set

Basic Care and Comfort NCLEX questions

View Set

Adult Health Final Exam Nclex -Review for weeks 4-7

View Set

Harvard Business Writing in Business Exam

View Set