Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
 CS 1301 
Lab 6 
 
Working with Loops – Chapter 5 
 
Use JGrasp to complete, compile, and run the following programs selected for this lab. These programs use 
Scanner class for inputs. They illustrate the use of Java loops. Understand what each loop does, and keep these 
programs for future reference. 
 
//***************************************************************** 
// Average.java Author: Lewis/Loftus 
// Demonstrates the use of a while loop with a sentinel value. 
//***************************************************************** 
import java.text.DecimalFormat; 
import java.util.Scanner; 
public class Average 
{ 
//----------------------------------------------------------------- 
// Computes the average of a set of values entered by the user. 
// The running sum is printed as the numbers are entered. 
//----------------------------------------------------------------- 
public static void main (String[] args) 
{ 
int sum = 0, value, count = 0; 
double average; 
Scanner scan = new Scanner (System.in); 
System.out.print ("Enter an integer (0 to quit): "); 
value = scan.nextInt(); 
 
while (value != 0) // enter sentinel value of 0 to terminate loop 
{ 
count = count + 1; 
sum = sum + value; 
System.out.println ("The sum so far is " + sum); 
System.out.print ("Enter an integer (0 to quit): "); 
value = scan.nextInt(); 
} 
 
System.out.println (); 
if (count == 0) 
System.out.println ("No values were entered."); 
else 
{ 
average = (double)sum / count; 
DecimalFormat fmt = new DecimalFormat ("0.###"); 
System.out.println ("The average is " + fmt.format(average)); 
} 
} 
} 
 
 
 
 
 
 
 
//******************************************************************** 
// WinPercentage.java Author: Lewis/Loftus 
// Demonstrates the use of a while loop for input validation. 
//******************************************************************** 
import java.text.NumberFormat; 
import java.util.Scanner; public 
class WinPercentage 
{ 
//----------------------------------------------------------------- 
// Computes the percentage of games won by a team. 
//----------------------------------------------------------------- 
 public static void main (String[] args) 
{ 
final int NUM_GAMES = 12; 
int won; 
double ratio; 
Scanner scan = new Scanner (System.in); 
System.out.print ("Enter the number of games won (0 to " 
+ NUM_GAMES + "): "); 
won = scan.nextInt(); 
 
while (won < 0 || won > NUM_GAMES) 
{ 
System.out.print ("Invalid input. Please reenter: "); 
won = scan.nextInt(); 
} 
 
ratio = (double)won / NUM_GAMES; 
NumberFormat fmt = NumberFormat.getPercentInstance(); 
System.out.println (); 
System.out.println ("Winning percentage: " + fmt.format(ratio)); 
} 
} 
 
 
 
 
 
 
 
 
 
//******************************************************************** 
// ReverseNumber.java Author: Lewis/Loftus 
// Demonstrates the use of a do loop. 
//******************************************************************** 
import java.util.Scanner; 
public class ReverseNumber 
{ 
//----------------------------------------------------------------- 
// Reverses the digits of an integer mathematically. 
//----------------------------------------------------------------- 
public static void main (String[] args) 
{ 
int number, lastDigit, reverse = 0; Scanner scan = new 
Scanner (System.in); System.out.print ("Enter a 
positive integer: "); number = scan.nextInt(); 
 
do 
{ 
lastDigit = number % 10; 
reverse = (reverse * 10) + lastDigit; 
number = number / 10; 
} while (number > 0); 
 
System.out.println ("That number reversed is " + reverse); 
} 
} 
 
 
 
 
 
 
 
//******************************************************************** 
// Multiples.java Author: Lewis/Loftus 
// Demonstrates the use of a for loop. 
//******************************************************************** 
import java.util.Scanner; 
public class Multiples 
 { 
//----------------------------------------------------------------- 
// Prints multiples of a user-specified number up to a user- 
// specified limit. 
//----------------------------------------------------------------- 
public static void main (String[] args) 
{ 
final int PER_LINE = 5; 
int value, limit, mult, count = 0; Scanner scan = 
new Scanner (System.in); System.out.print ("Enter a 
positive value: "); value = scan.nextInt(); 
System.out.print ("Enter an upper limit: "); 
limit = scan.nextInt(); 
System.out.println (); 
System.out.println ("The multiples of " + value + " between " + 
value + " and " + limit + " (inclusive) are:"); 
 
for (mult = value; mult <= limit; mult = mult + value) 
{ 
System.out.print (mult + "\t"); 
// Print a specific number of values per line of output 
count = count +1; 
if (count % PER_LINE == 0) 
System.out.println(); 
} 
} 
} 
 
 
 
 
 
 
//******************************************************************** 
// Stars.java Author: Lewis/Loftus 
// Demonstrates the use of nested for loops. 
//******************************************************************** 
public class Stars 
{ 
//----------------------------------------------------------------- 
// Prints a triangle shape using asterisk (star) characters. 
//----------------------------------------------------------------- 
public static void main (String[] args) 
{ 
final int MAX_ROWS = 10; 
 
for (int row = 1; row <= MAX_ROWS; row = row + 1) 
{ 
for (int star = 1; star <= row; star = star + 1) 
System.out.print ("*"); 
 
System.out.println(); 
} 
} 
} 
 
 
Now, use the above programs for guidance to complete the following practice programs 
 
For each lab program you develop during this course, make sure that include the following header - replace the 
dots in the header with your section #, semester, your full name, your instructor’s name, and lab #: 
 
// Class: CS 1301/12 
// Term: ... 
// Name: ... 
// Instructor: ... 
// Lab#: ... 
 
 
 Exercise 1: Write a Java program (called Practice_6_1) that uses while loops to read an integer number 
between 20 and 60, then the program prints out the entered value followed by (on a separate line) the sum of all 
even integer values between 2 and the entered number (including the entered value if even). Use a while loop to 
validate the user input before determining and printing the sum. In addition, design your program such it allows 
the user to re-run the program with a different input. Use proper labels for the outputs as shown in the examples 
below. 
 
 
Entered value: 20 
Sum of even numbers between 2 and 20 is: 110 
 
Entered value: 25 
Sum of even numbers between 2 and 25 is: 156 
 
Entered value: 30 
Sum of even numbers between 2 and 30 is: 240 
 
Entered value: 40 
Sum of even numbers between 2 and 40 is: 420 
 
Entered value: 60 
Sum of even numbers between 2 and 60 is: 930 
 
 
 
Exercise 2: Write a Java program (called Practice_6_2) to read a sentence (as a string) from the user, the 
program then prints out the entered sentence. Then, print out the sentence one word per line using the space 
(blank) character as are delimiter. Use proper labels for the outputs as shown in the examples below. Design 
your program such it allows the user to re-run the program with a different input. 
 
Entered String: This is a test input, and can be a longer string too. 
Word #1: This 
Word #2: is 
Word #3: a 
Word #4: test 
Word #5: input, 
Word #6: and 
Word #7: can 
Word #8: be 
Word #9: a 
Word #10:   longer 
Word #11:   string 
Word #12:   too. 
 
 
 
Exercise 3: Write a Java program (called Practice_6_3) that uses nested loops to print out the following 
shape.  
 
 
* 
*** 
***** 
******* 
********* 
*********** 
************* 
*************** 
 
 
 Exercise 4: Write a Java program (called Practice_6_4) that reads a string from the user, and then prints out 
the count of each letter (regardless of case) as shown below. Note that you need to use separate counter for 
each letter, that is 26 counters, named as aCount, bCount, cCount, dCount, …. Print out only the 
counters that count the letters used in the string (that is, counters with values > 0 only). Do not printer counters 
with 0 values. Use proper labels for the outputs as shown in the examples below. 
 
 
Entered String: This is a test input. 
 
Letter A: 1 
Letter E: 1 
Letter H: 1 
Letter I: 3 
Letter N: 1 
Letter P: 1 
Letter S: 3 
Letter T: 4 
Letter U: 1 
 
 
 
Instructions: 
 
1. Programs must be working correctly. 
2. Programs must be completed and checked by TA before submitting to D2L. 
3. Programs must be checked during the designated lab session.