*i need help with 3 different java programs* 1)/* ******************************
ID: 3801960 • Letter: #
Question
*i need help with 3 different java programs*
1)/* ******************************************************** The program prompts for a value which will determine the number of repetitions for the loop. Within the loop the program will prompt for a number and add the value value entered to an accumulator.******************************************************** */import java.util.Scanner;public class Lab0701 {
public static void main(String[] args) { int limit = 0; int counter = 0; int valu = 0; int sum = 0; float avg = 0.0F;// Prompt for a repetition limit Scanner kBd = new Scanner(System.in); System.out.print("Sum from 0 to...? "); limit = kBd.nextInt();// Develop the loop to sum the values between one (1) and the limit entered bythe user// while (conditional expression) {} System.out.println(" The sum of the numbers between 0 and " + limit + " is..." + sum); System.out.println(" **** end of Lab0701 **** ");}
2)
// The program will provide a count of// the upper case letters in the string// provided.
public class Lab0703 {
public static void main(String[] args) {
char c;int count = 0;int i = 0;
String lineIn = "AranDomlINeoFiNPutToTeSt"; System.out.println("Input: " + lineIn);
/* Note:
the while loop is controlled bythe length of the string */
while(i < lineIn.length()) { c = lineIn.charAt(i); if(Character.isUpperCase(c)) count++; i++; }
System.out.println("Count is: " + count); }
3)
import java.util.Scanner;public class Lab0704 {
public static void main(String[] args) {
int runTime = 0;int totalTime = 0;double avg = 0.0;int numTimes = 0;Scanner inp = new Scanner(System.in);
System.out.print(" Enter running time in seconds or -1 to end: ");runTime = inp.nextInt();while(runTime != -1){
totalTime += runTime;System.out.print(" Enter running time in seconds or -1 to
end: ");
runTime = inp.nextInt();
}
// insert code to report the total time, the number of times entered and theaverage time
}
Explanation / Answer
java program1
import java.util.Scanner;
public class Lab0701 {
public static void main(String[] args) {
int limit = 0;
int counter = 0; int valu = 0;
int sum = 0;
float avg = 0.0F;
// Prompt for a repetition limit
Scanner kBd = new Scanner(System.in);
System.out.print("Sum from 0 to...? ");
limit = kBd.nextInt();
// Develop the loop to sum the values between one (1) and the limit entered bythe user//
valu=limit;
while(valu>=1) {
sum+=valu;
valu--;
counter++;
}
System.out.println(" The sum of the numbers between 0 and " + limit + " is..." + sum);
System.out.println(" The number of repetitions for the loop is..."+counter);
System.out.println(" **** end of Lab0701 **** ");
}
}
In progarm1 value of limit which is taken by user assigned to another variable valu
In while loop put condition valu>=1, so that it can sum upto 1 from input given by user.
And within loop write counter++, to count number of repetitions loop.
Java Program2
public class Lab0703 {
public static void main(String[] args) {
char c;int count = 0;
int i = 0;
String lineIn = "AranDomlINeoFiNPutToTeSt";
System.out.println("Input: " + lineIn);
/* Note:
the while loop is controlled bythe length of the string */
while(i < lineIn.length()) {
c = lineIn.charAt(i);
if(Character.isUpperCase(c))
count++;
i++;
}
System.out.println("Count is: " + count); }}
In progarm2, you did almost correct, but only 2 changes you have to do to run successfully,
First, remove one curley braces after i++; statement
Second Add one curley braces at end of program.
Java Progarm 3
import java.util.Scanner;
public class Lab0704 {
public static void main(String[] args) {
int runTime = 0;
int totalTime = 0;
double avg = 0.0;
int numTimes = 0;
Scanner inp = new Scanner(System.in);
System.out.print(" Enter running time in seconds or -1 to end: ");
runTime = inp.nextInt();
while(runTime != -1){
numTimes++;
totalTime += runTime;
System.out.print(" Enter running time in seconds or -1 toend: ");
runTime = inp.nextInt();
}
// insert code to report the total time, the number of times entered and theaverage time
avg=(float)totalTime/numTimes;
System.out.println("The Total time is..."+totalTime);
System.out.println("The number of times entered by user is..."+numTimes);
System.out.println("The Average time is...:"+avg);
}
}
In program 3 add one statement within while loop i.e. numTimes++; to count the numbered of times entered by user
And after while loop add statement avg=(float)totalTime/numTimes;
to count average of times which is entered by user.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.