1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Help me with this java code.

Discussion in 'Software Development' started by RedWizard, Feb 13, 2007.

Thread Status:
Not open for further replies.
Advertisement
  1. RedWizard

    RedWizard Thread Starter

    Joined:
    Jan 14, 2007
    Messages:
    48
    Hey. Recently I have been trying to teach myself java. I have run into some problems and was curious if anyone could help me with this code?

    what i'm trying to write is a class with constructor, accessor, and calculation method that will discover the average and display it. Heres my code; and it doesn't work.

    package twonumbers;


    public class Main {
    private int firstNumber;
    private int secondNumber;
    private double average;
    public Main() {
    firstNumber = 10;
    secondNumber = 15;
    }

    public void showValues()
    {
    System.out.println("average = " + "10 + 5 / 2");
    }

    public void calculateArea()
    {
    System.out.println((firstNumber + secondNumber) / 2);
    }
    public static void main(String[] args) {
    System.out.println((firstNumber + secondNumber) / 2);
    }

    }

    As you can see; my logic is flawed and my code sucks. But not bad for my second day of java.

    I was wondering if anyone could help me understand what i'm doing wrong?

    Thanks
     
  2. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    Hi RedWizard,

    I brought a little improvement to your coding :

    Code:
    [SIZE=2]
    public class Main {
        private static int firstNumber;
        private static int secondNumber;
        private static double average;
    
        public Main() {                               [COLOR=Blue]// This is the constructor[/COLOR]
            this.firstNumber = 10;                    [COLOR=Blue]// it is generally used to [/COLOR]
            this.secondNumber = 15;                   [COLOR=Blue]// initialize varaiables[/COLOR]
        }
        
        public static void main(String[] args) {      [COLOR=Blue]// This is where your program is started[/COLOR] 
            Main test = new Main();                   [COLOR=Blue]// Generate a new intance of the constructor[/COLOR]
                                                      [COLOR=Blue]// to initialize the variables[/COLOR]
            calculateArea();                          
            showValues();
        }
        
        private static void calculateArea() {
            average = (firstNumber + secondNumber) / 2; 
        }
        
        private static void showValues() {
            System.out.println("Average of " + firstNumber + " and " + secondNumber + " = " + average);
        }
        
    }
    
    [COLOR=Blue]1° You will notice that the variables are declared as [B]static[/B] :
    in this example, it is mandatory because they are used by static methods [B]calculateArea(), showValues()[/B] which
    are themselves invoked by [B]main()[/B]. And [B]main()[/B] must be static when it is a part of the class that must be launched the first
    in a application.
    
    2° You will also notice that the average displayed is [B]12.0[/B] because by default, the result is rounded and the number format only shows
    one decimal   
    [/COLOR]
    [/SIZE]
     
  3. Gibbs

    Gibbs

    Joined:
    Feb 28, 2005
    Messages:
    609
    I think this is an excellent read. It is a read though, bare that in mind: Overview: The Mental Landscape.

    I've been learning Java for about 3 days too. I've been following this guide which I think is very useful.
     
  4. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    Hi Gibbs,

    Indeed, the link you posted is an excellent reference for beginners. (y)
    Here is a good Java memento that can be downloaded : it is a useful tool for people who don't always have the possibilities to go online while programing in Java.
    ;)
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/543895

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice