Unreachable error [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am trying to have the code print out the highest of the three grades; however, the if/else statement in the highest method is finding an error in return d. I have tried putting if(d>b && d>c) return d, and also else return d. But both times the program says it is unreachable. Can someone explain what I'm doing wrong? Thank you!
import java.util.Scanner;
public class Methods2 {
public static double average(double a){
double ave= a/3.0;
return ave;
}
public static double highest(double b, double c, double d){
if(b>c && b>d)
return b;
if(c>b && c>d);
return c;
return d;//unreachable code
}
public static void main(String[] args){
Scanner kb= new Scanner(System.in);
System.out.println("Enter your name.");
String name = kb.nextLine();
System.out.println("Enter your three grades.");
double b= kb.nextDouble();
double c= kb.nextDouble();
double d= kb.nextDouble();
double av= average(b+c+d);
System.out.println(av);
double high= highest(b,c,d);
System.out.println(high);
}
}

Yes. Because semicolon makes it an empty if body.
if(c>b && c>d);
return c;
should be
if(c>b && c>d)
return c;
or (the arguably better)
if(c>b && c>d) {
return c;
}
You could also use Math.max(double, double) to make this a one line method like
public static double highest(double b, double c, double d) {
return Math.max(d, Math.max(b, c));
}

because of the semicolon in the second if
if(c>b && c>d);
That terminates the conditional statement right there and return c; becomes a statement that will get executed regardless of condition, remove it

Related

interest rate java confusion with private static doubles [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
alright so i have already made a working compound interest rate program. but now for this program i have to re write the program using more methods. using
private static double FVCALC(..)
and
private static double validate(........)
i dont quite understand how i need to do this. the current code i have only lets me input the 3 values of interest rate and it stops. is it because of the private mehtods? im not sure what to do and i have searched for 3 days now.
bottom line is. my code is not working the way i want it to.
public class interest_rate
{
static Scanner input = new Scanner(System.in);
public static void main(String[] args)
{
double i;
double n;
double FVCalc;
double PV;
System.out.print("please enter value for n (years): ");
n = input.nextInt();
System.out.print("please enter interest rate: ");
i=input.nextDouble();
System.out.print("please enter Present Value: ");
PV = input.nextDouble();
}
private static double validate (double upLimit, double lowLimit, double PV)
{
upLimit=100000.00;
lowLimit=0.00;
while(PV>upLimit|| PV<lowLimit)
{
System.out.print("please enter value between "+upLimit+" and "+lowLimit);
System.out.print("please enter PV");
PV=input.nextDouble();
}
return PV;
}
private static double FVCalc(double PV, double i, double n, double FV)
{
FV = PV*Math.pow(1+(i/100), n);
return(FV);
}
}
First, you need to call the methods in main.
Secondly, you cant pass in PV, then reassign it and expect the value to update.
For example..
private static double validate (double upLimit, double lowLimit, double PV)
You need to call this in main like so
PV = 0.0; // some double value
double validated = validate(0,100000); // return the value, don't pass it in
And remove these lines in that method because overriding parameters is typically bad.
upLimit=100000.00;
lowLimit=0.00;
Next, add a field for your values that you want to use throughout the class.
public class InterestRate
{
static double pv , fvCalc;
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
}
And remove these lines in main and use those two class variables instead
double FVCalc;
double PV;
Additionally, I don't see a reason to store fvCalc as a variable. You can just calculate it
private static double fvCalc(double pV, double i, double n)
{
return pV*Math.pow(1+(i/100), n);
}
i believe i have figured it out. and i made it pretty. excuse the notes. i wanted to keep them there to see what i did wrong. thanks all.
public class interest_rate
{
static double pV , fvCalc, i, n;
static Scanner input = new Scanner(System.in);
public static void main(String[] args)
{
double validated;
double calculated;
double i;
double n;
//double fV;
//double pV1;
System.out.print("please enter value for n (years): ");
n = input.nextInt();
System.out.print("please enter interest rate: ");
i=input.nextDouble();
validated = validate(0,100000);
System.out.println("pV is validated and equal to: "+validated);
calculated= fvCalc(validated,i,n);
System.out.println("your calulation for interest is: "+calculated);
}
private static double validate(double upLimit, double lowLimit) {
double pV;
System.out.print("please enter pV");
pV=input.nextDouble();
while(pV<upLimit|| pV>lowLimit)
{
System.out.print("please enter value between "+upLimit+" and "+lowLimit);
System.out.println("please enter pV");
pV=input.nextDouble();
}
return pV;
}
private static double fvCalc(double pV, double i, double n)
{
double fV;
fV=pV*Math.pow(1+(i/100), n);
return fV;
}
}

Is my implementation of a Rational number using classes acceptable? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to finish my rational class for java and everywhere I have looked to make it finished doesn't have it near the same. I know I could use others programs that where made but the ones I have seen don't have it for where you put the input in when you run the program. This is the code I have so far
import java.util.Scanner;
public class Lab09ast
{
private static int num, den; // numerator and denominator of the rational number
public static void main (String[] args)
{
enterData();
Rational r = new Rational(num,den);
r.displayData();
}
public static void enterData()
{
Scanner input = new Scanner(System.in);
System.out.print("\nEnter the numerator ----> ");
num = input.nextInt();
System.out.print("\nEnter the denominator --> ");
den = input.nextInt();
}
}
class Rational
{
public void displayData()
{
System.out.println();
System.out.println(getNum() + "/" + getDen() + " equals " + getDecimal());
System.out.println();
}
private void getGCF(int n1,int n2)
{
int rem = 0;
do
{
rem = n1 % n2;
if (rem == 0)
gcf = n2;
else
{
n1 = n2;
n2 = rem;
}
}
while (rem != 0);
}
}
Member variables num and den (numerator and denominator) are in class Lab09ast. These should be in class Rational. Do you understand the concepts of classes and objects?
It's logical that a Rational object, which you make from the class Rational, has member variables for the numerator and denominator.
Also, those member variables must not be static. See Understanding Class Members to learn what static means and why it is not appropriate for these member variables.
The methods getNum() and getDen() should return the values of the num and den member variables, and should also be in class Rational.
Class Rational should also have a constructor that takes two arguments, for the numerator and denominator. You're already calling that constructor in the main method of class Lab09ast, but it's not in your class Rational yet.

What is wrong with this recursion method? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
import java.util.Scanner;
public class Recursion
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter n to start: ");
int n = in.nextInt();
System.out.println("Sum of numbers from 1 to " + n + ": " +new Recursion().sumUpTo(n));
}
}
/**
* Computes the sum of a range of numbers
*
* #param n an integer
* #return the sum of n range
*/
public int sumUpTo(int num){
if (num == 0)
{
return 0;
}
else{
return (num + sumUpTo(num-1))
}
}
This should be a very simple method, I know, but I can't seem to get it to compile. I keep getting "class, interface, or enum expected" on the public int sumUpTo(int num) line. This is the method that performs the actual computations. Any help would be greatly appreciated.
A couple things :
Your method is declared outside of the class
Missing semicolon on the line with your recursive call
You should be new to Object Oriented Programming. In Java a function(method) must be inside(belong) to a class. You either put public int sumUpTo(int num) inside public class Recursion or you can create another class and put it in there. But remember there should be only one public class inside a file.
In Java, everything must be inside of a class. Simply move your method to within the curly braces of the class and it should work.
The error class, interface, or enum expected is quite self-explained! It encountered code that wasn't inside of a class, inteface, or enum, and it wasn't expecting that, because that shouldn't happen!
In case you need to see what I mean:
public class Recursion {
public static void main (String[] args) {
// your code here...
}
// Where your method should have gone.
}
Also, welcome to Stack Overflow. Please remember to accept an answer if it answers your question.
Move your sumUpTo method inside the class.
import java.util.Scanner;
public class Recursion
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter n to start: ");
int n = in.nextInt();
System.out.println("Sum of numbers from 1 to " + n + ": " +new Recursion().sumUpTo(n));
}
/**
* Computes the sum of a range of numbers
*
* #param n an integer
* #return the sum of n range
*/
public int sumUpTo(int num){
if (num == 0)
{
return 0;
}
else{
return (num + sumUpTo(num-1));
}
}
}

syntax error on else, delete this token error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
My if, else statement is giving me an error that is telling me to delete the word else. What should I do?
import java.util.Scanner;
public class Test5 {
public static void main(String[] args) {
double GradeAverage;
{
System.out.print("Welcome! Please enter in your mid-year grade average.");
}
Scanner kbdln = new Scanner(System.in);
{
GradeAverage = kbdln.nextDouble();
{
if (GradeAverage > 60 && GradeAverage <= 100);
{
System.out.print("You're passing your class!");
}
else if (GradeAverage > 0 && GradeAverage <= 60)
{System.out.print("Hook up with a smart classmate and STUDY!");}
}
}
}
}
There should be no semi-colon after the if statements.
The if statement should be as follows:
if(GradeAverage > 60 && GradeAverage <= 100)
First, I would get rid of all those { } you are using. It is way too much. Although it is correct to use them to declare a local namespace and absolutely necessary to use them with your if-else statements. But it is better not to use local namespaces if you are not absolutely sure what you are doing.
The second thing is you have a ; after your if statement. Which will be interpreted as an ; = empty statement. So your follow up else is completely alone – and that's why your IDE tells you to remove the else.
Here is a version of your code a little cleaned up :)
public class Test5 {
public static void main(String[] args) {
double GradeAverage;
System.out.print("Welcome! Please enter in your mid-year grade average.");
Scanner kbdln = new Scanner(System.in);
GradeAverage = kbdln.nextDouble();
if (GradeAverage > 60 && GradeAverage <= 100) {
System.out.print("You're passing your class!");
} else if (GradeAverage > 0 && GradeAverage <= 60) {
System.out.print("Hook up with a smart classmate and STUDY!");
}
}
}

Polymorphism Call [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Please Help me in my code. This is polymorphism! I can't call all my class as you can see below, only the addition shows the output. I need some explanation too because I really need to know how this happened.
This is my code:
import java.util.Scanner;
class Variation{
public void reality(){
}
public int reality(int n1,int n2,int n3,int n4,int n5){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter 5 numbers for addition: ");
n1=keyboard.nextInt();
n2=keyboard.nextInt();
n3=keyboard.nextInt();
n4=keyboard.nextInt();
n5=keyboard.nextInt();
int sum=n1+n2+n3+n4+n5;
System.out.println("The answer is: "+sum);
return sum;
}
}
class multi extends Variation{
public double reality(double n1,double n2, double n3, double n4, double n5){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter 5 numbers for multiplication: ");
n1=keyboard.nextInt();
n2=keyboard.nextInt();
n3=keyboard.nextInt();
n4=keyboard.nextInt();
n5=keyboard.nextInt();
double prod=n1*n2*n3*n4*n5;
System.out.println("The answer is: "+prod);
return prod;
}
}
class sub extends Variation{
public int reality(int n1,int n2, int n3, int n4, int n5){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter 5 numbers for Subtraction: ");
n1=keyboard.nextInt();
n2=keyboard.nextInt();
n3=keyboard.nextInt();
n4=keyboard.nextInt();
n5=keyboard.nextInt();
int diff=n1-n2-n3-n4-n5;
System.out.println("The answer is: "+diff);
return diff;
}
}
class Calling
{
public static void main(String[] args) {
int n1=0,n2=0,n3=0,n4=0,n5=0;
Variation variaTions = new Variation();
System.out.println(variaTions.reality(n1, n2, n3, n4, n5));
}
}
You only ever create an instance of Variation. You never create an instance of any of the derived classes. So of course they won't be called.

Categories

Resources