Jennifer Dana
Jennifer Dana

Reputation: 3

Java Averaging Program

Write a class called Average that can be used to calculate average of several integers. It should contain the following methods:

 A method that accepts two integer parameters and returns their average.  A method that accepts three integer parameters and returns their average.  A method that accepts two integer parameters that represent a range. Issue an error message and return zero if the second parameter is less than the first one. Otherwise, the method should return the average of the integers in that range (inclusive).

I am totally new to Java and programming, this has me completely lost! Here's what I've tried.

import java.util.Scanner;

public class Average {

    public static void main(String[] args) {

    double numb1, numb2, numb3;
    System.out.println("Enter two numbers you'd like to be averaged.");
    Scanner keyboard = new Scanner(System.in);
    numb1 = keyboard.nextInt();
    numb2 = keyboard.nextInt();

    }

        public double average (int num1, int num2) {

            return (num1 + num2) / 2.0; 
    } 

        public double average (int num1, int num2, int num3) 
        { 
         return (num1 + num2 + num3) / 3.0; 
        } 

}

The program doesn't go past getting the values from the user. Please help!

Upvotes: 0

Views: 41780

Answers (9)

Lalitha Rajesh
Lalitha Rajesh

Reputation: 11

public class Marks {
    int roll_no;
    int subject1;
    int subject2;
    int subject3;

    public int getRoll_no() {
        return roll_no;
    }

    public void setRoll_no(int roll_no) {
        this.roll_no = roll_no;
    }

    public int getSubject1() {
        return subject1;
    }

    public void setSubject1(int subject1) {
        this.subject1 = subject1;
    }

    public int getSubject2() {
        return subject2;
    }

    public void setSubject2(int subject2) {
        this.subject2 = subject2;
    }

    public int getSubject3() {
        return subject3;
    }

    public void setSubject3(int subject3) {
        this.subject3 = subject3;
    }
    public void getDetails(){
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the marks of subject1");
        this.subject1 = sc.nextInt();
        System.out.println("Enter the marks of subject2");
        this.subject2 = sc.nextInt();
        System.out.println("Enter the marks of subject3");
        this.subject3 = sc.nextInt();
        System.out.println("Enter the roll number");
        this.roll_no = sc.nextInt();
    }
    public int getAverage(){
        int avg = (getSubject1() + getSubject2() + getSubject3()) / 3;
        return avg;
    }
    public void printAverage(){
        System.out.println("The average is : " + getAverage());
    }
    public void printRollNum(){
        System.out.println("The roll number of the student is: " + getRoll_no());
    }
    public static void main(String[] args){
        Marks[] e1 = new Marks[8];
        for(int i=0; i<2; i++) {
            System.out.println("Enter the data of student with id:");
            e1[i] = new Marks();
            e1[i].getDetails();
            e1[i].printAverage();
        }
        System.out.println("Roll number details");
        for(int i=0; i<2; i++){
            e1[i].printRollNum();
        }
    }
}

Upvotes: 1

Shivam Shah
Shivam Shah

Reputation: 41

The main method will only execute what it is asked to. If you want the average methods to be executed, you will have to create an object, pass the required variable and call the methods from the main method. Write the following lines in the main method after accepting the input.

Average avrg = new Average();
System.out.println("The average is: " + avrg.average(numb1, numb2, numb3));

Write only numb1 and numb2 if you want to average only two numbers.

Upvotes: 0

Praveen Gundu
Praveen Gundu

Reputation: 11

You have to use bitwise operators. average of int a and b can be calculated as avg= (a >> 1) + (b >> 1) + (((a & 1) + (b & 1)) >> 1);

Upvotes: 0

Airrr
Airrr

Reputation: 11

Let's detail what you did there.

public static void main(String[] args) {   
//Create variables numb1, numb2 & numb3
double numb1, numb2, numb3;
System.out.println("Enter two numbers you'd like to be averaged.");
//Read standard input (keyboard)
Scanner keyboard = new Scanner(System.in);
//Retrieve first input as an int
numb1 = keyboard.nextInt();
//Retrieve second input as an int
numb2 = keyboard.nextInt();
}

Then your two next methods compute for two or three given integers their average. The main method is the first method called during your program execution. The jvm will execute everything inside. So it will declare the three doubles, read two values from keyboard and then end.

If you want to compute the average of numb1 & numb2 using your method, you have to create an object Average and call your average method like this

public static void main(String[] args) {   
//Create variables numb1, numb2 & numb3
double numb1, numb2, numb3;
System.out.println("Enter two numbers you'd like to be averaged.");
//Read standard input (keyboard)
Scanner keyboard = new Scanner(System.in);
//Retrieve first input as an int
numb1 = keyboard.nextInt();
//Retrieve second input as an int
numb2 = keyboard.nextInt();
//Declare the average value
double average;
//Create an average instance of the class average
Average averageObject = new Average();
//Call your average method
average = averageObject.average(numb1,numb2);
//Print the result
System.out.println("Average is : " + average);
}

Everything in Java is object (read about Object Oriented Programming). Writing your class "Average" defines how your object is structured. It has attributes (characteristics) and methods (actions). Your Average object has no attributes. However it has two methods (average with two and three numbers) acting on integers. However your class is just the skeleton of your object. You need to create an object from this skeleton using the keyword new as :

Average averageObject = new Average();

Sincerely

Upvotes: 1

Whoppa
Whoppa

Reputation: 999

After the access specifier (public) and before the return type (double) place the Java keyword static. You shouldn't worry about what this means right now.

Upvotes: 0

Jeroen Vannevel
Jeroen Vannevel

Reputation: 44438

You have to actually call your methods.

Just place

Average avg = new Average();
System.out.println("The average is: " + avg.average(numb1, numb2));

at the end of your main method.

Alternatively you can make the methods static:

public static double average (int num1, int num2) {
    return (num1 + num2) / 2.0; 
} 

More info on constructors and static.

Upvotes: 5

Arvind
Arvind

Reputation: 13

you need to call the methods that you have written after you accept the input.

...

System.out.println("Enter two numbers you'd like to be averaged.");
Scanner keyboard = new Scanner(System.in);
numb1 = keyboard.nextInt();
numb2 = keyboard.nextInt();
System.out.println(average (int numb1 , int numb2 ))

...

You probably want to provide a menu of options for the user to select to determine which method to call

System.out.println("Select one option");
System.out.println("1. Enter two numbers you'd like to be averaged.");
System.out.println("2. Enter the 3 numbers you want averaged.");
System.out.println("3. Enter the number Range you want averaged.");

and based on that answer you can determine which method to call

Upvotes: 0

sirFunkenstine
sirFunkenstine

Reputation: 8515

It looks like your not actually printing out the results. Try the following.

    System.out.print(average(numb1, numb2));                                            

Upvotes: 1

Farmer Joe
Farmer Joe

Reputation: 6070

If you'd like your program to find the average you need to include a call to that method in your main method.

numb1 = keyboard.nextInt();
numb2 = keyboard.nextInt();
System.out.println("The average of " + numb1 + " and " + numb2 + " is " + average(numb1,numb2);
}

Upvotes: 0

Related Questions