Calculating Average Of Two Arrays - Calculation Coming Out Incorrect
Oct 8, 2014
I am working on an assignment, and I have come up with a program that runs, but it does not run correctly. This is the assignment:
Write two overloaded methods that return the average of an array with the following headers:
public static int average(int[] array)
public static double average(double[] array)
Write a test program that prompts the user to enter ten double values, invokes this method and displays the average value.
When I run it my averages are coming out incorrect, and I am not sure why. I have tried moving and rearranging things and nothing is working.
Java Code:
public static int average(int[] array) {
int sum = 0;
int average = 0;
[Code].....
View Replies
ADVERTISEMENT
Apr 21, 2014
I'm struggling with this assignment I was given:
Given list of positive integer values, write a program to calculate average of the values. List terminates with -1.
View Replies
View Related
Feb 21, 2014
I am trying to do the following java assignment and every thing seems to work fine except when I put a number<4 or >10 it prints both "Invalid grade!"
"You didn't enter any data!" what I wanted is to print only "Invalid grade!" I tried to fix it but I couldn't.
Create a program that asks for results of exams and calculates the mean average of the grades. Grades are floating point numbers between 4 and 10. Program asks for grades until a negative number is typed. If user gives a grade other than a number between 4 and 10, the text "Invalid grade!" will be printed on screen and program asks for another grade. Finally the program prints the number of inputted grades and their mean average on screen as shown in the example print. If no grades were inputted, the notice "You did not input any grades." is the only thing printed on screen.
A double type variable is to be used to store the value of the average.
Program is written to a class called Average.
Example output
Program calculates the average of inputted grades.
Finish with a negative integer.
Input a grade (4-10): 5
Input a grade (4-10): 6,5
Input a grade (4-10): 7,5
Input a grade (4-10): 7
Input a grade (4-10): 0
Invalid grade!
Input a grade (4-10): -4
4 grades inputted.
Average of the grades: 6.5
Java Code:
import java.util.Scanner;
public class apples {
public static void main(String[] args)
int inputNumber=0;
int sum;
int count;
double average;
[Code] .....
View Replies
View Related
Oct 31, 2014
I have been asked to construct an application that will read in the exam mark and coursework mark and then print out to 1 decimal place the average of the coursework and exam mark. When I submit it it says it's incorrect.
class Main
{
public static void main( String args[] )
{
int ExamMark=71;
int CourseworkMark=40;
double mark = (double)(ExamMark + CourseworkMark) / 2;
[Code] ....
View Replies
View Related
Oct 31, 2014
how to use Java and for my coursework I have been asked to construct an application that will read in the exam mark and coursework mark and then print out to 1 decimal place the average of the coursework and exam mark. When I submit it to the coursework submission system it says it's incorrect.
The answer that it's asking for is the answer that my code produces (ex = 71 cw = 40 mark = 55.5) But for some reason it says this when I submit it:
-#### << Differences between expected (<) your answer (>) >> ------------
1c1
< ex= 91 cw = 80 mark = 85.5
---
> ex = 71 cw = 40 mark = 55.5
-------------------------------------------------------------------------
Check the above output for why this attempt failed And when I change the ex to 91 and cw to 80 it asks for > ex = 71 cw = 40 mark = 55.5 again.
class Main
{
public static void main( String args[] ) {
int ExamMark=71;
int CourseworkMark=40;
double cost = (double)(ExamMark + CourseworkMark) / 2;
System.out.printf("ex = " + ExamMark + " cw = " + CourseworkMark + " mark = " + cost);
System.out.println();
}
}
View Replies
View Related
May 21, 2015
For some reason, the averages aren't calculating correctly. I'm aware I need to convert to double if I'm dividing by an odd amount but that itself isn't the issue. It's just simple easy divisible numbers that are not being calculated correctly. For instance, (10 + 10 + 10 + 50) / 4 = should be 2 but I get 10.
Here is the code:
import java.util.Scanner;
public class Avg {
public static void main(String[] args){
Scanner inputGains = new Scanner(System.in);
int userInput = inputGains.nextInt();
[Code] ....
View Replies
View Related
Jun 27, 2014
Why is my program not calculating the total and average of the price of the items and not printing it out?
Java Code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package assignment4;
[code]...
It prints out everything but the total of the items and I dont know why. :(
View Replies
View Related
May 7, 2014
I have to write a program that takes an array of grades and calculate the average and then count the number of grades that are above average and also count the number of grades that are below average. This is what I have so far, it calculate the average and tells me how many are above average... where and how do I get to count how many are below average
public class arrays{
public static void main(String[] args) {
final int grades = 5;
double[] numbers = new double[grades];
double sum = 0;
[Code] .....
View Replies
View Related
Nov 12, 2014
For my lab this week I have to print out four arrays. The arrays have to be 5 units long. The first three arrays are the student's exam grades for exam 1, 2 & 3 and the fourth array should be the average for each student's three tests. The program should ask the user to input the grades in and then the program should compute the average. For example the program should print this if these are the exam grades the user imputs:
Exam 1 Exam 2 Exam 3 Average
65 79 80 74.66
79 82 59 73.33
99 100 98 99
and so on.......
I thought I wrote the program perfectly and when I compile it there are no errors but when I run it, it only lets me input the numbers for the first three students and then prints out something so strange.
here is my program:
public class Lab9{
public static void main(String[] args){
java.util.Scanner input = new java.util.Scanner(System.in);
int i, sum;
double avg;
sum = 0;
int A[ ] = new int [5];
int B[ ] = new int [5];
int C[ ] = new int [5];
double avg1[ ] = new double[5];
[code]....
View Replies
View Related
May 3, 2014
How does jsp create session when there're two requests coming simultaneously from the same client browser(maybe one tab maybe two tabs)?
I ask this question because it is said jsp could solve DuplicateSessionException in flex.
[URL] ....
if so, jsp would create same session(with same session id) for the two requests coming simultaneously?
View Replies
View Related
Sep 29, 2014
I have a controller that on the basis of commands (formaction and subaction) dispatch requests to different jsp pages. But somehow when I am debugging my application, I can find duplicate request coming to the controller, so one jsp page does load twice. I am not sure from where the duplicate request is generating.
View Replies
View Related
Aug 14, 2014
I'm making a program to demonstrate Exception Handling. (Note: the code was written inside towards out). The user is prompted to input two values (integers) and then the program will divide those values and output the result.
I then added the "try" and "catch keywords for exception handling-instead of the program shutting down if an error occurs, the words "you cant do that" will appear
I then added a while loop to allow the user another chance to input usable values. Of course, I made sure to make a termination to the loop as well.
Everything works as expected if the user inputs e.g 8 divided by 0. in that case exception occurse, the user is told "you cant do that", is then prompted to start over and so forth until he gives usable integers. then the program continues to the termination. However, if the user inputs a String , e.g 9 divided by xyz, then the program goes into an endless loop
My question is:
1) why does a String cause an endless loop ( I have a theory)
2)why does the loop occur as soon as I put in an incorrect value- is the while loop somehow in extant? somehow always waiting? (hard to picture it)
3) lastly, what would be the correct code to handle and exception caused by a String (as opposed to being caused by impossible to execute math)
Here is the program
package inschool;
/*
*GOAL: to allow for an exception to occur without causing the entire program to stop.
*NOTE: the coding was written from the center outwards (e.g first "System.out.println", then "try", then "do")
*/
import java.util.*;
public class L82_ExceptionHandling {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
[Code] .....
View Replies
View Related
May 30, 2014
I am making a shopping cart app in JSP and servlets to bring it all together. Finally.
I have a registration servlet (c1), a registration form jsp (c2), a login jsp (c3). c1 checks if a username is in the database or not.
If the username is not there, then register the user and send them to c3. I want c3 to display a "registration successful" message if a user has just registered successfully and is coming from c1. ELSE, take the user back to c2 with a message which tells them to choose a different username or password etc.
How do I implement the logic of showing a message depending on where a user is coming from ? The servlets and jsps are ready and I only need to add this logic for a custom message. Of course, I could make a JSP for Registration Successful and one for Registration Failure. But, that seems to be unnecessary.
View Replies
View Related
Jun 16, 2014
I've been stuck on this one for a while. If you scroll down to the while loop there is some code that calculates the angle of a triangle created by two circles colliding. The problem is, The angle between the hypotenuse / x axis, and the angel between the hypotenuse / y axis never go above 65 degrees and i have no clue why?
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class MainTest extends JFrame {
public rectt g = new rectt();
[Code] .....
View Replies
View Related
Dec 18, 2014
I have an apache server with rest APIs accepting Json object and produce Json object.testing the sever using Chrome advanced rest client show no problems.I then tried to create a jave client, using org.jboss.resteasy:
public T sendPostRequest(String url, T input) throws Exception {
ClientRequest request = getNewClientRequest(url);
ObjectMapper om = new ObjectMapper();
final String body = om.writeValueAsString(input);
request.body(MediaType.APPLICATION_JSON, body);
final ClientResponse<String> responseObj = request.post(String.class);
validateResponseString(url, responseObj);
String responseString = responseObj.getEntity(String.class);
responseObj.releaseConnection();
[code]....
View Replies
View Related
Mar 24, 2014
How to get used to program control statements. This is an attempt to write a program that returns the number of days between two dates. Sadly, it give incorrect values.
public class DaysBetweenDates {
public static boolean isLeapyear(int year) {//This method will determine if a year is a leap year or not
boolean isTrue=false;
if(year%4==0 && year%100!=0) isTrue=true;
else if(year%400==0) isTrue=true;
return isTrue;
[Code] ....
View Replies
View Related
Jan 27, 2015
I am using the calendar component of the primefaces and I am using the attribute pattern="MM/dd/yyyy.But when I print the values in my MBean along with the selected date event the timeZone (i think) is printed as Sun Mar 29 00:00:00 IST 2015 I do not understand why its printed even though I have set the pattern.Part of my code :
XHTML
<p:calendar id="singleDaySchedule" value="#{scheduleMBean.exactDate}"
showOn="button" pattern="MM/dd/yyyy" required="true" requiredMessage="Date is Required"/>
<p:commandButton value="Submit" update="form" actionListener="#{scheduleMBean.showSchedule}" icon="ui-icon-check" />
[Code] ....
I found out that we need to use <f:convertDateTimePattern> instead of how I am using currently.But I am not able to figure out where to use
<f:convertDateTimePattern>
in my case
View Replies
View Related
Jun 28, 2014
I mainly would like to know is a int[4][4] or a int [4*4] is more efficient to use. Which takes more storage? Which requires more to process? that kind of stuff.
View Replies
View Related
Sep 8, 2014
I have a maths application I am working on in java. This is more related to maths but there could be some here who have worked with calculations like this in their java project.
One could convert from degree to radians and from mph to knots. Could one convert from knots to degree or from mph to degree?
View Replies
View Related
Apr 9, 2014
use arrays to store taxpayer information. Use methods for tasks that will be repeated.
-Ask the user how many taxpayers he would like to calculate taxes for.
-Ask the user to enter each taxpayer's first name, last name gross income, and number of children.
-Each taxpayer's tax due is computed as follows
-The taxpayer's dependency exemption is determined by multiplying $3,000 times the number of children.
-The taxpayer's net income is determined by taking the taxpayer's gross income and subtracting the taxpayer's dependency exemption.
-If the taxpayer's net income is between 0 and 50,000, the tax due is 15% of net income.
-If the taxpayer's net income is greater than 50,000, the tax due is
-15% of the first 50,000 of net income PLUS
-25% of any income over 50,000
-The tax due can never be less than 0.
-If the net income is a negative number, the tax due is 0
TAXPAYER INFORMATION: output a message in one dialog box which lists the following info for every taxpayer: first name, last name, gross income, number of children, tax due.
AVERAGE TAX: output a message in a dialog box which states the average of the taxes due.
PRESIDENTAL MESSAGE: output a message in a dialog box which says either "We computed taxes for the president. " or "We did not compute taxes for the president." The president's name is Barack Obama.
Here is my code so far
import javax.swing.JOptionPane;
public class AssignmentSeven
{
public static void main (String [] args)
{
String [] taxPayers;
String[] firstName;
String [] lastName;
String message = "";
double[] grossIncome;
[code]....
View Replies
View Related
Jul 10, 2014
I am facing problem during calculation in my awt calculator. It is showing NumberFormatException. Below is part of code.
if(e.getSource()==b14)//On button 14, label is "/"
{
String v1=tf.getText().toString();
num1=num1+Integer.parseInt(v1);//num1 is as integer variable
tf.setText("");//tf means textField
}else
[code]....
View Replies
View Related
Oct 3, 2014
how to do this last calculation. I want the user to input and odometer (in feet) and have it use that and the Duration (in hours) to calculate speed in feet per second. Ive been trying to reference it using various methods but I'm stuck. I have it set as a string but whenever i try to pretty much use somewhat the same formula i did for total time it just gives me errors. How do I use that number the user inputs into a formula with the output of the Duration?
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
[code]....
View Replies
View Related
Sep 8, 2014
I am writing a program in Java where a user adds 1 stamp on a letter for every five sheets of paper in it (or fraction thereof)For example, I if I have 11 sheets of paper, I add 3 stamps. If the total number of stamps is more than 3, they don't send it.
I need to ask the user how many sheets of paper they have, and then calculate the appropriate number of stamps based on the formula above. I just don't know how to write the math formula to calculate this, i'm stuck. So far I have:
package test;
import java.util.Scanner;
public class LetterMailing {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
int numSheets;
[Code] .....
What should I do next?
View Replies
View Related
May 12, 2014
I'm trying to create a GUI interface in Netbeans that calculates a phone bill.
I created a GUI interface that input:
- Account Name.
- Account Number.
- Number of Minutes the phone was used. (with a textarea so the user input the minutes)
- 2 JRadioButtoms so the user can chose the regular or premium service.
- Amount Due (+10% taxes) (also with a textarea)
- Clear/Enter/Result/Done Buttoms
And I need to output:
- Account Name.
- Account Number.
- Number of Minutes.
- Taxes
- Total Amount Due.
I have a couple of math formulas I have to add them to output the above however I'm new to java so I don't know which code to use.
The math formulas are:
-Regular charges = 20 + ( No. Minutes - 50 ) * 0.20
-Premium charges = 50 + ( No. Minutes - 75 ) * 0.50
-Taxes = ( Regular charges + Premium charges ) * 0.10
-Amount Due = Regular charges + Premium charges + Taxes
View Replies
View Related
Jan 23, 2014
My GCD testing:
Java Code:
public static long gcd(long a, long b) {
if(b == 0) {
return a;
} else {
return gcd(b, a % b);
}
} mh_sh_highlight_all('java');
Is there a more efficient way? Also, what can I do to speed this forloop up?
Java Code:
for(long i = 1; i < 750000; i++) { dothis; } mh_sh_highlight_all('java');
View Replies
View Related
Jun 19, 2014
I have a calculation that sorts books by rating...
System.out.println("
" + "
Sorted by Rating
");
for (int count = 0; count < bookL.size(); count++) {
for (int in = 0; in < bookList.size() - 1; in++)
if (videoList.get(in).getRating() < bookList.get(in + 1).getRating()) {
[Code] ....
What am I missing here? Why doesn't this work?
View Replies
View Related