Results 1 to 12 of 12

Thread: Java beginner

  1. #1
    ducky29 is offline Member
    Join Date
    Nov 2014
    Posts
    5
    Rep Power
    0

    Default Java beginner

    I just started getting some lessions at school about java.My teacher gave me some homeworks but im having troubles solving them.I don't want somebody to solve them.I just want to learn what im doing wrong.
    This is how far I got into it.
    Sum of odd numbers from 1 to n.
    Java Code:
    public class Sum{
    public Sum(){
    }
    int sumodd (int i;int s){
    i=0;
    s=0;
    for(i=0;i<n;i++)
    {if i℅2!0
    s=s+i}
    return s;
    }
    }
    Ps:I should be using a method so I can use it later.
    Sorry if Im breaking any rules.Thanks a lot.
    Last edited by ducky29; 11-23-2014 at 01:44 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Java beginner

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

    im having troubles solving them.
    Please explain what problems you are having. If you are getting errors, copy the full text of the error messages and paste it here.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Java beginner

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

    im having troubles solving them.
    Please explain what problems you are having. If you are getting errors, copy the full text of the error messages and paste it here.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    ducky29 is offline Member
    Join Date
    Nov 2014
    Posts
    5
    Rep Power
    0

    Default Re: Java beginner

    After I complied it,I got some syntax errors , then after fixing em it didn't show me any results.I used a website to compile it becouse I haven't setup the environment in my pc yet.
    - Code tags added in the first post.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    17

    Default Re: Java beginner

    after fixing em it didn't show me any results.
    Your original code doesn't contain any print statements, so you can't expect any results to be displayed.

    If the code is still not doing what you want, post the updated code so we all know what you're working on currently. And post any compiler messages that you get fully, as they also help a lot in explaining a problem.

  6. #6
    ducky29 is offline Member
    Join Date
    Nov 2014
    Posts
    5
    Rep Power
    0

    Default Re: Java beginner

    I guess I fucked up even worst
    Java Code:
    public class Sum {
        public Sum(){
        }
            public int sumodd(int i,int s);{
            
                i=0;
                s=0;
             for(i=0;i<n;i++)
             { 
                 if (i%2!=0);
             s=s+i;
            }
            System.out.println("s");
         }
    }
    Errors
    Java Code:
    Sum.java:4: error: missing method body, or declare abstract
            public int sumodd(int i,int s);{
                       ^
    Sum.java:6: error: cannot find symbol
                i=0;
                ^
      symbol:   variable i
      location: class Sum
    Sum.java:7: error: cannot find symbol
                s=0;
                ^
      symbol:   variable s
      location: class Sum
    Sum.java:8: error: cannot find symbol
             for(i=0;i<n;i++)
                 ^
      symbol:   variable i
      location: class Sum
    Sum.java:8: error: cannot find symbol
             for(i=0;i<n;i++)
                     ^
      symbol:   variable i
      location: class Sum
    Sum.java:8: error: cannot find symbol
             for(i=0;i<n;i++)
                       ^
      symbol:   variable n
      location: class Sum
    Sum.java:8: error: cannot find symbol
             for(i=0;i<n;i++)
                         ^
      symbol:   variable i
      location: class Sum
    Sum.java:10: error: cannot find symbol
                 if (i%2!=0);
                     ^
      symbol:   variable i
      location: class Sum
    Sum.java:11: error: cannot find symbol
             s=s+i;
             ^
      symbol:   variable s
      location: class Sum
    Sum.java:11: error: cannot find symbol
             s=s+i;
               ^
      symbol:   variable s
      location: class Sum
    Sum.java:11: error: cannot find symbol
             s=s+i;
                 ^
      symbol:   variable i
      location: class Sum
    11 errors

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    17

    Default Re: Java beginner

    Don't panic about getting lots of messages. Often when one thing offends the compiler it spits out lots of messages which simply go away when the primary cause is fixed.

    Sum.java:4: error: missing method body, or declare abstract
    public int sumodd(int i,int s);{
    The problem here is that semicolon.

    Java Code:
    public int someMethod(/*arguments*/) {
        // code goes here
    }
    Notice how there is no ; in the opening line of the method definition. That's because ; is used to end statements (roughly "instructions") in Java. But that first line isn't a statement - rather it's the start of a method declaration and definition which requires the rest (between the curly braces) before its finished.
    Last edited by pbrockway2; 11-23-2014 at 02:51 AM.

  8. #8
    ducky29 is offline Member
    Join Date
    Nov 2014
    Posts
    5
    Rep Power
    0

    Default Re: Java beginner

    Oh ok Ill keep that in mind.
    Got till here
    Java Code:
    public class Sum {
        public Sum(){
        }
            public int sumodd(int i,int s,int n){
             i=0;
             s=0;
             for(i=0;i<n;i++)
             { 
                 if (i%2!=0);
             s=s+i;
            }
            System.out.println("s");
         return s;}
    }
    Error log
    Java Code:
    Error: Main method not found in class Sum, please define the main method as:
       public static void main(String[] args)
    Please can I get a little more help?
    What does the second line do
    Java Code:
    "public Sum()"
    ?
    How do I call a method to use it later in the code?
    Last edited by ducky29; 11-23-2014 at 03:36 AM.

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    17

    Default Re: Java beginner

    Are you using a textbook or notes? The line you asked about (and the following curly braces) is a constructor for the Sum class. You should find out about what constructors do - not much in this case, because thereis no code inside the associated curly braces.

    The error message is saying there is no main method. Again this is something to look up, but every Java program has a main method that is declared as shown in the compler message. It is this method that runs when you run the program. In other words the main() method defines "what the program should do".

    In your case what you want the program to do is the case you have written as part of sumodd().

    ---

    Do read about constructors and a program's main() method.

    One approach here is to remove the constructor, and rename the sumodd() method as main(). Notice that the compiler message tells you how main() should be declared. Fix any remaining compiler messages, and see what happens.

    ---

    Here's a really simple Java program along the lines of what I'm suggesting (no constructor and a single main() method).

    Java Code:
    public class Greet {
        public static void main(String[] args) {
            System.out.println("Greeting from Java!");
        }
    }
    Pretty much what your program should look like - although your class will be called someting else, and your main() method won't contain the single line like this one but, rather, the code you have already written.

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    17

    Default Re: Java beginner

    How do I call a method if I wanna use it later in the code?
    Did you add this question? In any case, I didn't see it.

    Here is a more elaborate version of the Greet template. This one does have a constructor and it is used precisely to allow a method to be called later.

    Java Code:
    public class Greet {
        public Greet() {
        }
    
        public void doGreeting() {
            System.out.println("Greeting from Java!");
        }
    
        public static void main(String[] args) {
                // first create an instance of Greet.
            Greet app = new Greet();
                // then use the instance to call a method that
                // was defined earlier
            app.doGreeting();
        }
    }
    Your example would replace doGreeting() with sumodd() but would otherwise be similar.

    It is a more realistic example of how a program is structured - and it does attempt to answer your question! - but your program is so simple I would tend to prefer version I posted. The two version are exactly identical in effect.

  11. #11
    ducky29 is offline Member
    Join Date
    Nov 2014
    Posts
    5
    Rep Power
    0

    Default Re: Java beginner

    Sorry for the late reply but it was 3 am and I couldnt keep up any longer so I had to get some sleep.
    Im using notes.
    Thanks a lot for all the instructions
    With your help I somehow managed to fix it without using a constructor.
    Code without a constructor
    Java Code:
    public class Sum {
        public class sum {
        }
        public static void main(String [] args){
            int i=0;int s=0; int n;
             s=0;
             n=10;
             
             for(i=0;i<n;i++)
             {
                 if (i%2!=0)
                 s=s+i;
            }
            System.out.println("Sum is " +s);
         }
     }
    But it only works If I inicialize the n first ( give it a value ex 10)
    What is the command which ask for an input from my keyboard?(equivalent to scanf command in c language)
    I didn't add that question but its ok.
    The reason that the teacher gave us this exercise is exactly "Calling the method later in the exercise"
    Im trying to do the sum of odd numbers from 1 to n
    Then I should do product of even numbers from 1 to n
    At last I should do the difference between those 2, so I guess calling the method would solve my problem.
    Code with constructor added
    public class Sum {
    public sumconstructor()
    {
    }
    public static void main(String [] args){
    int i=0;int s=0; int n;
    n=10;

    for(i=0;i<n;i++)
    {
    if (i%2!=0)
    s=s+i;
    }
    System.out.println("Sum is " +s);
    }
    }

    Errors
    Java Code:
    Sum.java:2: error: invalid method declaration; return type required
        public sumconstructor()
               ^
    1 error
    Thanks again for being so patient.

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    17

    Default Re: Java beginner

    First, forget the constructor. I think I overstated things by suggesting the constructor was needed to allow methods to be called later. This exercise can be done with so-called "static" methods: although they're not really how larger programs might be written they will do here.

    ---

    The way you have it the variable n is said to be "hard coded". To get a value from the user and assign it to n you can use the nextInt() method of the Scanner class.

    Java Code:
    class Eg {
        import java.util.Scanner; // the class has to be imported
    
        public static void main(String[] args) {
                // declare an initialise a scanner that reads the keyboard
            Scanner in = new Scanner(System.in);
            System.out.println("Enter an integer:");
            int n = in.nextInt();
            System.out.println("You entered " + n + "!");
        }   
    }
    ---

    The general approach of your code will be to define the methods (you already have written one of them) and then use the main() method to tie everything together.

    Java Code:
    public class Sum {
        import java.util.Scanner; 
    
            /** Returns the sum of the odd numbers 1->n */
        static int sumodd(int n) {
            // you already have the code for this
            // which only needs to be changed a little to compile
            // Remember to return s (or whatever you are calling the sum)
        }
    
            /** Returns the sum of the even numbers 1->n */
        static int sumeven(int n) {
            // rather similar
        }
    
        public static void main(String[] args) {
            // Create a scanner and get n
    
            int oddSum = sumodd(n); // this is how you use the method you defined earlier
            // print a message saying what the oddSum is
    
            // same for even
    
            //print a message saying what evenSum-oddSum is
        }
    
    }
    The comments written above the two methods are deliberate and are very helpful in describing exactly what the methods do to you (when you read it later) and us.

    In general pass as little stuff around as possible. What I mean is the methods only really need to be told what value to use as n, so that should be the only argument. If you have written a good description comment (and it is usually written first!) it will more or less tell you what the arguments should be.
    Last edited by pbrockway2; 11-23-2014 at 07:53 PM.

Similar Threads

  1. Java beginner Help
    By Animuslo in forum New To Java
    Replies: 2
    Last Post: 06-16-2014, 10:44 AM
  2. Java beginner
    By Bhumika in forum NetBeans
    Replies: 3
    Last Post: 02-11-2014, 10:53 AM
  3. Java for a beginner
    By ukenpachi in forum New To Java
    Replies: 1
    Last Post: 10-19-2013, 03:51 AM
  4. Java beginner
    By annaharris in forum Introductions
    Replies: 2
    Last Post: 01-07-2013, 03:12 PM
  5. I am java beginner please help me
    By shki_80 in forum Introductions
    Replies: 1
    Last Post: 04-20-2012, 02:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •