Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8275063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:56:59+00:00 2026-06-08T07:56:59+00:00

I wrote below code to satisfy program requirement as follows: Average of Three Write

  • 0

I wrote below code to satisfy program requirement as follows:

Average of Three Write a program that reads three whole numbers and
displays the average of the three numbers.

Input Notes: Three whole numbers (non-negative integers) are entered
at the console.

Output Notes (Prompts and Labels): The program prompts for the three
integers with these strings: “Enter the first integer.”, “Enter the
second integer.”, “Enter the third integer.”. The program then prints
        The average of NUMBER1,  NUMBER2,  and NUMBER3  = AVG   where NUMBER1  is the first integer value entered and NUMBER2  and NUMBER3 
the subsequent integers, and AVG  is the computed average value.

SPECIFICATION OF NAMES: Your application class should be called
Average3:

My source code:

import java.util.Scanner;
public class Average3 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        int AVG, NUMBER1, NUMBER2, NUMBER3;
        System.out.println("Enter the first integer.");
        Scanner keyboard = new Scanner(System.in);
        NUMBER1 = keyboard.nextInt();
        System.out.println("Enter the second integer.");
        NUMBER2 = keyboard.nextInt();
        System.out.println("Enter the third integer.");
        NUMBER3 = keyboard.nextInt();
        AVG = (NUMBER1 + NUMBER2 + NUMBER3) / 3;
        System.out.println("The average of NUMBER1, NUMBER2, and NUMBER3 = " + AVG);

    }

}

My program compiles fine but I know I could have implemented and invoked an object with an associated method but am struggling where to start I understand the methods and objects conceptually but not as far as writing code. Anyone have any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T07:57:01+00:00Added an answer on June 8, 2026 at 7:57 am

    First, I would create a class InputData that would store the inputs from the user:

    class InputData {
        public int number1;
        public int number2;
        public int number3;
    }
    

    This in itself is a useful general technique: collect several related values into a single data structure. You can then either rewrite your main method to use this class instead of three separate int variables, or you can go one step further and add some behavior to the InputData class. The obvious first behavior to add would be to compute the average:

    class InputData {
        public int number1;
        public int number2;
        public int number3;
    
        public int average() {
            return (number1 + number2 + number3) / 3;
        }
    }
    

    With this, you could rewrite your main as follows:

    public class Average3 {
        static class InputData {
            public int number1;
            public int number2;
            public int number3;
    
            public int average() {
                return (number1 + number2 + number3) / 3;
            }
        }
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            InputData input = new InputData();
            System.out.println("Enter the first integer.");
            Scanner keyboard = new Scanner(System.in);
            input.number1 = keyboard.nextInt();
            System.out.println("Enter the second integer.");
            input.number2 = keyboard.nextInt();
            System.out.println("Enter the third integer.");
            input.number3 = keyboard.nextInt();
            System.out.println("The average of NUMBER1, NUMBER2, and NUMBER3 = "
                + input.average());
        }
    }
    

    Notice that I’ve made InputData a static inner class of Average3. It could also have been a separate, top-level class in the same file (as long as it wasn’t public) or a class in a separate file.

    An improvement on this would be to use an array instead of separate int fields in the InputData class. Your program might then look like this:

    public class Average3 {
        static class InputData {
            public int[] numbers;
    
            InputData(int size) {
                numbers = new int[size];
            }
    
            public int average() {
                int sum = 0;
                for (int number : numbers) {
                    sum += number;
                }
                return sum / numbers.length;
            }
        }
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            String[] prompts = { "first", "second", "third" };
            InputData input = new InputData(3);
            Scanner keyboard = new Scanner(System.in);
            for (int i = 0; i < prompts.length; ++i) {
                System.out.println("Enter the " + prompts[i] + " integer.");
                input.numbers[i] = keyboard.nextInt();
            }
            System.out.println("The average of NUMBER1, NUMBER2, and NUMBER3 = "
                + input.average());
        }
    }
    

    Here I’ve added a constructor to InputData to initialize the array and given it an argument to set the size of the array.

    You could then introduce additional improvements like making the number of input values dynamic (using an ArrayList<Integer> instead of an int array). But this goes beyond the specific requirements of the assignment. Some people (many, actually) tend to do this kind of generalization automatically. However, advocates of Extreme Programming will point out that it is generally better to keep things simple: design and code for the needs of today instead of those of tomorrow, next week, or next month.

    In other words, you don’t know what the next assignment will bring, so however you generalize beyond the immediate assignment might be wasted work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have wrote the code below which was taken from Java How to program
I wrote the below code to retrieve data from the data base, in that
I wrote below code to compare to arrays that have same elements but in
In my Project I included a splash-screen. For that i wrote below code in
I wrote code below that is very similar to the accepted answer here .
I wrote the below code in order to check for three files and whichever
I want to send email programmatically. For that, i wrote the below code: if(field
I have to perform async execution, and i have wrote the below code var
I wrote the code below : 1. MyClass[] origArr=new MyClass[3]; 2. MyClass[] arr1; 3.
So I wrote the code below: jQuery('#contentWrapper').delegate(.addButton, click, function(){ addRow(jQuery(this)); }); function addRow(thisButton){ var

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.