Reputation: 1
Right now I have both of these in separate java files, but I can't seem to figure out how to combine them both together. When I combine them and run it usually only one class will actually work and the other doesn't do anything. I've tried getting rid of one class and combining them into one large class but then I get the error error: method main(String[]) is already defined in class lapTimes public static void main (String[] args)
class GFG
{
static void ConvertSectoDay(int n)
{
n %= 3600;
int minutes = n / 60 ;
n %= 60;
int seconds = n;
System.out.println( minutes + " "
+ "minutes " + seconds + " "
+ "seconds ");
}
public static void main (String[] args)
{
int n = 200;
ConvertSectoDay(n);
}
}
public class lapTimes {
public void lap1(int lap) {
System.out.println("How many seconds for lap one: " + lap);
}
public void lap2(int lap) {
System.out.println("How many seconds for lap two: " + lap);
}
public void lap3(int lap) {
System.out.println("How many seconds for lap three: " + lap);
}
public void lap4(int lap) {
System.out.println("How many seconds for lap four: " + lap);
}
public static void main(String[] args) {
lapTimes run = new lapTimes();
run.lap1(34);
run.lap2(54);
run.lap3(43);
run.lap4(61);
}
}
Upvotes: 0
Views: 1002
Reputation: 719006
If you put all of the methods in your GFG
and lapTimes
classes into a single class, you obviously have two methods called main
with arguments String[] args
. That is not valid Java.
You cannot have two static
methods (or two instance methods) with the same and the same argument types ... in the same class.
Why? Because if you did, then Java would not know which method to use when something calls main
...
So, if you want your combined class to do the stuff done by both existing main
methods, then the obvious solution is to combine the main
methods into one; e.g.
public static void main(String[] args) {
int n = 200;
ConvertSectoDay(n);
NewClass run = new NewClass();
run.lap1(34);
run.lap2(54);
run.lap3(43);
run.lap4(61);
}
But this looks conceptually wrong. You now have a class that represents both a 4 lap race ... and ... whatever a GFG
is supposed to represent.
You should use class names that indicate the purpose of the class. (I have know idea what GFG
means!)
It only takes a few seconds to type a descriptive name. Choosing an appropriate name may take a bit longer ... but it will take far more time for someone else to understand your code if you use opaque names. You should be writing your code for other people to read. (The other people includes yourself, in 5 years time, when you have totally forgotten what the GFG
class is supposed to do.)
You should not combine unrelated abstractions and functions into a single class. It makes your code confusing.
You should follow Java style rules for identifiers:
Upvotes: 1