I need to compute the multiplication of the grade[0] to percentage1[0] and then print the result. Can someone help me?
LaboratoryExam.java
import java.util.*;
public class LaboratoryExam {
static Scanner glad=new Scanner(System.in);
static double grades[]=new double[5];
static double percentage1[]=new double[5];
static double comp[]=new double[5];
static double sum=0;
public static void main(String[] args) {
System.out.print("Enter 5 grades: ");
for(int i=0; i<grades.length; i++){
grades[i]=glad.nextDouble();
}
Percentage();
compute(grades,percentage1);
System.out.println("\nSum is: " + sum);
}
public static double[] Percentage(){
double Percentage;
System.out.print("\nEnter 5 Percentage: ");
for(int p=0; p<percentage1.length; p++)
percentage1[p]=glad.nextDouble();
return percentage1;
}
public static double compute( double[] grades, double[] percentage1 ){
for(int i=0; i<comp.length;i++){
double comp=grades[0]*percentage1[1];
System.out.println("Equivalent: "+sum);
double sum=comp+comp;
}
return sum;
}
}
You should do the multiplication with grades[i] and percentage1[i]. Here you're just multiplying the same number over and over. Try this:
double comp=grades[i]*percentage1[i];
Also, I think you're not printing the right thing, since you seem to want to print the result of the multiplication. Try with this:
System.out.println("Equivalent: "+ comp);
Finaly, I think it would be better to declare comp and sum out of the for:
public static double compute( double[] grades, double[] percentage1 ){
double comp;
double sum;
for(int i=0; i<comp.length;i++){
comp=grades[0]*percentage1[1];
System.out.println("Equivalent: "+sum);
sum=comp+comp;
}
return sum;
}
In your compute method, you're getting the product of grades[0] and percentage1[1] for all the iterations. Instead, you should use the elements on the i index as stated in your for loop:
for(int i=0; i<comp.length;i++) {
double comp = grades[i]*percentage1[i];
Also, you're calculating the wrong sum:
for(int i=0; i<comp.length;i++) {
double sum=comp+comp;
}
Here you're shadowing the static double sum declared before, so you will always get 0 when doing return sum;. You should instead use the sum variable declared in your class:
for(int i=0; i<comp.length;i++) {
//note: no re-declaration of sum variable
//also fixed the sum calculation
sum += comp;
}
In the end, your code will look like:
for(int i=0; i<comp.length;i++) {
double comp=grades[i]*percentage1[i];
comp[i] = comp;
sum += comp;
System.out.println("Equivalent: "+sum);
}
return sum;
This is almost good. Make sure you declare the sum variable outside of the for loop so you can retain its value on each iteration of the loop. Also use your array indices to access the correct values. Change it to this:
public static double compute( double[] grades, double[] percentage1 ){
double sum = 0;
for(int i=0; i<comp.length;i++){
comp[i]=grades[i]*percentage1[i];
System.out.println("Equivalent: "+comp[i]);
sum += comp[i];
}
return sum;
}
Also, in your main method, make it:
sum = compute(grades,percentage1);
Related
I need to write a program that creates a HomeworkGrades() class that stores the homework grades of 8 chapters into an array of doubles. I need to create a constructor that takes an array as input and copies the contents of the array and copies it into the classes array. Finally in my main() class I need to print out the average of the scores in the array and the lowest value in the array. I think I created my HomeworkGrades() class correctly, I just can't figure out how to call the correct methods to make the values print to the screen.
Here is my main() class...
package classwork_7_1;
public class ClassWork_7_1 {
public static void main(String[] args) {
double[] grades = {98.7, 77.9, 90, 83, 67, 33, 81, 90};
double lowChap = grades[0];
System.out.println(average(grades[]));
System.out.println(lowChap(lowChap[]));
}
}
Here is my HomeworkGrades() class...
package classwork_7_1;
public class HomeworkGrades {
private double[] grades = new double[8];
public HomeworkGrades(double[] grades) {
this.grades = grades;
}
public double average(double[] grades) {
int chap = 8;
double sum = 0;
for (int i = 0; i < grades.length; i++)
sum += i;
double average = sum / chap;
return average;
}
public double lowChap(double[] grades) {
double minValue = grades[0];
for (int i = 0; i < grades.length; i++) {
if (grades[i] < minValue) {
minValue = grades[i];
return minValue;
}
}
}
}
Here in these lines:
double lowChap = grades[0];
System.out.println(average(grades[]));
System.out.println(lowChap(lowChap[]));
lowChap is a single double and you're trying to pass lowChap[] into lowChap() which accepts an Array of doubles, and needs to be invoked on a HomeWorkGrades object. Clearly this will not work. Instead, you must create an object of type HomeWorkGrades and pass grades into the constructor and then call the methods on that object. In which case you could remove the arguments in your methods and just use this.grades:
HomeWorkGrades hw = new HomeWorkGrades(grades);
System.out.println(hw.average());
System.out.println(hw.lowChap());
So in your methods you would have:
public double average(){
int chap = 8;
double sum = 0;
for(int i = 0; i<grades.length; i++)
sum+=grades[i];
double average = sum / chap;
return average;
}
public double lowChap(){
double minValue = grades[0];
for(int i=0;i<grades.length;i++){
if(grades[i] < minValue)
minValue = grades[i];
}
return minValue;
}
Note that in average I changed sum += i; to sum+= grades[i]; to add the grade at index i to sum instead of just i.
Output:
77.575
33.0
Note that below code doesn't handle the case of an empty grades array.
double min = Arrays.stream(grades).min().getAsDouble();
double average = Arrays.stream(grades).average().getAsDouble();
import java.util.Scanner;
public class ProjectFour {
public static void main(String args[]) {
int[] firstArray = {1,2,3,2,1,6,3,4,5};
System.out.println("this is the average of array : "+analyzeNumbers(firstArray));
System.out.println("These are the numbers above the average : "+aboveAvg(firstArray));
}
//finding the average
public static int analyzeNumbers(int[] firstArray){
int avg;
avg=sumArray(firstArray);
avg=avg/firstArray.length;
return avg;
}
//suming the array method
public static int sumArray(int[] firstArray){
int sum = 0;
for(int x=0;x<firstArray.length;x++){
sum+=firstArray[x];
}
return sum;
}
**this is where im running into problems im kinda stumpted**
// this is my method i cant figure out trying to take the average and find all the numbers in the array above the average and printing them.
public static int aboveAvg(int[] firstArray){
int[] aboveAvg;
aboveAvg = new int[0];
int x;
for(x=analyzeNumbers(firstArray);x<firstArray.length;x++){
aboveAvg+=firstArray[x];
}
return aboveAvg;
}
}
Try using a for loop.
int sum = 0;
for(int i = 0; i < firstArray; i++) {
int getSum = firstArray.get(i);
sum + getSum;
}
int average = sum / firstArray.length;
int[] aboveAverage;
for(int c = 0; c < firstArray; c++) {
if(firstArray.get(c) > average) {
aboveAverage.add(firstArray.get(c));
}
}
This aboveAvg function is completly wrong.
public static List<Integer> aboveAvg(int[] firstArray){
List<Integer> aboveAvg = new ArrayList<Integer>();
int Avg = analyzeNumbers(firstArray);
for(int i = 0; i<firstArray.length; i++)
{
if(firstArray[i] > Avg)
{
aboveAvg.add(firstArray[i]);
}
}
return aboveAvg;
}
Check your for loop and more examples about it
+= will sum two values, won't add new element on any array.
you have to define your return value correctly.
You can use List for create arrays, it's more flexible.
I've just recently started programming Java and I'm having a problem that's making me want to break things. It starting to get annoying, and I'm feeling pretty stupid right now.
The task is to write a program that asks for five numbers to be input into an array (yes, can't use a list) and to then calculate the average of the five numbers input.
Where am I going wrong?
My current code calculates the average after each input. I want to do that after they've all been inserted, otherwise what is the point?
All help is greatly appreciated, you believe me!
Here's the code:
import java.util.Scanner;
public class Uppg3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] numbers = new int[5];
int sum = 0;
for (int i = 0; i < numbers.length; i++)
{
System.out.println("enter a number: ");
numbers[i] = input.nextInt();
sum = numbers[i];
}
double average = sum / 5;
System.out.println("Average is " + average);
input.close();
}
}
You ought to encapsulate such a thing in a method.
You can either process all the numbers at once by storing them in an array and passing them at the same time or keep a running tally and return it on request.
public class StatisticsUtils {
private double sum;
private int numValues;
public StatisticsUtils() {
this.sum = 0.0;
this.numValues = 0;
}
public void addValue(double value) {
this.sum += value;
++this.numValues;
}
public double getAverage() {
double average = 0.0;
if (this.numValues > 0) {
average = this.sum/this.numValues;
}
return average;
}
public static double getAverage(double [] values) {
double average = 0.0;
if ((values != null) && (values.length > 0)) {
for (double value : values) {
average += value;
}
average /= values.length;
}
return average;
}
}
Looking at this for loop
for (int i = 0; i < numbers.length; i++)
{
System.out.println("enter a number: ");
numbers[i] = input.nextInt();
sum = numbers[i];
}
You are adding it to the array just fine, but you keep setting the sum to whatever they enter. For example, if I enter 5 3 2, the array will have 5 3 2 in it, but the sum will be set to 2. If I only entered 5 3, it would be set to 3.
One solution would be to use +=, that way it adds that number to whatever the current value is (which you start at 0). It's also important to note that when you divide two integers (sum and 5), it will not give you a decimal. This can be solved by using 5.0 or casting sum to a double.
Your code is currently just assigning a different value to sum on every iteration of the loop. You need to change sum = numbers[i]; to sum += numbers[i]; (equivalent to sum = sum + numbers[i];). You'll also want to change int sum = 0; to double sum = 0 so that your answer has decimals.
Ok, it works with the "+=", love you Chris Phelps. That and changing:
int sum = 0;
to
double sum = 0;
To make the answer have a decimal. Thanks a lot you guys! A weight has been lifted of my chest :).
Code for calculating average of array input
package com.imedxs;
import java.util.Scanner;
public class Test {
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("for calculating average of array");
int n=0;
int b=sc.nextInt();
int[] numbers = new int[b];
int sum = 0;
for (int i = 0; i < b; i++)
{
System.out.println("enter a number: ");
numbers[i] = sc.nextInt();
sum += numbers[i];
n++;
}
double average = sum / n;
System.out.println("Average is " + average);
}
}
I am having some issues with some homework that I cannot figure out, if someone could point me in the correct direction I would greatly appreciate it. My sentinel value is not working at all and I cannot figure it out so that it works. Also my array for my averages is printing out as memory locations and not the value. Here is the code I have so far
public class DistanceFromAverage
{//Global Declaration Section
public static void main(String args[])
{//Declaration Section
double[] Numbers;
double[] Average;
//Input Section
Numbers = array_numbers();
//Processing Section
Average = distance_average(Numbers);
//Output Section
display_array(Numbers, Average);
}//end
public static double[] array_numbers()
{
double[] tmp;
tmp = new double[20];
double[] sentinel = {99999};
Scanner input = new Scanner(System.in);
int count = 0;
try
{
for (int i = 0; i < tmp.length; i++)
{
System.out.println("Please enter number");
tmp[i] = input.nextDouble();
}
}
catch(InputMismatchException exception)
{
System.out.println("A number must be entered");
System.exit(0);
}
return tmp;
}//end array
public static double[] distance_average(double[] Numbers) {
double sum = 0.0;
for (int i=0; i < Numbers.length; i++)
sum = sum + Numbers[i];
double average = sum / Numbers.length;
return new double[] {average};
} // determine average
public static void display_array(double[] Numbers, double[] Average)
{
for (int i = 0; i < Numbers.length; i++){
System.out.println("The numbers in the array are: " + Numbers[i] + "and the average is" + Average);
}
}//end display_array
}//end class
Regarding Average being printed as Memory Location,
Your display function accepts Average as an Array and not as a simple double.
This will call toString() on your Array class.
Since Average is just a single number, either use Average[0] in your 'System.out.println' statement or accept a simple average of type double in your display method like below:
public static void display_array(double[] Numbers, double Average)
and change the signature of
public static double[] distance_average(double[] Numbers) {
to
public static double distance_average(double[] Numbers) {
I am expecting 3.5 as result from average method, but I get 3.0. No idea why. I expected Double to give me the result, but no.
java.util.ArrayList;
public class Hangman {
public static void main(String[] args) {
ArrayList<Integer> intList = new ArrayList<Integer>();
intList.add(3);
intList.add(2);
intList.add(7);
intList.add(2);
System.out.println("The variance number is : ");
System.out.println(sum(intList));
System.out.println(intList.size());
System.out.println(average(intList));
}
public static int sum(ArrayList<Integer> intList) {
int sum = 0;
for (int counter = 0; counter < intList.size(); counter++) {
sum = sum + intList.get(counter);
}
return sum;
}
public static double average(ArrayList<Integer> intList) {
double avg = sum(intList) / (intList.size());
return avg;
}
public static ArrayList<Double> subtract(ArrayList<Integer> intList) {
ArrayList<Double> subtracted = new ArrayList<Double>();
for (double subtract : intList) {
subtracted.add((double) (subtract - average(intList)));
}
return subtracted;
}
public static double variance(ArrayList<Integer> intList) {
double sumDiffsSquared = 0.0;
double avg = average(intList);
for (int value : intList) {
double diff = value - avg;
diff *= diff;
sumDiffsSquared += diff;
}
return (sumDiffsSquared / (intList.size() - 1));
}
}
sum needs to return a double, otherwise when you do a
sum(intList) / (intList.size());
in your average method, it truncates the value calculated down to an integer, and then puts that value in double form.
Your function needs to return a double value.
Change your method to
public static double sum(ArrayList<Integer> intList) {
double sum = 0;
for (int counter = 0; counter < intList.size(); counter++) {
sum = sum + intList.get(counter);
}
return sum;
}
and you should be doing fine.
Though the type of avg is double , sum() method is returning a Integer value. So you have return a double from sum() method
double avg = (double)sum(intList) / (intList.size());