Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How to make a java project with Main class and other class ?

+3 votes
asked Nov 9, 2022 by cw s (160 points)
edited Nov 9, 2022 by cw s

How to make a java project with Main class and other class ?

https://onlinegdb.com/PoJWi5QK7

Please, help me to complete that coding....

Thanks.

1 Answer

+1 vote
answered Nov 9, 2022 by Peter Minarik (84,720 points)

Every class has to go in its own file with the same name as the class and have a .java extension.

Here's what you need to do:

  1. Rename your file "dstesteq2" to "dstesteq2.java".
  2. Move your dstesteq1 class from "Main.java" to "dstesteq1.java"
Then all will work. :)
commented Nov 12, 2022 by cw s (160 points)
Thank you for your kind answer.
It's a good help to me for your advice.
commented Nov 12, 2022 by Peter Minarik (84,720 points)
I'm glad you're sorted.

Keep on coding! :)
commented Nov 15, 2022 by vaibhav (310 points)
class dstesteq1 {
    int Sub(int x, int y) {
        int z;
        z = x - y;
        return z;
    }
}
public class Main {
    static int Add(int x, int y) {
        int z;
        z = x + y;
        return z;
    }
    public static void main(String[] args) {
        int a, b, c;
       
        //-----------------------------
        a = Add(10, 4);
        System.out.println("a : " + a);
        //-----------------------------
        dstesteq1 eq1 = new dstesteq1();
        b = eq1.Sub(10, 4);
        System.out.println("b : " + b);
        //-----------------------------
        dstesteq1 eq2 = new dstesteq1();
        c = eq2.Sub(10, 4);
        System.out.println("c : " + c);
    }
}
commented Feb 2, 2023 by rajilauffer (100 points)
When I try that, my project still doesn't work. Any suggestions?
https://onlinegdb.com/NzBWzKHJ1
commented Feb 2, 2023 by Peter Minarik (84,720 points)
Look at your project. "TeamPerson" and "SoccerTeam" files do not have the ".java" extensions. Add them.

Also, packages do not work in OnlineGDB (I'm not a Java programmer, I'm not sure why). If you comment out "package ..." lines in both files and the import in the Main.java, your code will run.

Good luck!
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...