I call this method in java:
private void updateDisplay()
{
displayString = hours.getDisplayValue() + ":" +
minutes.getDisplayValue();
}
What triggers this method two times for hours and minutes:
public String getDisplayValue()
{
if(value < 10) {
return "0" + value;
}
else {
return "" + value;
}
}
My question is how can i check in getDisplayValue if the method is triggerd as minute or as hour? For example:
public String getDisplayValue()
{ if(this == minutes){
if(value < 10) {
return "0" + value;
}
else {
return "" + value;
}
}
Entire code:
public class ClockDisplay
{
private NumberDisplay hours;
private NumberDisplay minutes;
private String displayString; // simulates the actual display
/**
* Constructor for ClockDisplay objects. This constructor
* creates a new clock set at 00:00.
*/
public ClockDisplay()
{
hours = new NumberDisplay(24);
minutes = new NumberDisplay(60);
updateDisplay();
}
/**
* Constructor for ClockDisplay objects. This constructor
* creates a new clock set at the time specified by the
* parameters.
*/
public ClockDisplay(int hour, int minute)
{
hours = new NumberDisplay(24);
minutes = new NumberDisplay(60);
setTime(hour, minute);
}
/**
* This method should get called once every minute - it makes
* the clock display go one minute forward.
*/
public void timeTick()
{
minutes.increment();
if(minutes.getValue() == 0) { // it just rolled over!
hours.increment();
}
updateDisplay();
}
/**
* Set the time of the display to the specified hour and
* minute.
*/
public void setTime(int hour, int minute)
{
hours.setValue(hour);
minutes.setValue(minute);
updateDisplay();
}
/**
* Return the current time of this display in the format HH:MM.
*/
public String getTime()
{
return displayString;
}
/**
* Update the internal string that represents the display.
*/
private void updateDisplay()
{
displayString = hours.getDisplayValue() + ":" +
minutes.getDisplayValue();
}
}
And:
public class NumberDisplay
{
private int limit;
private int value;
/**
* Constructor for objects of class NumberDisplay.
* Set the limit at which the display rolls over.
*/
public NumberDisplay(int rollOverLimit)
{
limit = rollOverLimit;
value = 0;
}
/**
* Return the current value.
*/
public int getValue()
{
return value;
}
/**
* Return the display value (that is, the current value as a two-digit
* String. If the value is less than ten, it will be padded with a leading
* zero).
*/
public String getDisplayValue()
{
if(value < 10) {
return "0" + value;
}
else {
return "" + value;
}
}
/**
* Set the value of the display to the new specified value. If the new
* value is less than zero or over the limit, do nothing.
*/
public void setValue(int replacementValue)
{
if((replacementValue >= 0) && (replacementValue < limit)) {
value = replacementValue;
}
}
/**
* Increment the display value by one, rolling over to zero if the
* limit is reached.
*/
public void increment()
{
value = (value + 1) % limit;
}
}
}
Do it using reflection by checking the stack trace, see Thread#getStackTrace:
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace()
Go through the API and see what methods are useful for your needs.
But why don't you simply pass an identifier that allows you to detect who called the method?
pass a parameter into getDisplayValue() function like this
getDisplayValue(char c)
and change your function definition to :
public String getDisplayValue(char c)
{
if(c == 'h'){
if(value < 10) {
return "0" + value;
}
else {
return "" + value;
}
}
else if(c=='m'){
return value*60;
}
}
and change updateDisplay() to :
private void updateDisplay()
{
displayString = hours.getDisplayValue(h) + ":" +
minutes.getDisplayValue(m);
}
Introduce a boolean parameter in the function declaration
public String getDisplayValue(Boolean isMinute)
{
if(isMinute)
{
if(value < 10) {
return "0" + value;
}
else {
return "" + value;
}
}
else{
// not a minute, continue
}
}
you can call this like
displayString = hours.getDisplayValue(false) + ":" +
minutes.getDisplayValue(true);
i will added a boolean flag in ClockDisplay i.e. isHour. And will change the constructure:
class ClockDisplay{
boolean isHour;
public ClockDisplay(boolean isHour)
{
hours = new NumberDisplay(24);
minutes = new NumberDisplay(60);
updateDisplay();
this.isHour=isHour;
}
...........
...........
}
Now in NumberDisplay i will change the method:
public String getDisplayValue(ClockDisplay c)
{
if(value < 10) {
return "0" + value;
}
else {
return "" + value;
}
c.
}
Now inside the method getDisplayValue() you can call any method on top of c, and it can print accordingly because you have set isHour accordingly.
The reason behind my design is: The abstraction whether hour or minute it should be encapsulated inside ClockDisplay. So just pass the ClockDisplay reference to getDisplayValue().
You can introduce 2 sub-classes
public class HourDisplay extends NumberDisplay {
// override getDisplayValue method the way you want
}
public class MinuteDisplay extends NumberDisplay {
// override getDisplayValue method the way you want
}
Then in ClockDisplay constructor
public ClockDisplay()
{
hours = new HourDisplay(24);
minutes = new MinuteDisplay(60);
updateDisplay();
}
Related
i donĀ“t know how to pass this test. I tried every solution i thought of and didnt find a solution on the internet neither. Its my exam to school.
So, I have this class MojeException.java:
public class MojeException extends RuntimeException {
/**
* Creates a new instance of <code>NewException</code> without detail
* message.
*/
public MojeException() throws AssertionError{
}
/**
* Constructs an instance of <code>NewException</code> with the specified
* detail message.
*
* #param msg the detail message.
*/
public MojeException(String msg) throws AssertionError {
super(msg);
}
}
And i have this test:
#Test(expected = MojeException.class)
public void testKonstruktor11() {
Rozmer rozmer = new Rozmer(0, 0, 0);
fail() ;
}
The error i got is "Unexpected exception, expected but was<java.lang.AssertionError>"
The main class is this, however i dont know if its not irelevant:
public class Rozmer {
public static final double DIMENZE_MAX = 100;
public static final double DIMENZE_MIN = .1;
private static final double TO_CM = 100.00;
private final long delka;
private final long sirka;
private final long vyska;
public Rozmer(double delka, double sirka, double vyska){
this.delka = (long)(delka * TO_CM);
this.sirka = (long) (sirka * TO_CM);
this.vyska = (long) (vyska * TO_CM);
}
public double getDelka() {
return delka/TO_CM;
}
public double getSirka() {
return sirka/TO_CM;
}
public double getVyska() {
return vyska/TO_CM;
}
#Override
public String toString() {
return "Rozmer{" + "delka= " + delka/TO_CM + "0,sirka= " + sirka/TO_CM + "0,vyska= " + vyska/TO_CM + "0}";
}
#Override
public int hashCode() {
int hash = 7;
hash = 89 * hash + (int) (this.delka ^ (this.delka >>> 32));
hash = 89 * hash + (int) (this.sirka ^ (this.sirka >>> 32));
hash = 89 * hash + (int) (this.vyska ^ (this.vyska >>> 32));
return hash;
}
#Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Rozmer other = (Rozmer) obj;
if (this.delka != other.delka) {
return false;
}
if (this.sirka != other.sirka) {
return false;
}
if (this.vyska != other.vyska) {
return false;
}
return true;
}
public static boolean kontrolaDimenze(double dimenze) {
return DIMENZE_MIN <= dimenze && dimenze<=DIMENZE_MAX;
}
Thanks for all your ideas and solutions :)
Basically, you are instructing JUnit that in your test you expect that a MojeException (which is a RuntimeException) will be eventually thrown, and that it's not done by fail(), which instead throws an AssertionException, so a total different one.
So, you have to throw that specific exception somewhere, otherwise your test will always fail.
The best point to do that is possibly in your constructor, because it's the only method you invoked in the test, so it looks like you are testing that specific constructor. Maybe after checking one of the input parameters, which doesn't match an expected value, you can throw your exception.
Here is just an example of how you can modify your Rozmer class constructor:
public Rozmer(double delka, double sirka, double vyska) throws MojeException {
if(delka == 0.0 || sirka == 0.0 || vyska == 0.0) {
throw new MojeException("Unsupported value for delka, sirka or vyska");
}
this.delka = (long)(delka * TO_CM);
this.sirka = (long) (sirka * TO_CM);
this.vyska = (long) (vyska * TO_CM);
}
Then remove the fail() from your test.
I've got 2 classes: Deadline and Task. Within the Task class, I want to create an extendDeadline method where it increases the deadline of a task. I tried the following:
public void extendDeadline(int extend){
deadline = deadline + extend;
}
but this didn't work because the type of deadline is Deadline. Can anyone help me?
Code for deadline class:
import java.text.DateFormat;
import java.util.Date;
public class Deadline {
// dates are stored as the number of seconds since 01/01/1970
private long deadline;
// we have a DateFormat to format the date as text
private DateFormat dateFormatter;
// define some constants...
public static final long SECOND = 1000;
public static final long MINUTE = SECOND*60;
public static final long HOUR = MINUTE * 60;
public static final long DAY = HOUR*24;
public static final long WEEK = DAY*7;
/**
* Construct a new deadline.
* By default, deadlines are one week ahead of the time now.
*/
public Deadline() {
// by default you get a week
deadline = now() + WEEK;
dateFormatter = DateFormat.getDateInstance();
}
/**
* #return the time now as a long
*/
private long now() {
return new Date().getTime();
}
/**
* Get the date of this deadline as a Date object.
* #return the date of this deadline as a Date object.
*/
private Date getDeadlineDate() {
return new Date(deadline);
}
/**
* Change the date of this deadline by a specified number of days.
* #param numDays the number of days to add to the deadline date (may be negative).
*/
public void setNewDeadline(int numDays) {
deadline = deadline + (DAY*numDays);
}
/**
* Find out if this deadline has passed.
* #return true when the time now is later than the deadline.
*/
public boolean hasPassed() {
return now() > deadline;
}
/**
* Return this deadline formatted as text to be printed.
* #return a string representation of this deadline.
*/
#Override
public String toString() {
return dateFormatter.format(getDeadlineDate());
}
#Override
public boolean equals(Object o) {
if (o instanceof Deadline) {
Deadline other = (Deadline) o;
if (this.toString().equals(other.toString())) {
return true;
} else {
return false;
}
} else {
return false;
}
}
}
code for task class:
public class Task {
private int id;
private String description;
private Deadline deadline;
private boolean done;
private int estimatedLength;
private int hoursWork;
/**
* Constructor for objects of class Task
*/
public Task(int id , String description, int estimatedLength) {
this.description = description;
this.id = id;
this.estimatedLength = estimatedLength;
deadline = new Deadline();
done = false;
hoursWork = hoursWork;
}
public int getId(){
return id;
}
public Deadline getDeadline(){
return deadline;
}
public String getDescription(){
return description;
}
public boolean isDone(){
return done;
}
public double getPercentageComplete(){ //make sure this is right
double result = 0;
double hoursWorkDouble = (double) hoursWork;
double estimatedLengthDouble = (double) estimatedLength;
result = (double) hoursWork / (double) estimatedLength * 100.0;
System.out.println("Percentage complete: "+result);
return result;
}
public boolean isLate(){
if(done = false) { // dont forget to put AND date greater deadline
return true;
} else{
return false;
}
}
public void setDone(){
this.done = true;
}
public void extendEstimate(int extendEstimate){
estimatedLength = estimatedLength + extendEstimate;
/*int extendEstimate= 0;
int estimatedLength= 0;
int result= extendEstimate + estimatedLength;
System.out.println(+result);
*/
}
public void recordHoursWorked(int recordHours){
hoursWork= hoursWork + recordHours;
}
public void extendDeadline(int extend){
}
}
It looks like you have an setNewDeadline(int numDays) method in your Deadline class. (this should probably be renamed to something like extendDeadlineByDays because you're not resetting the deadline, just extending it) It looks like what you actually want to do is call that method. So, in the Task class, the method should look like this:
public void extendDeadline(int extend){
deadline.setNewDeadline(extend);
}
In this case you're updating the deadline, rather than trying to reassign it. What you were trying to do was take a Deadline, and add an int to it, which is impossible.
I have a static ArrayList (masterLog) that is in my main driver class. The ArrayList contains Event objects, the Event object has an ArrayList (heats) as a global variable. the heat object as an ArrayList (racers) as a global variable. Now when I have the following line of code:
System.out.println(ChronoTimer1009System.getMasterLog().get(0).getHeats().get(getCurHeat()).getRacers().toString());
this returns [] even though the getRacers() IS NOT empty!
When I call this:
System.out.println(getHeats().get(getCurHeat()).getRacers());
this returns the proper filled array.
I think I need to sync the masterLog ArrayList but I am unsure how. I have tried syncing it the way other threads on Stack Exchange have recommended but no luck.
it seems like the static ArrayList masterLog is updated two levels deep but not three levels deep if that makes sense.
What am I doing wrong?
UPDATE:
Maybe this will help explain:
In my main (driver) class, I have a static ArrayList called masterLog. The purpose of this ArrayLIst is to store instances of Event objects for later data retrieval. Now, without making it too complicated, the Event class contains an ArrayList called heats, and the Heat class contains an ArrayList called racers. When I access the masterLog ArrayList at some point in the program (when the other ArrayLists are populated with data), say for example by the call "masterLog.getHeats().get(0).getRacers()", the masterLog does not find any data in the racers ArrayList. It does, however, find data in the heats ArrayList. In other words, the object instance that is stored in the masterLog only updates information to a depth of 2 (not 3 if that makes sense).
UPDATE:
Here is some code:
ChronoTimer1009System class (driver)
package main;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Stack;
public class ChronoTimer1009System {
private Event curEvent;
private static Channel[] channels = new Channel[8];
private boolean state;
private static Stack<Log> log;
private static ArrayList<Event> masterLog;
private static Printer p;
public static Time globalTime;
private int oldLogSize; //used only in this.export()
public ChronoTimer1009System() throws UserErrorException{
for(int i=0; i<channels.length; ++i){channels[i] = new Channel(SensorType.NONE);} // initialize channels
masterLog = new ArrayList<Event>(); //this holds references to each event
this.newEvent(EventType.IND);
this.state = false; //system is initally off
log = new Stack<Log>();
p = new Printer();
globalTime = null;
oldLogSize = 0;
}
public void newEvent(EventType e) throws UserErrorException {
switch(e){
case IND: this.curEvent = new IND();ChronoTimer1009System.masterLog.add(this.curEvent);break;
case PARIND: this.curEvent = new PARIND();ChronoTimer1009System.masterLog.add(this.curEvent);break;
case GRP: this.curEvent = new GRP();ChronoTimer1009System.masterLog.add(this.curEvent);break;
case PARGRP: this.curEvent = new PARGRP();ChronoTimer1009System.masterLog.add(this.curEvent);break;
}
for(Channel x : channels){if(x.getState()) x.toggleState();}
}
public void on() throws UserErrorException{
if(state) throw new IllegalStateException();
this.curEvent = new IND();
ChronoTimer1009System.globalTime = new Time(0);
state = true;
}
public void reset() throws UserErrorException{
if(state) state = false;
on();
}
public void exit(){
this.curEvent = null;
ChronoTimer1009System.globalTime = null;
if(!state) throw new IllegalStateException();
state = false;
}
public static Time searchElapsedByID(int idNum){
Time toReturn = null;
for(Log item : log){
if(item.getCompetitorNumber() == idNum){
toReturn = item.getElapsedTime(); break;
}
}
return toReturn;
}
/**
* #return the curEvent
*/
public Event getCurEvent() {
return curEvent;
}
/**
* #return the state
*/
public boolean isState() {
return state;
}
public static Channel getChan(int chan){
if(chan < 1 || chan > 8) throw new IllegalArgumentException("Argument is not in range");
return channels[chan-1];
}
public static void export(){
//*****FORMAT JSON*****
//before formating, a sort of the runners within each heat is needed to determine place.
String toJson = "{\"events\":[";
System.out.println(ChronoTimer1009System.getMasterLog().get(0).getHeats().get(0).getRacers().size());
//iterate through each event
for(int i = 0; i < ChronoTimer1009System.getMasterLog().size(); ++i){
//iterate through each heat of each event
toJson += "{\"name\":\"" + ChronoTimer1009System.getMasterLog().get(i).getType().toString() + "\",\"heats\":[";
for(int j = 0; j < ChronoTimer1009System.getMasterLog().get(i).getHeats().size(); ++j){
//iterate through each competitor in each heat
toJson += "{\"runners\":[";
System.out.println(ChronoTimer1009System.getMasterLog().get(i).getHeats().size());
ArrayList<Competitor> x = sortByPlace(ChronoTimer1009System.getMasterLog().get(i).getHeats().get(j).getRacers()); <----- on this line, the getRacers() part has a size of zero when it isn't empty.
for(int k = 0; k < x.size(); ++k){
//notice we are working with a sorted copy
//TODO make Competitor endTime the elapsed time
toJson += "{\"place\":\"" + String.valueOf(k+1) + "\",\"compNum\":\"" + x.get(k).getIdNum() + "\", \"elapsed\":\"" + x.get(k).getEndTime().toString() + "\"},";
}
toJson += "]},";
}
toJson += "]},";
}
toJson += "}";
System.out.println(toJson);
/*try{
URL site = new URL("http://7-dot-eastern-cosmos-92417.appspot.com/chronoserver");
HttpURLConnection conn = (HttpURLConnection) site.openConnection();
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
String data = "data=" + toJson;
out.writeBytes(data);
out.flush();
out.close();
System.out.println("Done sent to server");
new InputStreamReader(conn.getInputStream());
}
catch (Exception e)
{
e.printStackTrace();
}*/
}
private static ArrayList<Competitor> sortByPlace(ArrayList<Competitor> unsorted)
{
ArrayList<Competitor> whole = (ArrayList<Competitor>) unsorted.clone();
ArrayList<Competitor> left = new ArrayList<Competitor>();
ArrayList<Competitor> right = new ArrayList<Competitor>();
int center;
if(whole.size()==1)
return whole;
else
{
center = whole.size()/2;
// copy the left half of whole into the left.
for(int i=0; i<center; i++)
{
left.add(whole.get(i));
}
//copy the right half of whole into the new arraylist.
for(int i=center; i<whole.size(); i++)
{
right.add(whole.get(i));
}
// Sort the left and right halves of the arraylist.
left = sortByPlace(left);
right = sortByPlace(right);
// Merge the results back together.
merge(left,right,whole);
}
return whole;
}
private static void merge(ArrayList<Competitor> left, ArrayList<Competitor> right, ArrayList<Competitor> whole) {
int leftIndex = 0;
int rightIndex = 0;
int wholeIndex = 0;
// As long as neither the left nor the right arraylist has
// been used up, keep taking the smaller of left.get(leftIndex)
// or right.get(rightIndex) and adding it at both.get(bothIndex).
while (leftIndex < left.size() && rightIndex < right.size())
{
if ((left.get(leftIndex).getEndTime().compareTo(right.get(rightIndex)))<0)
{
whole.set(wholeIndex,left.get(leftIndex));
leftIndex++;
}
else
{
whole.set(wholeIndex, right.get(rightIndex));
rightIndex++;
}
wholeIndex++;
}
ArrayList<Competitor>rest;
int restIndex;
if (leftIndex >= left.size()) {
// The left arraylist has been use up...
rest = right;
restIndex = rightIndex;
}
else {
// The right arraylist has been used up...
rest = left;
restIndex = leftIndex;
}
// Copy the rest of whichever arraylist (left or right) was
// not used up.
for (int i=restIndex; i<rest.size(); i++) {
whole.set(wholeIndex, rest.get(i));
wholeIndex++;
}
}
/**
* #return the log
*/
public static Stack<Log> getLog() {
return log;
}
/**
* #return the masterLog
*/
public static ArrayList<Event> getMasterLog() {
return masterLog;
}
/**
* #return the p
*/
public static Printer getPrinter() {
return p;
}
}
Event Class:
package main;
import java.util.ArrayList;
public abstract class Event extends Display{
private ArrayList<Heat> heats;
private int curHeat; //private means only this class can modify, not the subclasses
private Competitor curComp;
private String name;
public Event(String name) throws UserErrorException{
this.name = name;
heats = new ArrayList<Heat>();
curHeat = -1;
curComp = null;
createRun();
}
/**
* This method will be used by all EventTypes and will not change
* regardless of the EventType.
* #throws UserErrorException
*/
public void createRun() throws UserErrorException{
heats.add(new Heat()); ++curHeat;
}
/**
* #return the heats
*/
public ArrayList<Heat> getHeats() {
return heats;
}
/**
* #return the name
*/
public String getName() {
return name;
}
/**
* #return the currentHeat
*/
public int getCurHeat() {
return curHeat;
}
/**
* #return the curComp
*/
public Competitor getCurComp() {
return curComp;
}
/**
* #param curComp the curComp to set
*/
public void setCurComp(Competitor curComp) {
this.curComp = curComp;
}
/* (non-Javadoc)
* #see Display#displayHeatNumber()
*/
#Override
public String displayHeatNumber() {
// TODO Auto-generated method stub
return "Heat: " + (curHeat+1);
}
/* (non-Javadoc)
* #see Display#displayFinished()
*/
#Override
public String displayFinished() {
String toReturn = "";
boolean noRunners = true;
for(Competitor x : getHeats().get(getCurHeat()).getRacers()){
if(x.getEndTime() != null){
toReturn += "\n" + x.getIdNum() + " " + (ChronoTimer1009System.searchElapsedByID(x.getIdNum()).equals(new Time(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE)) ? "DNF" : ChronoTimer1009System.searchElapsedByID(x.getIdNum()).toString() + " F");
noRunners = false;
}
}
if(noRunners){toReturn = "no runners have finished";}
return toReturn;
}
public abstract void endRun() throws UserErrorException;
public abstract void trigChan(int chan, boolean dnf) throws UserErrorException;
public abstract void cancel(int ln) throws UserErrorException;
public abstract EventType getType();
}
Heat class:
package main;
import java.util.ArrayList;
public class Heat {
private ArrayList<Competitor> racers;
//private ArrayList<Competitor> racers;
private int currentCompetitor;
/**
* Constructor
*/
public Heat(){
racers = new ArrayList<Competitor>();
//racers = new ArrayList<Competitor>();
currentCompetitor = 0;
}
/**
* Set selected racer as next on to start
* #param racer the racer to start next
*/
public void setNextCompetitor(Competitor x){
int pos = racers.indexOf(x);
if(pos == -1 || pos<currentCompetitor) throw new IllegalArgumentException("Competitor not in the race! Please add first");
for(int i = pos; i>currentCompetitor; --i){
racers.set(i, racers.get(i-1));
}
racers.set(currentCompetitor, x);
}
/**
* Take the selected runner (the next runner) out from the race
* #param racer the runner to be cleared
*/
public void clearNextCompetitor() throws UserErrorException {
if(racers.size()-(currentCompetitor)<1) throw new UserErrorException("No runners to clear!");
for(int i = currentCompetitor+1; i<racers.size(); ++i){
racers.set(i-1, racers.get(i));
}
racers.remove(racers.size()-1);
}
/**
* basically a remove method
* #param x
*/
public void remove(Competitor x){
int pos = racers.indexOf(x);
if(pos < 0) throw new IllegalArgumentException("runner does not exists");
racers.remove(pos);
}
/**
* Swaps two runners positions in line
*/
public void swap() throws UserErrorException{
int count = 0;
for(Competitor x : racers){
if(x.getStartTime() == null) ++count;
}
if(count > 1 && currentCompetitor + 1 <= racers.size()){
Competitor first = racers.get(currentCompetitor);
Competitor second = racers.get(currentCompetitor+1);
racers.set(currentCompetitor, second);
racers.set(currentCompetitor+1, first);
}
else{
throw new UserErrorException("Not enough competitors to swap");
}
}
/**
* Add a competitor to the end of the current line of competitors if any
* #param x the competitor to add
*/
public boolean addCompetitor(Competitor x) throws UserErrorException{
if(x.getIdNum() < 0 || x.getIdNum() > 99999) throw new UserErrorException("ID number out of range");
if(x.getRunNum() < 0) throw new IllegalArgumentException("Run Num Out of range");
boolean add = true;
for(Competitor i : racers){
if(i.getIdNum() == x.getIdNum()){
add = false;
break;
}
}
if(add){
racers.add(x);
}
return add;
}
/**
* Retrieve the next competitor if there is one
* #return the next competitor
*/
public Competitor getNextCompetitor() throws UserErrorException{
if(!hasNextCompetitor()) throw new UserErrorException("There are no more competitors!");
while(racers.get(currentCompetitor).isCompeting()){++currentCompetitor;}
return racers.get(currentCompetitor++);
}
/**
* used to fix the order of the queue after cancel is called
*/
public void fix(EventType x){
switch(x){
case IND:
--currentCompetitor;
break;
case GRP: case PARGRP: case PARIND:
for(int i = 0; i<racers.size(); ++i){
if(racers.get(i).getStartTime() == null){
currentCompetitor = i;
break;
}
}
break;
}
}
/**
* Is there another competitor to go?
* #return whether or not there is another competitor to go.
*/
public boolean hasNextCompetitor(){
return currentCompetitor < racers.size();
}
/**
* Return a 1D array view of the competitors
* #return
*/
public ArrayList<Competitor> getRacers(){
return racers;
}
}
in the export method of the ChronoTimer1009System class, I point out where the error is and what is happening
I am doing a UML and I am not quite sure how to do these mutator methods I am supposed to do this:
+turnOn(): void //sets on to true
+turnOff(): void //sets on to false
+channelUp(): void //increases channel by 1 if on, rolls to 1 after maximum
+channelDown(): void //decreases channel by 1 if on, rolls to maximum after 1
+volumeUp(): void //increases the volume by 1 if on and less than maximum
+volumeDown(): void //decreases volume by 1 if on and greater than 0
+toString( ): String //returns the current state(instance variable values)
my code right now: (keep in mind the mutator part isn't right)
public class TV {
private int volume;
private int channel;
private boolean on;
private int maxVolume;
private int maxChannel;
TV() {
volume = 1;
channel = 1;
on = false;
maxVolume = 1;
maxChannel = 1;
}
public int getChannel() {
return channel;
}
public int getVolume() {
return volume;
}
public boolean isOn() {
return on;
}
public int getMaxChannel() {
return maxChannel;
}
public int getMaxVolume() {
return maxVolume;
}
public void setChannel(int i) {
if (isOn() && i >= 1 && i <= maxChannel) channel = i;
}
public void setVolume(int i) {
if (isOn() && i >= 0 && i <= maxVolume) volume = i;
}
public void setMaxChannel(int i) {
maxChannel = i;
}
public void setMaxVolume(int i) {
maxVolume = i;
}
// + turnOn() * * This is where the mutator methods begin I need help here * *
// if (channel == maxChannel) channel = 1;
// else channel++;
//if (channel == 1) channel = max;
//else channel--;
// if (volume == maxVolume) volume = 1;
// else channel++;
//if (volume == 1) volume = max;
// else channel--;
public string toString() {
return "channel: " + channel + "\nvolume: " + volume +
"\non: " + on + "\nmax Channel: " + maxChannel +
"\nmax Volume: " + maxVolume;
}
}
Mutator generally means the same things as 'setter'
So in your above code, a 'getter' would be:
public int getMaxChannel() {
return maxChannel;
}
and a 'mutator' or 'setter' would be:
public void setMaxChannel(int maxChannel) {
this.maxChannel = maxChannel;
}
Sample methods:
public void turnOn() {
this.on = true;
}
public void channelUp() {
if (on) {
if (channel == maxChannel) {
channel = 1;
}
else {
channel++;
}
}
}
public void volumeDown() {
if (on && volume > 0) {
volume--;
}
}
Other methods follows the same logic.
Strings in java are objects, so your toString method signature should read public String toString().
We use setters and mutator as interchangeably.
A mutator method is used to set a value of a private field. It follows
a naming scheme prefixing the word "set" to the start of the method
name. These methods do not have a return type and accept a parameter
that is the same data type as their corresponding private field. The
parameter is then used to set the value of that private field.
Below are some examples of mutators or setters:
public void setMaxChannel(int i) {
maxChannel = i;
}
public void setChannel(int c) {
channel=c;
}
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Towards the bottom of this code I am getting an "unreachable statement" error. I have tried a few things, but cannot figure out why this is happening. The error is towards the bottom of the code (I have commented with // where the error is) Please help point me in the right direction I'm stumped!
/**
* Describes a certain model.
*
* #author (Joshua Baker)
* #version (1.0)
*/
public class Model
{
public static final int IN_PER_FOOT = 12;
public static final int BASE_RATE = 60;
public static final int TALL_INCHES = 67;
public static final double THIN_POUNDS = 140.0;
public static final int TALL_THIN_BONUS = 5;
public static final int TRAVEL_BONUS = 4;
public static final int SMOKER_DEDUCTION = 10;
private String firstName;
private String lastName;
private int heightInInches;
private double weightInPounds;
private boolean travel;
private boolean smokes;
private String newHeight;
private int perHourRate;
/**
* Default constructor
*/
public Model()
{
setFirstName ("");
setLastName ("");
setHeightInInches (0);
setWeightInPounds (0.0);
setTravel (false);
setSmokes (false);
}
/**
*
*/
public Model (String whatIsFirstName, String whatIsLastName, int whatIsHeight, double whatIsWeight,
boolean canTravel, boolean smoker)
{
setFirstName (whatIsFirstName);
setLastName (whatIsLastName);
setHeightInInches (whatIsHeight);
setWeightInPounds (whatIsWeight);
setTravel (canTravel);
setSmokes (smoker);
}
/**
*#return first name
*/
public String getFirstName()
{
return firstName;
}
/**
*#return last name
*/
public String getLastName()
{
return lastName;
}
/**
*#return height in inches
*/
public int getHeightInInches()
{
return heightInInches;
}
/**
*#return the converted height
*/
public String getNewHeight()
{
return newHeight;
}
/**
*#return weight in pounds
*/
public double getWeightInPounds()
{
return weightInPounds;
}
/**
*#return models pay per hour rate
*/
public int getPerHourRate()
{
return perHourRate;
}
/**
*#return travel
*/
public boolean getTravel()
{
return travel;
}
/**
*#return smokes
*/
public boolean getSmokes()
{
return smokes;
}
/**
* models first name
*/
public void setFirstName(String whatIsFirstName)
{
firstName = whatIsFirstName;
}
/**
* models last name
*/
public void setLastName(String whatIsLastName)
{
lastName = whatIsLastName;
}
/**
* models height in inches
*/
public void setHeightInInches(int whatIsHeight)
{
if (whatIsHeight >0){
heightInInches = whatIsHeight;
}
}
/**
* models weight in pounds
*/
public void setWeightInPounds(double whatIsWeight)
{
if (whatIsWeight >0){
weightInPounds = whatIsWeight;
}
}
/**
* can model travel
*/
public void setTravel(boolean canTravel)
{
travel = canTravel;
}
/**
* does model smoke
*/
public void setSmokes(boolean smoker)
{
smokes = smoker;
}
/**
* Converts to feet and inches
*/
public String convertheightToFeetInches()
{
int leftOver = (heightInInches %= IN_PER_FOOT);
int newHeight = (heightInInches % IN_PER_FOOT);
return newHeight + "Foot" + leftOver + "Inches";
}
/**
*
*/
public int calculatePayPerHour(){
if (heightInInches >= TALL_INCHES && (weightInPounds <= THIN_POUNDS)) {
perHourRate = BASE_RATE + TALL_THIN_BONUS;
return perHourRate;
}
else
{
perHourRate = BASE_RATE;
return perHourRate;
}
if (travel) { //unreachable statement
perHourRate = BASE_RATE + TRAVEL_BONUS;
return perHourRate;
}
else
{
perHourRate = BASE_RATE;
return perHourRate;
}
if (smokes) { //unreachable statement
perHourRate = BASE_RATE - SMOKER_DEDUCTION;
return perHourRate;
}
else {}
}
/**
* Displays details
*/
public void displayInfo()
{
System.out.print("Name : " + getFirstName() + " ");
System.out.println(getLastName());
System.out.println("Height : " + getNewHeight() + "inches");
System.out.println("Weight : " + getWeightInPounds() + "pounds");
System.out.print("Travel : " + getTravel() + " " );
System.out.print("Smokes : " + getSmokes() );
System.out.println("Hourly rate : " + getPerHourRate() );
}
}
That is because your program will return from either your first if block or the corresponding else block: -
if (heightInInches >= TALL_INCHES && (weightInPounds <= THIN_POUNDS)) {
perHourRate = BASE_RATE + TALL_THIN_BONUS;
return perHourRate;
}
else
{
perHourRate = BASE_RATE;
return perHourRate;
}
System.out.println("This will never get printed. And will show compiler error");
So, either of the two return statement will be executed. And hence any further code is unreachable.
Seems that you want to have cumulative sum of all the service rates to get the final perHourRate, for that, you can remove the return statement from each of the if-else block. And then for all the if-else block after the first one, instead of assigning the current price to perHourRate, do a compound addition +=.
Also, since you are working on the instance field - perHourRate, you don't need to return it at all. The changes you did on perHourRate can be obtained using getPerHourRate(). So, change the return type to void.
May you can try updating your calculatePayPerHour method to the one below:
public void calculatePayPerHour(){
if (heightInInches >= TALL_INCHES && (weightInPounds <= THIN_POUNDS)) {
perHourRate = BASE_RATE + TALL_THIN_BONUS; // Initial assignment
} else {
perHourRate = BASE_RATE; // Initial assignment
}
/** Rest of the assignment will be compound assignment, since you
are now updating the `perHourRate` **/
if (travel) {
perHourRate += TRAVEL_BONUS;
} // You don't need an else now. Since BASE_RATE is already added
if (smokes) {
perHourRate -= SMOKER_DEDUCTION;
}
}
Inside your calculatePayPerHour method you have an if/else and the statement will never be reached because in both cases you are returning a result:
if (heightInInches >= TALL_INCHES && (weightInPounds <= THIN_POUNDS)) {
perHourRate = BASE_RATE + TALL_THIN_BONUS;
return perHourRate; // you return in the if
}
else
{
perHourRate = BASE_RATE;
return perHourRate; // you return in the else
}
... the execution will never reach here
if (heightInInches >= TALL_INCHES && (weightInPounds <= THIN_POUNDS)) {
perHourRate = BASE_RATE + TALL_THIN_BONUS;
return perHourRate; // <------------
}
else
{
perHourRate = BASE_RATE;
return perHourRate; // <------------
}
Regardless of your height or weight, one of these 2 returns will trigger, so any statement after it will never be executed. This is identical to the below code.
if (heightInInches >= TALL_INCHES && (weightInPounds <= THIN_POUNDS)) {
perHourRate = BASE_RATE + TALL_THIN_BONUS;
}
else
{
perHourRate = BASE_RATE;
}
return perHourRate;
//unreachable
in the if/else above the code you have 2 return statements already...
This means it will never reach the code below this..
In your method public int calculatePayPerHour() the first if else statement is returning a value in any case (in both if and else block).
If you dry run the program you will see that the control flow will never reach statements below this block hence your exception.