CSC 1302 Lab 3: Loops and User Input Practice solution

$24.99

Category:

Description

5/5 - (8 votes)

CSC 1302 Principles of Computer Science II
Purpose:
1. Continue to practice using for-loop in Java programming. Loops can execute a block of code
as long as a specified condition is reached. When you know exactly how many times you
want to loop through a block of code, for-loop can help you to save time, reduce errors and
make your code more readable.
2. Using scanner class in Java to practice reading input from Java console. The main purpose of
the Scanner class is to parse primitive composes and strings utilizing general expressions, in
any case, it can utilize to peruse contribution from the client in the order line.
In this assignment, you will practice how to use for-loop with parameters in Java and how to use
the Scanner class and when to sentinel values.
Task1:
Write a java program that will print out following pattern. Use nested for loop.
Sample Expected Output of Task 1:
Task2:
Write a java program which asks the user to input multiple numbers and a negative number will
stop allowing user input. Then the program computes and printouts the average all the
non-negative number inputted from the user.
Partial Samples for Task1:
//Name: Chengzong Peng
//PantherID:000-000-000
//Due Date: June 12
//=======================================
public class Lab_03{
//method for printing out the request pattern
public static void main(String[] args) {
PrintNum();
}
public static void PrintNum()
{
//for-loop to separate the different features of different lines
for()
{
for()
{
System.out.println();
}
System.out.println(“\n”);
//if statement to state if the number of line is greater than 3, then we can directly print out the result
if()
{
System.out.println(“\n”);
}
}
}
}
Partial Samples for Task2:
//Name: Chengzong Peng
//PantherID:000-000-000
//Due Date: June 12
//=======================================
import java.util.Scanner;
public class Lab_3_task2 {
public static void main(String[] args) {
//declare the object and initialize with predefined standard input object
Scanner scan =
System.out.println(“Enter your integer (use ‘return’ key to separate each): “);
//initialize the sum and count of input elements
double num = ;
double sum = ;
double count = ;
//check if an integer value is available
while () {
//read an int value
}
double mean =;
//print the values to check if the input was correctly obtained
System.out.println(“The average is: ” + mean);
}
}
Criteria:
1. Upload all of the .java and the .class files to the CSc1302 dropbox on http://
icollege.gsu.edu.
2. Your assignment will be graded based on the following criteria: (a) Are your programs
runnable without errors? (b) Do your programs complete the tasks with specified outputs?
(c) Do you follow the specified rules to define your methods and programs? (d) Do you
provide necessary comments include the programmer information, date, title of the
program and brief description of the program.
3. Please comment the important lines in the .java file as shown in the template. The
important lines including but not limited to i) variables, ii) for-loop, iii) while-loop, iv)
if-else statement, iv) methods. Please use your own words to describe what is your
purpose to write this line. A .java file without comment will be graded under a 40%
penalty.
4. Make sure that both the .java and .class files are named and uploaded to icollege
correctly. If any special package is used in the program, be sure to upload the package
too. Should you use any other subdirectory (whatsoever) your program would not be
graded, and you will receive a 0 (zero).
5. No copying allowed. If it is found that students copy from each other, all of these
programs will get 0.