Java程序辅导

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

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
CS 1301 – Fall 2015 
Lab 7 
 
Working with Loops – Chapter 5 
 
Use JGrasp to complete the following programs and understand what they do. Some of these programs are 
similar to those you completed in Lab #6. 
 
 
Exercise 1: Write a Java program (called Practice_7_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_7_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.  
 
 
 
 
 
Next Page Î 
 
Page 1 of 2 
 
Page 2 of 2 
 
Exercise 3: Write a Java program (called Practice_7_3) that uses nested loops to print out the following 
shape. See Lab 6 and chapter 4 slides for related examples. 
 
 
         *  
        ***  
       *****  
      *******  
     *********  
    ***********  
   *************  
  ***************  
 
 
Exercise 4: Write a Java program (called Practice_7_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  
 
  
 
Submission: 
 
1.  Save all programs. 
2.  Check with your instructor for submission instructions.