Reputation: 1449
I'm very new to Java. Here is my code:
public class funk {
int largest(int a,int b,int c)
{
if(a>b)
{
if(a>c)
{
return a;
}
else if(b>c)
{
return b;
}
else
{
return c;
}
}
}
public class firstprog {
public static void main(String args[]) {
int a=7;
int b=8;
int c=9;
funk punk=new funk();
System.out.println(punk.largest(a,b,c));
}
}
The error that Eclipse gives me is The public type funk must be defined in its own file.
Why is this so?
Upvotes: 2
Views: 30140
Reputation: 1
During compilation time, your compiler cannot find the parallel else condition of (a>b) that is => if(a<=b)
what it will return if your input matches this block? So you should provide this
Upvotes: 0
Reputation: 854
This Eclipse error has already been answered by other users. Anyway here is an alternative to find the largest number of type int
Alternative - Find the largest integer, using stream():
public static int largest(int a, int b, int c) {
return Arrays.asList(a, b, c).stream().max(Integer::compareTo).get().intValue();
}
Summary of the error:
Here is the Eclipse error:
The method must return a result of type int
Reson for error:
Not all condition are taken care of in method largest. "Outside" condition 'if(a > b)' a "return an int" is needed.
Alernative - 'if statements' using Clean code's concept 'early return'/'eager return':
public static int largest(int a, int b, int c) {
if(a > b && a > c) {
return a;
}
if(b > c) {
return b;
}
return c;
}
Upvotes: 0
Reputation: 28962
In Java public classes must be in separate files with name the same as class name.
So put your funk class in funk.java file and firstprog class in firstprog.java file
Or delete public
in funk class, then this class will have default package modifier.
Upvotes: 1
Reputation: 3742
Yes the problem is that you can only have one public class per file and this file should have the same name than the class. You can just remove the public in front of the definition of the first class. A better way to do would be to make it a static method of the main class.
To solve you second problem you can do this:
public class firstprog {
public static int largest(int a,int b,int c)
{
if(a>b)
{
if(a>c)
return a;
else
if(b>c)
return b;
else
return c;
}
else
{
if(b>c)
return b;
else
return c;
}
}
public static void main(String args[]) {
int a=19;
int b=2;
int c=1;
System.out.println(largest(a,b,c));
}
}
Upvotes: 3
Reputation: 1
public class firstprog {
public static int largest(int a,int b,int c)
{
if(a>b)
{
if(a>c)
{
return a;
}
else if(b>c)
{
return b;
}
else
{
return c;
}
}
return 0;
}
public static void main(String args[]) {
int a=7;
int b=8;
int c=9;
System.out.println(largest(a,b,c));
}
}
Note: you have to add return statement, because a is not greater then b, so it it not going inside of if block.. In your larget(...) it is expecting return statement as int.. so you have to add one more return statement. then it work ... Cheers ...!!!
Upvotes: 0
Reputation: 12543
Only one public top level class (a top level class is a class not contained in another class) is allowed per .java file.
Define funk
in funk.java with no other top level classes.
Put any other top level classes in their own files where the file name matches the class name.
Regarding your second question, if you declare a method to return a particular type, like int
, then all paths through that method must result in a return
statement returning a valid value. In your example, the if
statement might not be entered!
What happens if b == a
or a < b
?
Upvotes: 2
Reputation: 61643
Why is this so?
If you mean "why do I get this error", it's because you have not put the class in its own file, and you must. If you mean "why must I put the class in its own file", it's because Java says so: one public
type (class or interface) per file.
Specifically, "its own" file must have the same name: the public class funk
goes in funk.java
, and the public class firstprog
goes in firstprog.java
.
Upvotes: 0
Reputation: 613
For your second error, the logic seems to be off a little... there is no return statement in the case where b > a.
Upvotes: 0
Reputation: 12019
Are you trying to put multiple classes into one file? Each class should get its own .java file with the appropriate name. Also make the first letter of your class upper case, as this is the naming convention.
As an aside, your function will only work if a is larger than c. You've missed out on some cases.
EDIT: you can have nested classes, but I think you might want to stay away from stuff like that for now.
Upvotes: 1