HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Output in one window

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
windowoneoutput

Problem

Can anyone give me feedback please? I used what I have learnt so far, mainly methods, loops and arrays. I would like you view based on these topics, however, comments on how to improve are welcome.

```
/*ask the user for 10 numbers
store them in an array
print them "your numbers are:"
print them "Average"
print them "Max"
print them "Min"*/

import javax.swing.*;

public class homeworkArrayWFunctionstest5
{
public static void main(String[] args) throws Exception
{
String listOfNums = "";
int sum = 0;
int population = 0;
int average = 0;
int maxValue = 0;
int minValue = 0;
String [] numsStringInput = new String[10];
population = numsStringInput.length;
int [] arrayNumInt = new int[population];

numsStringInput = UserInputs(numsStringInput,population);
arrayNumInt = StringToInt(numsStringInput,population);
listOfNums = OneLineList(numsStringInput, population);
sum = TotalCalc(arrayNumInt,population);
average = AvarageCalc(sum, population);
maxValue = MaxValueCalc(arrayNumInt,population);
minValue = MinValueCalc(arrayNumInt,population);

Output(listOfNums,average,minValue,maxValue);
}

public static String [] UserInputs(String []numsStringInput,int population)
{
for (int i = 0; i maxValue)
{
maxValue = arrayNumInt[i];
}
}
return maxValue;
}

public static int MinValueCalc(int []arrayNumInt,int population)
{
int minValue = arrayNumInt[0];
for(int i=1;i < population;i++)
{
if(arrayNumInt[i] < minValue)
{
minValue = arrayNumInt[i];
}
}
return minValue;
}

public static void Output(String listOfNums, int average, int minValue,int maxValue )
{
String text = "";

Solution

-
The Java standard dictates the use of the "Egyptian braces" style:

if (something) {
    // something...
}


-
In AvarageCalc():

int avarage = sum / population;
return avarage;


you can just return the calculation:

return sum / population;


Also, it should be spelled average. ;-)

-
In Output(), you don't need to declare text and then assign it later. Instead, initialize it where it's first used. Always try to keep variables as close in scope as possible, which can help keep your code cleaner and easier to maintain.

String text = messageText[0]+ "\n" + listOfNums;

Code Snippets

if (something) {
    // something...
}
int avarage = sum / population;
return avarage;
return sum / population;
String text = messageText[0]+ "\n" + listOfNums;

Context

StackExchange Code Review Q#54555, answer score: 5

Revisions (0)

No revisions yet.