Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Creating a simple java program for assignment

Hey guys, so this is the set of instructions i got from my professor.
I didn't pay attention to this part of the class and it's supposed to be something easy and I'm kinda confused so i'm wondering if anyone can walk me through this and of course you will be rewarded.

Quote:Using jGRASP, write a Java program that does the following:
Ask the user to enter in 3 numbers
Keep in mind that the user may enter numbers with decimals
Ask the user 3 times to enter a number
Print the three numbers with labels
Print the average of the numbers.
Be sure to format your output in such a way that the computing process and the results are understandable.
Do NOT use magic numbers
You may be tempted to divide the sum of the numbers by a hard-coded 3. However, what you should do it store the number 3 in a variable to perform the average calculation.

well the first thing would be to go to http://jgrasp.org/ and download and install it. After that watch their tutorials.
https://www.youtube.com/watch?v=DHICqIYV33k

Then try again yourself. PM me if stuck.
You'll have to be more specific on what you're stuck on. I'll assume you want someone to do this for you?
lol why are you using jgrasp, seems like an odd ide to use.

Ask the user to enter in 3 numbers - Scanner class, google it
Keep in mind that the user may enter numbers with decimals - Doubles and floats exist in java
Ask the user 3 times to enter a number - ask user through the scanner class
Print the three numbers with labels - system.out.println
Print the average of the numbers. - use math then print it
I think the most easiest way to code this is doing it by this:

Code
package com.hackforums;
import java.util.Scanner;

/**
*
* @author Mohinder
*/
public class Lelz {

    public static void main(String[] args) {
        Scanner namescanner = new Scanner(System.in);
        
        System.out.print("Enter the first number: ");
        double numberOne = namescanner.nextDouble();
        System.out.print("Enter the second number: ");
        double numberTwo = namescanner.nextDouble();
        System.out.print("Enter the third number: ");
        double numberThree = namescanner.nextDouble();
        double averageNumbers = (numberOne + numberTwo + numberThree) / 3;
        
        System.out.println("The average of the three numbers is: " + averageNumbers);
    }
    
}

Havent run it, but it should work.
(Sep 29, 2016 12:36 PM)lolxd123 Wrote:  
Code
You may be tempted to divide the sum of the numbers by a hard-coded 3. However, what you should do it store the number 3 in a variable to perform the average calculation.

Storing 3 in a variable would STILL be hard coded as it is considered a constant. Tell your professor he is an idiot.

You should learn what magic numbers are before calling someone an idiot.
(Sep 29, 2016 07:52 PM)lolxd123 Wrote:  I know exactly what a magic number is. However, that is completely irrelevant to my response (what I stated does not even regard to magic numbers). Assigning 3 to a variable is still considered hard coded, which is poor wording by the professor. That can easily confuse someone new to programming.

If I went on about naming conventions for variables then you can bring up magic numbers (which I did not).

You really don't understand magic numbers, lol.

And variable naming conventions have absolutely nothing to do with magic numbers.
With respect to the above conversation (using python like pseudo code, cause that's the simplest to read and write)

Code
sum = num1+ num2 + num3;
elementsCount = 3;
avg = sum/elementsCount;

The above code avoids usage of magic numbers but still does hard code 3.

A non hard-coded solution would be something like
Code
list = []
list.append(num1);
list.append(num2);
list.append(num3);
// There can be more appends
sum = 0
for num in list:
    sum+=num
avg = sum/len(list);
This wouldn't be hard coded.

So I guess both of you are trying to say the same thing.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Java] by GrandpaSwampDec 14, 2019 09:09 PM 5 259 Dec 14, 2019 09:09 PM
Last Post: GenericSkid
[Java] by GulpDec 7, 2018 05:41 AM 5 231 Dec 7, 2018 05:41 AM
Last Post: .Skies
[Python] by GlassAug 14, 2018 10:22 AM 2 152 Aug 14, 2018 10:22 AM
Last Post: Glass