JAVA EXPERT NEEDED
CSE 110 - ASSIGNMENT # 4 – Fall 2015
Due: Tuesday October 6 by 10:00AM Maximum Points: 20 pts Topics
• Loops (Chapter 4)
Note: Your programming assignments require individual work and effort to be of any benefit. Every student must work independently on his or her assignments. This means that every student must ensure that neither a soft copy nor a hard copy of their work gets into the hands of another student. Sharing your assignments with others in any way is NOT permitted.
Violations of the University Academic Integrity policy will not be ignored. The university academic integrity policy is found at http://www.asu.edu/studentaffairs/studentlife/judicial/academic_integrity.htm
Use the following Guidelines: • Give identifiers semantic meaning and make them easy to read (examples numStudents,
grossPay, etc). • Keep identifiers to a reasonably short length. • User upper case for constants. Use title case (first letter is upper case) for classes. Use lower
case with uppercase word separators for all other identifiers (variables, methods, objects). • Use tabs or spaces to indent code within blocks (code surrounded by braces). This includes
classes, methods, and code associated with ifs, switches and loops. Be consistent with the number of spaces or tabs that you use to indent.
• Use white space to make your program more readable. • Reasonably good amount of comments should be added in your program so that it is easy
for other people to understand it. Please see the comment style in the textbook.
Important Note: All submitted assignments must begin with the descriptive comment block. To avoid losing trivial points, make sure this comment header is included in every assignment you submit, and that it is updated accordingly from assignment to assignment. (If not, -1 Pt)
//*********************************************************** // Name: your name // Title: title of the source file // Author: (if not you, put the name of author here) // Description: Write the description in your words. // Time spent: how long it took you to complete the assignment // Date: the date you programmed //**********************************************************
Part 1: Writing Exercise: (5 pts) The following are the exercises about the loops in Java. Write the answers in a comment block before the code of Part2.
a. What are the three required expressions of a for-loop? (1 pts)
b. Consider the following code. This code was written to generate the output as shown in Output 2. However, there is an error in this code. Correct the code so that it is able to generate the desired output. (2 pts)
int count = 0; while (count < 10) {
System.out.println("count:" + count); }
Output 2 count:0 count:1 count:2 count:3 count:4 count:5 count:6 count:7 count:8 count:9
c. What is the output of the following code? (2 pts) int i =0; do { System.out.println("i is : " + i); i++; }while(i < 5);
Note: The answers to the questions (a through c) above should be typed in the block of comments in the java file such as;
//a) The three requirements of a loop are … //b) The corrected code is … //c) The output of the given code is …
Part 2: Programming (15 pts) Write a Java program called Assignment4.java. There are two tasks in this assignment. Your program will ask for user choice based on which it will switch between the two tasks. After performing the computations of the selected task, your program should ask the user if the user wants to do another execution. If the user opts to continue, then your program should loop again and display the option for two tasks, else it should terminate. (3 pts) Read the instructions carefully about each task. The development process of each task is decomposed into two steps. Follow the instruction one by one. Don't go to the next step if your program does not return the proper output in a step. Task 1: Generate a multiplication table.
Step1 : The first step is to read input from the user. The input should be a single line with two positive numbers such as "4 3". The inputs are separated by a space. Make sure the user inputs a positive integer for the number and table-Size, or else prompt the user again to do so (as shown in Output 1). (3 pts)
Output 1: Please enter a number and tableSize: -3 4 Please enter a number and tableSize:
Step2 : Generate a multiplication table for the entered number of the required tableSize. Your output should be well formatted (as shown in Output 2). You might want to make use of tab spaces for formatting. (3 pts)
Output 2: Please enter a number and tableSize: 8 9 1 * 8 = 8 2 * 8 = 16 3 * 8 = 24 4 * 8 = 32 5 * 8 = 40 6 * 8 = 48 7 * 8 = 56 8 * 8 = 64 9 * 8 = 72
Task 2: Display a pattern of stars ‘*’ and lower case character ‘o’ in a proper format as suggested below.
Step1 : The first step is to read the user input. If the user input is invalid (not integer or not positive), the program displays the message below and break out of the switch-case (as shown in Output 3 and 4). *** Invalid number of rows *** (3 Pts)
Output 3:
Please enter number of rows: -6 *** Invalid number of rows *** Output 4:
Please enter number of rows: Q *** Invalid number of rows ***
Step2: Create nested loops to print out a pattern of stars and lower case ‘o’. Your output should be well formatted (as shown in Output 5). (3 Pts)
Output 5:
Please enter number of rows: 9
* *o*
*o*o* *o*o*o*
*o*o*o*o* *o*o*o*o*o*
*o*o*o*o*o*o* *o*o*o*o*o*o*o*
*o*o*o*o*o*o*o*o*
Example Execution: The following are example inputs and outputs. The user inputs are shown in bold. Example Execution #1
1. Generate a multiplication table 2. Display a pattern of stars and character ‘o’
Your choice: 1 Please enter a number and tableSize: 3 4 1 * 3 = 3 2 * 3 = 6 3 * 3 = 9 4 * 3 = 12
Do you want to continue? (Yes/No): Yes 1. Generate a multiplication table 2. Display a pattern of stars and character ‘o’
Your choice: Example Execution #2
1. Generate a multiplication table 2. Display a pattern of stars and character ‘o’
Your choice: 2 Please enter number of rows: 6
* *o*
*o*o* *o*o*o*
*o*o*o*o* *o*o*o*o*o*
Do you want to continue? (Yes/No): No
Some Useful Methods for completing this assignment: Class : Scanner
Methods : next(), nextLine(), nextInt(), hasNextInt() Class : String
Methods: equals(), equalsIgnoreCase() Use only the Java statements that have been covered in class to date. DO NOT use any other items out of the Chapter 1- 4 (Array, Array List, exit() etc.). If in doubt, ask. If you use them, then you lose the points of task. Complete each step one by one. Don't copy any code developed by others. Don't give your code to others. Don't use any algorithm, which you cannot understand. Your assignment file is checked by the MOSS (by Stanford Univ.), which is a program to detect cheatings.
Submit your homework by following the instructions below:
*********************************************************************************
• Go to the course web site (my.asu.edu), and then click on the on-line Submission tab. Login to the page by typing your email and password
• Submit your Assignment4.java file on-line. Make sure to choose Hw4 from drop-down box.
• Assignment4.java should have the following, in order: o In comments, the assignment Header described in "Important Note" o Answer to questions in Part 1 o The working Java code for Part 2 o The Assignment4.java file must compile and run as you submit it. You can
confirm this by viewing your submission results.
Important Note: You may resubmit as many times as you like until the deadline, but we will only mark your last submission.
NO LATE ASSIGNMENTS WILL BE ACCEPTED.
- Topics
- Loops (Chapter 4)
- Use the following Guidelines:
- Important Note:
- Part 1: Writing Exercise: (5 pts)
- Part 2: Programming (15 pts)