Help needed in Java for Writing an Application that reads four doubles as its input
I have given a project. I need help for the same project that has to be completed soon. I have to write an application that reads four doubles as its input in Java. I am new to such scenarios. The program then displays as its output the minimum integer of the four, maximum integer of the four and then the average of the four doubles. Can someone help me by providing the codes.?? Need help as early as possible.
Re: Help needed in Java for Writing an Application that reads four doubles as its input
I think that you will have to first get the input, so that you can display its output as minimum, maximum and the average integer of the four. For doing this you can either use the JOptionPane class or through system.in. You can calculate the average by adding up the four values and dividing by four once you have the four doubles stored in variables. You will have to call Math.max on the first two numbers in order to calculate the max, then you will have to store this in a temp variable. You will have to do the same procedure for the rest operations. After this you display the values, again you can use the JOptionPane class or System.out to print to the console.
Re: Help needed in Java for Writing an Application that reads four doubles as its input
I don't know much about Java. But I think that I can give the code for a program that takes its input from a file of numbers of type double in C++. The output is the average, variance, and standard deviation of the numbers in the file.
#include <iostream>
#include <fstream> //to use fstream functions
#include <stdio.h>
#include <string>
#include <iomanip>
#include <math.h>
using namespace std;
int RecCount=0;
int main()
{
double variance =0,standdev=0;
string myfilename, str;
myfilename = "test.txt";
ifstream inFile;
inFile.open(myfilename.c_str());
if (!inFile)
{
cout <<"Error opening file " << endl;
system("pause");
return -1;
}
int elmAdditons=0,elm=0,elmPrev=0;
cout<<"Numbers In The File As Following"<<endl;
while (!inFile.eof()) {
getline(inFile,str);
cout<<str<<endl;
elm =atoi(str.c_str());
if(RecCount>0)
{
elmAdditons+= (elm-elmPrev)*(elm-elmPrev);
}
elmPrev=elm;
RecCount++;
}
variance=( double) elmAdditons/RecCount;
standdev=sqrt(variance);
cout<<"Variance Is "<<variance<<endl;
cout<<"standard deviation Is " <<standdev<<endl;
getchar();
}
I think this coding will help you to clear the logic for the input from a file of numbers of type double.
Re: Help needed in Java for Writing an Application that reads four doubles as its input
I would like to Thanks 'Modifier', since I was searching for this code in C++ for a long time. Your work is really appreciable. I would like to suggest 'SKREECH' to search the similar topics in the Sun's website. I am sure that you will find some relevant answers over there. Also try to do the R & D even if you get some similar coding. By doing that you will improve your skills in specific programming languages.
Re: Help needed in Java for Writing an Application that reads four doubles as its input
I would like to suggest you to avoid use of the Math.max method since it only accepts two inputs. You have to check that number is greater than the current "greatest number". So you can just compare the number to the greatest number so far using the > operator. If it is greater then store this new number as the greatest number. By using the java.util.Scanner class, I believe that console input is most easily accomplished. Also you will have to use a for loop that iterates 4 times to accomplish this.