Jump to content

Help me, trying to get a head start on this project, and I though I was doing this right. I was wrong.

 

Can't even make a simple class called Range.

 

the instruction say make a constructor, 2 getter methods, a range method, and the get average method.

 

My logic is wrong on my range method, but idk why my getter methods aren't passing on the correct parameters. 

 

here's my code, also my professor provided me with a testrange program to test to see if my methods were working correctly.

 

 

/** * Write a description of class Range here. *  * @author (your name)  * @[member='versions'] (a version number or a date) */public class Range{    // instance variables - replace the example below with your own     private int low;     private int high;     /***************************************     * Constructor for objects of class Range     **************************************/    public Range(int plow, int phigh)    {        //instantiated both plow and phigh as low and high         //respectively.        plow = low;        phigh = high;    }        /*    public void setLow(int plow) {       plow = low;    }        public void getHigh(int phigh){        phigh = high;    }    */       /**************************     * returns the value of Low pased by plow     **************************/    public int getLow(){        return low;    }     /****************************     * returns the value of High passed by phigh     * in the constructor     ****************************/    public int getHigh(){        return high;    }     /******************************     * Checks is the valued passed by Plow     * and Phigh between the two values.     ******************************/     public boolean inRange(int value){        if(low < high){            return true;        }        if(high > low){            return true;        }        else{            return false;        }    }     /************************     * method devised to get the average      * of the instance variables passed on by pLow     * aka pillow (get it?) and pHigh.     *************************/    public double getAverage(){         double average = (low + high)*.5;         return average;    }} 

TestRange.txt

Link to comment
https://linustechtips.com/topic/229304-makin-some-classes-and-constructor/
Share on other sites

Link to post
Share on other sites

Your not using the value parameter anywhere in the inRange function so I expect it's not working as intended.

public boolean inRange(int value){    if(low < high){        return true;    }    if(high > low){        return true;    }    else{        return false;    }}

Can't remember if Java has this kind of comparison

low < value < high

If not, you can just use the && operator

low < value && value < high
Link to post
Share on other sites

Thanks alot everone! It's really helpful to me when I'm learning how to do the basics in java, and midterms are net week so I'm trying to get everything down and really kill the test.

 

Getting a headstart on my project so that if I don't understand anything I can have lots of time to figure things out.

 

Sorr or no usin code tags I'm a noob here on this part of the forum.

Link to post
Share on other sites

 

Exactly what fizzlesticks said, your code is the wrong way around and also your inRange() method never compares against the inputted parameter, it should be like this:

public boolean inRange(int value){    //Return true if value is same as plow or phigh or is in between the two    if(plow <= value && phigh >= value){        return(true);    }    //Otherwise return false    return(false);}

 

Could be better, what you're essentially saying is:

if(condition is true)  return trueelse // if condition is false  return false

This is unnecessarily verbose, try this

return (condition is true)

I.e.

if(value == 3)  return trueelse  return false

Is much worse than

return value == 3
Link to post
Share on other sites

That's shorter but dirtier, the code is much more clearer in an if-else form. It's not just you going to be working in a business so others need to be able to understand your code straight away.

 

It isn't clearer at all in an if else statement. That's subjective of course, but in both my undergraduate course and at my current c# dev job and also on any post you'd care to look at on stack overflow, the overwhelming majority prefer without the if-else nonsense.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×