***If the user chooses 3 program should prompt for and read up to 10 negative do
ID: 3542575 • Letter: #
Question
***If the user chooses 3 program should prompt for and read up to 10 negative doubles into an array. The user will indicate she is finished by entering a positive number. (This positive number should not be stored in your array)//FINISHED WITH THAT IN HOWMANYTIMES**
HELP WITH THIS PART--
Stuck on- - - You should then ask the user for a target value. Use a helper method to print out the array, the target and the number of times the target appears in the array, along with appropriate messages.
public static void howManyTimes()
{
Scanner lKeyboard = new Scanner(System.in);
double[] negDouble = new double [10];
double lNumEntry=0.00;
int lNumCounter=0;
int lArrayValueCount=0;
double targetValue=0.00;
//poputlate array
do
{
lNumEntry = getValidUserEntryNegativeDouble(lNumEntry);
negDouble [lNumCounter] = lNumEntry;
if (lNumEntry < 0)
{
negDouble [lNumCounter] = lNumEntry;
}
lNumCounter++;
} while (lNumEntry < 0);
lArrayValueCount = lNumCounter;
isItThere(negDouble, targetValue);
}
public static boolean isItThere(double [] lArray, double lTargetValue) //THIS IS MY HELPER METHOD
{
Scanner lKeyboard = new Scanner(System.in);
int lCounter=0;
boolean ans=false;
int lTargetValueCount=0;
System.out.println("Please enter your target value");
lTargetValue=lKeyboard.nextDouble();
lArray [lCounter] = lTargetValue;
for(lCounter=0; lCounter < lArray.length; lCounter++)
{
if (lTargetValue==lArray[lCounter])
ans=true;
return lTargetValueCount++;
}
return ans;
}
}
//close method howManyTimes
//end TestProg3
Explanation / Answer
import java.util.*;
public class TestProg3
{
public static void main(String[] args)
{
howManyTimes();
}
public static void howManyTimes()
{
Scanner lKeyboard = new Scanner(System.in);
double[] negDouble = new double [10];
double lNumEntry=0.00;
int lNumCounter=0;
int lArrayValueCount=0;
double targetValue=0.00;
//poputlate array
while(true)
{
System.out.println("Enter a negative value (postivie value to end ):");
lNumEntry = lKeyboard.nextInt();
if(lNumEntry > 0 || lNumCounter==9) break;
else
negDouble [lNumCounter] = lNumEntry;
lNumCounter++;
}
lArrayValueCount = lNumCounter;
isItThere(negDouble, targetValue);
}
public static boolean isItThere(double [] lArray, double lTargetValue) //THIS IS MY HELPER METHOD
{
Scanner lKeyboard = new Scanner(System.in);
int lCounter=0;
boolean ans=false;
int lTargetValueCount=0;
System.out.println("Please enter your target value :");
lTargetValue=lKeyboard.nextDouble();
int count=0;
for(lCounter=0; lCounter < lArray.length; lCounter++)
{
if (lTargetValue==lArray[lCounter])
{
ans=true;
count++;
}
}
System.out.println(lTargetValue + (ans?" found in array ":" not found in array ") + count + " times");
System.out.println("Array contents are :");
for(lCounter=0; lCounter < lArray.length; lCounter++)
{
if(lArray[lCounter]!=0)
System.out.printf("%.2f ",lArray[lCounter]);
}
return ans;
}
}
//close method howManyTimes
//end TestProg3
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.