CS141 Programming Assignment #4
|
|
- Sarah Nelson
- 6 years ago
- Views:
Transcription
1 Due Monday, Oct 31. CS141 Programming Assignment #4 1- Write a program to read in 10 numbers and compute the average, maximum and minimum values (using both while and for loop). Solution 1: * assignment 4 part 1 (using while loop) */ public class q1a Scanner input = new Scanner (System.in); int count =1, x, max, min, sum ; double avg ; System.out.println("please Enter 10 number :"); x= input.nextint (); // read the first number that entered max=x; min=x; sum=x; while (count < 10) x= input.nextint (); sum+=x ; if ( x > max) max=x; else if (x<min) min=x; count ++ ; avg = (sum /10.0) ; System.out.printf("The average = %f\n",avg); System.out.printf( "The max = %d\n", max); System.out.printf( "The min = %d\n", min); Solution 2: * assignment 4 part 1 (using For loop) */ public class q1b
2 Scanner input = new Scanner (System.in); int count, x, max, min, sum ; double avg=0 ; System.out.print ("please enter 10 numbers :"); x= input.nextint (); //read first number that entered max=x; min=x; sum=x; for (count =1 ;count <10;count ++ ) x= input.nextint (); sum+=x ; if ( x > max) max=x; else if (x< min ) min =x; avg = (sum /10.0); System.out.printf( "The avrage = %f\n",avg); System.out.printf( "The max = %d\n", max); System.out.printf( "The min = %d\n", min); 2- Write a program to read in numbers until the number -999 is encountered. The sum of all number read until this point should be printed out (using both while and for loop). Solution 1: * assignment 4 part 2 (using while loop) public class q2a Scanner input = new Scanner (System.in); int x,sum=0; ; System.out.print("please enter numbers to find its sum ( To stop enter -999): "); x = input.nextint(); while (x!= -999) sum+=x; x = input.nextint(); System.out.printf("The sum of the enterd numbers is : %d\n",sum); Solution 2:
3 * assignment 4 part 2 (using For loop) */ public class q2b Scanner input = new Scanner (System.in); int x, sum=0; System.out.print("please enter numbers to find there sum ( To stop enter -999): "); x = input.nextint(); for (int i =0; x!= -999; i ++ ) sum+=x; x = input.nextint(); System.out.printf("The sum of the enterd numbers is : %d\n",sum); 3- Write a program to count the vowels and letters in free text given as standard input. Read text a character at a time until you encounter end-of-data. Then print out the number of occurrences of each of the vowels a, e, i, o and u in the text, the total number of letters, and each of the vowels as an integer percentage of the letter total (using both while and for loop). Output format is: Numbers of characters: a 3 ; e 2 ; i 0 ; o 1 ; u 0 ; rest 17 Percentages of total: a 13%; e 8%; i 0%; o 4%; u 0%; rest 73% Solution 1: * assignment 4 part 3 (using while loop) public class q3a String line; int ctra = 0; int ctre = 0; int ctri = 0; int ctro = 0; int ctru = 0; int ctrrest = 0; int ctr = 0; int allchars; System.out.print("Enter your line of text: "); line = input.nextline();
4 line= line.tolowercase(); while (ctr < line.length()) if (line.charat(ctr) == 'a') ctra++; else if (line.charat(ctr) == 'e') ctre++; else if (line.charat(ctr) == 'i') ctri++; else if (line.charat(ctr) == 'o') ctro++; else if (line.charat(ctr) == 'u') ctru++; else ctrrest++; ctr++; allchars = ctra + ctre + ctri + ctro + ctru + ctrrest; System.out.println("Numbers of characters:"); System.out.printf("a %2d ; e %2d ; i %2d ; o %2d ; u %2d ; rest %2d\n", ctra, ctre, ctri, ctro, ctru, ctrrest); System.out.println("Percentages of total:"); System.out.printf("a %2d%%; e %2d%%; i %2d%%; o %2d%%; u %2d%%; rest %2d%%\n",(ctra*100/allChars), (ctre*100/allchars),(ctri*100/allchars), (ctro*100/allchars),(ctru*100/allchars), (ctrrest*100/allchars)); Solution 2: * assignment 4 part 3 (using For loop) */ public class q3b String line; int ctra = 0; int ctre = 0; int ctri = 0; int ctro = 0; int ctru = 0; int ctrrest = 0; int allchars; System.out.print("Enter your line of text: "); line = input.nextline(); line= line.tolowercase();
5 for (int i =0 ; i < line.length(); i ++) if (line.charat(i) == 'a') ctra++; else if (line.charat(i) == 'e') ctre++; else if (line.charat(i) == 'i') ctri++; else if (line.charat(i) == 'o') ctro++; else if (line.charat(i) == 'u') ctru++; else ctrrest++; allchars = ctra + ctre + ctri + ctro + ctru + ctrrest; System.out.println("Numbers of characters:"); System.out.printf("a %2d ; e %2d ; i %2d ; o %2d ; u %2d ; rest %2d\n", ctra, ctre, ctri, ctro, ctru, ctrrest); System.out.println("Percentages of total:"); System.out.printf("a %2d%%; e %2d%%; i %2d%%; o %2d%%; u %2d%%; rest %2d%%\n",(ctra*100/allChars), (ctre*100/allchars), (ctri*100/allchars), (ctro*100/allchars), (ctru*100/allchars), (ctrrest*100/allchars)); 4- Read a positive integer value, and compute the following sequence: If the number is even, halve it; if it's odd, multiply by 3 and add 1. Repeat this process until the value is 1, printing out each value. Finally print out how many of these operations you performed (using both while and for loop). Typical output might be: Initial value is 9 Next value is 28 Next value is 14 Next value is 7 Next value is 22 Next value is 11 Next value is 34 Next value is 17 Next value is 52 Next value is 26 Next value is 13 Next value is 40 Next value is 20 Next value is 10 Next value is 5 Next value is 16 Next value is 8 Next value is 4 Next value is 2 Final value 1, number of steps 19 if the input value is less than 1, print a message containing the word Error
6 Solution 1: * assignment 4 part 4 (using while loop) public class q4a int value; int ctr = 0; System.out.print("Enter a positive integer value: "); value = input.nextint(); if (value < 1) System.out.println("Error"); else System.out.printf("Initial value is %d\n", value); while (value > 1) if (value %2 == 0) value = value/2; else value = value * 3 + 1; System.out.printf("Next value is %d\n", value); ctr++; System.out.printf("Final value %d, number of steps %d\n", value, ctr); Solution 2: * assignment 4 part 4 (using For loop) */ public class q4b int i, value; System.out.print("Enter a positive integer value: "); value = input.nextint(); if (value < 1) System.out.println("Error"); else System.out.printf("Initial value is %d\n", value); for( i = 0 ; value > 1 ; i++)
7 if (value %2 == 0) value = value/2; else value = value * 3 + 1; System.out.printf("Next value is %d\n", value); System.out.printf("Final value %d, number of steps %d\n", value, i); 5- Write a class with methods to do the following output: a) Solution: * assignment 4 part 5 (a) public class q5 int z=5; int n=5; for(int i=n ;i>=1 ;i--) for(int j=n ;j>i ;j--) System.out.print( " " ); for(int k=1;k<=z;k++) System.out.print(i); z--; System.out.println( " " ); b) * * * * * * * * * * * * * * * * * * * * * * * * *
8 Solution: * assignment 4 part 5 (b) public class q5b for (int i = 5; i >= 1; i--) for (int j = 1; j <= i; j++) System.out.print(j+" "); for (int j = i; j < 5; j++) System.out.print("* "); for (int j = i; j < 4; j++) System.out.print("* "); for (int j = i; j >= 1; j--) if (j == 5) continue; System.out.print(j+" "); System.out.println(); for (int i = 2; i <= 5; i++) for (int j = 1; j <= i; j++) System.out.print(j+" "); for (int j = i; j < 5; j++) System.out.print("* "); for (int j = i; j < 4; j++) System.out.print("* "); for (int j = i; j >= 1; j--) if (j == 5) continue; System.out.print(j+" "); System.out.println(); c) Read odd integer x and the output depended on x for example: x = 5 x=7 * * *** *** ***** ***** *** ******* * ***** *** * Solution: * assignment 4 part 5 (c)
9 public class q5c System.out.print("please Enter odd integer: "); int x ; x=input.nextint(); if(x%2!= 0) for (int i = 1; i <=x; i+=2) for (int j = 1; j <= (x - i)/2; j++) System.out.print(" "); for (int j = 1; j <= i; j++) System.out.print("*"); System.out.println(); for (int i = x-2; i >= 1; i-=2) for (int j = 1; j <= (x - i)/2; j++) System.out.print(" "); for (int j = 1; j <= i; j++) System.out.print("*"); System.out.println(); else System.out.printf("Error!! %d is not odd!!", x); 6- Write a program that have a menu of 4 options: Please choose from the menu: 1- Print a number digits 2- Calculate the sum between two integers 3- Find all numbers divisible by a number in a given range 4- Exit Option 1 asks for a positive integer number n and prints its digits. Example: Please enter a positive integer: = Option 2 asks for two integers start and end then it finds the sum of all the integers between start and end. Example: Please enter the start and the end integers: 2 6 The sum for all numbers between 2 and 6 = 12
10 Option 3 asks for three integers start, end and a number then it finds those integers between start and end that are divisible by the number and prints them. Example: Please enter the start and the end integers: 1 10 Please enter the number you want to check for: 3 The numbers are divisible by 3 is : Option 4 Exit the program. Solution: * assignment 4 part 6 public class q6 int num,numcopy,places=0, factor=1; int choice; int sum=0; do System.out.println(); System.out.println("Please choose from the menu:\n"); System.out.println("1-Print the number's digits."); System.out.println("2-Calculate the sum between two integers."); System.out.println("3-Find all numbers divisible by a number in a given range"); System.out.println("4-Exit."); choice = input.nextint(); switch(choice) case 1: System.out.println("Please enter a positive integer: "); num = input.nextint(); numcopy= num; while(numcopy >0) numcopy/=10; places++; places--; while(places >0) factor*=10; places--; System.out.printf("%d=", num); while(num >0) numcopy =num % factor ; num/= factor; System.out.printf(" %d", num); factor/=10; num=numcopy; System.out.println(); break; case 2: int start, end;
11 System.out.println("Please enter the start and the end integers:"); start = input.nextint(); end = input.nextint(); for (int i = start+1; i < end; i++) sum += i; System.out.printf("The sum for all numbers between %d and % d = %d\n",start, end, sum); break; case 3: int x,y,z; System.out.println("Please enter the start,end and a number:"); x = input.nextint(); y= input.nextint(); z= input.nextint(); System.out.printf("The numbers divisible by %d are : /n", z); for (int i = x ; i < y; i++) if (i % z ==0) System.out.printf("%d ", i); break; case 4: System.out.println("Bye!!"); break; default:system.out.println("invalid input. Try again!!"); while (choice!= 4); 7- Write a class that reads 10 integers. The program should calculate and print the sum of even and odd numbers. You program should have three methods: - Read method that will be responsible for reading the numbers. - IsOdd a Boolean function that checks if a number is odd or not. - IsEven a Boolean function that checks if a number is even or not. Example: Please enter 10 integers: The sum of odd elements is: 25 The sum of even elements is: 30 Solution: * assignment 4 part 7 public class q7 int sumeven =0, sumodd = 0; System.out.println("Please enter 10 integers: ");
12 for (int i = 1; i <= 10; i++) int num = input.nextint(); if(num%2==0) sumeven+=num; else sumodd+=num; System.out.printf("The sum of odd elements is: %d\n",sumodd); System.out.printf("The sum of even elements is: %d\n",sumeven); 8- Write a class that reads 8 integers and finds the largest and the smallest numbers. The main should print the large and small numbers. Example: Please enter 8 integers: The largest number is: 11 The smallest number is: -6 Solution: * assignment 4 part 8 public class q8 int large,small; System.out.println("Please enter 8 integers:"); int num = input.nextint(); large = small = num; for (int i = 2; i <=8; i++) num = input.nextint(); if(num >large) large = num; else if (num < small) small = num; System.out.printf("The largest number is: %d\n", large); System.out.printf("The smallest number is: %d\n", small);
CS141 Programming Assignment #8
CS141 Programming Assignment #8 Due Sunday, April 14th. 1- Write a class with methods to do the following output: a) 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1 b) 1 2 3 4 5 4 3 2 1 1 2 3 4 * 4 3 2 1 1 2 3 * * * 3 2
More informationCS141 Programming Assignment #6
CS141 Programming Assignment #6 Due Sunday, Nov 18th. 1) Write a class with methods to do the following output: a) 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1 b) 1 2 3 4 5 4 3 2 1 1 2 3 4 * 4 3 2 1 1 2 3 * * * 3 2 1
More informationCS141 Programming Assignment #5
CS141 Programming Assignment #5 Due Wednesday, Nov 16th. 1) Write a class that asks the user for the day number (0 to 6) and prints the day name (Saturday to Friday) using switch statement. Solution 1:
More informationComputer Programming, I. Laboratory Manual. Experiment #6. Loops
Think Twice Code Once The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2005 Khaleel I. Shaheen Computer Programming, I Laboratory Manual Experiment #6
More informationMidterm Examination (MTA)
M105: Introduction to Programming with Java Midterm Examination (MTA) Spring 2013 / 2014 Question One: [6 marks] Choose the correct answer and write it on the external answer booklet. 1. Compilers and
More informationAL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I
AL GHURAIR UNIVERSITY College of Computing CSC 209 JAVA I week 2- Arithmetic and Decision Making: Equality and Relational Operators Objectives: To use arithmetic operators. The precedence of arithmetic
More informationChoose 3 of the 1 st 4 questions (#'s 1 through 4) to complete. Each question is worth 12 points.
Choose 3 of the 1 st 4 questions (#'s 1 through 4) to complete. Each question is worth 12 points. Use the remaining question as extra credit (worth 1/2 of the points earned). Specify which question is
More informationIntroduction to Java Applications
2 Introduction to Java Applications OBJECTIVES In this chapter you will learn: To write simple Java applications. To use input and output statements. Java s primitive types. Basic memory concepts. To use
More informationLoops. Eng. Mohammed Abdualal. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Department
Islamic University of Gaza Faculty of Engineering Computer Engineering Department Computer Programming Lab (ECOM 2114) Created by Eng: Mohammed Alokshiya Modified by Eng: Mohammed Abdualal Lab 6 Loops
More informationwhile (/* array size less than 1*/){ System.out.print("Number of students is invalid. Enter" + "number of students: "); /* read array size again */
import java.util.scanner; public class CourseManager1 { public static void main(string[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter number of students: "); /* read the number
More informationAP CS Unit 3: Control Structures Notes
AP CS Unit 3: Control Structures Notes The if and if-else Statements. These statements are called control statements because they control whether a particular block of code is executed or not. Some texts
More informationAP COMPUTER SCIENCE A
AP COMPUTER SCIENCE A CONTROL FLOW Aug 28 2017 Week 2 http://apcs.cold.rocks 1 More operators! not!= not equals to % remainder! Goes ahead of boolean!= is used just like == % is used just like / http://apcs.cold.rocks
More informationExam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:
كلية الحاسبات وتقنية المعلوما Exam 2 Programming I (CPCS 202) Instructor: M. G. Abbas Malik Date: November 22, 2015 Student Name: Student ID: Total Marks: 40 Obtained Marks: Instructions: Do not open this
More informationCSE 20 Intro to Computing I. Coding
CSE 0 Intro to Computing I Coding ChooseFunc.java for (int i = ; i
More information16. Give a detailed algorithm for making a peanut butter and jelly sandwich.
COSC120FinalExamReview2010 1. NamethetwotheoreticalmachinesthatCharlesBabbagedeveloped. 2. WhatwastheAntikytheraDevice? 3. Givethecodetodeclareanintegervariablecalledxandthenassignitthe number10. 4. Givethecodetoprintout
More informationCS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2009
CS 102 - Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2009 What is your name?: There are two sections: I. True/False..................... 60 points; ( 30 questions, 2 points each) II.
More informationCS Computers & Programming I Review_01 Dr. H. Assadipour
CS 101 - Computers & Programming I Review_01 Dr. H. Assadipour 1. What is the output of this program? public class Q_01 public static void main(string [] args) int x=8; int y=5; double z=12; System.out.println(y/x);
More informationJava Classes: Random, Character A C S L E C T U R E 6
Java Classes: Random, Character A C S - 1903 L E C T U R E 6 Random An instance of the Random can be used to generate a stream of random values Typical process: 1. Create a Random object 2. Use the object
More informationJAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly using a while, do while and for loop
Islamic University of Gaza Faculty of Engineering Computer Engineering Dept. Computer Programming Lab (ECOM 2114) ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly
More information1. A company has vans to transport goods from factory to various shops. These vans fall into two categories: 50 items 150 items
If..Else Statement Exercises 1. A company has vans to transport goods from factory to various shops. These vans fall into two categories: Category Normal Duty Van Heavy Duty Van Capacity 50 items 150 items
More informationCOE 212 Engineering Programming. Welcome to Exam II Thursday April 21, Instructors: Dr. Salim Haddad Dr. Joe Tekli Dr. Wissam F.
1 COE 212 Engineering Programming Welcome to Exam II Thursday April 21, 2016 Instructors: Dr. Salim Haddad Dr. Joe Tekli Dr. Wissam F. Fawaz Name: Student ID: Instructions: 1. This exam is Closed Book.
More informationWrite a program which converts all lowercase letter in a sentence to uppercase.
Write a program which converts all lowercase letter in a sentence to uppercase. For Example: 1) Consider a sentence "welcome to Java Programming!" its uppercase conversion is " WELCOME TO JAVA PROGRAMMING!"
More informationMenu Driven Systems. While loops, menus and the switch statement. Mairead Meagher Dr. Siobhán Drohan. Produced by:
Menu Driven Systems While loops, menus and the switch statement Produced by: Mairead Meagher Dr. Siobhán Drohan Department of Computing and Mathematics http://www.wit.ie/ Topics list while loops recap
More informationObject Oriented Programming. Java-Lecture 1
Object Oriented Programming Java-Lecture 1 Standard output System.out is known as the standard output object Methods to display text onto the standard output System.out.print prints text onto the screen
More informationCOSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand
COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com
More informationSTUDENT LESSON A12 Iterations
STUDENT LESSON A12 Iterations Java Curriculum for AP Computer Science, Student Lesson A12 1 STUDENT LESSON A12 Iterations INTRODUCTION: Solving problems on a computer very often requires a repetition of
More informationMotivation of Loops. Loops. The for Loop (1) Learning Outcomes
Motivation of Loops Loops EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG We may want to repeat the similar action(s) for a (bounded) number of times. e.g., Print
More informationIntroduction to Java Applications
2 What s in a name? that which we call a rose By any other name would smell as sweet. William Shakespeare When faced with a decision, I always ask, What would be the most fun? Peggy Walker Take some more
More informationLoops. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG
Loops EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG Learning Outcomes Understand about Loops : Motivation: Repetition of similar actions Two common loops: for
More informationHandout 5 cs180 - Programming Fundamentals Spring 15 Page 1 of 8. Handout 5. Loops.
Handout 5 cs180 - Programming Fundamentals Spring 15 Page 1 of 8 Handout 5 Loops. Loops implement repetitive computation, a k a iteration. Java loop statements: while do-while for 1. Start with the while-loop.
More informationITERATION WEEK 4: EXMAPLES IN CLASS
Monday Section 2 import java.util.scanner; public class W4MSection2 { ITERATION WEEK 4: EXMAPLES IN CLASS public static void main(string[] args) { Scanner input1 = new Scanner (System.in); int CircleCenterX
More informationCS110 Programming Language I. Lab 6: Multiple branching Mechanisms
CS110 Programming Language I Lab 6: Multiple branching Mechanisms Computer Science Department Fall 2016 Lab Objectives: In this lab, the student will practice: Using switch as a branching mechanism Lab
More informationUniversity of Cape Town Department of Computer Science. Computer Science CSC115F
University of Cape Town Department of Computer Science Computer Science CSC115F Class Test 2 Solutions Wednesday 6 April 2005 Marks: 40 Approximate marks per question are shown in brackets Time: 40 minutes
More informationM105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014
M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014 Question One: Choose the correct answer and write it on the external answer booklet. 1. Java is. a. case
More informationProgramming with Java
Programming with Java Data Types & Input Statement Lecture 04 First stage Software Engineering Dep. Saman M. Omer 2017-2018 Objectives q By the end of this lecture you should be able to : ü Know rules
More informationIntroduction to Computer Science Unit 3. Programs
Introduction to Computer Science Unit 3. Programs This section must be updated to work with repl.it Programs 1 to 4 require you to use the mod, %, operator. 1. Let the user enter an integer. Your program
More informationConditionals. For exercises 1 to 27, indicate the output that will be produced. Assume the following declarations:
Conditionals For exercises 1 to 27, indicate the output that will be produced. Assume the following declarations: final int MAX = 25, LIMIT = 100; int num1 = 12, num2 = 25, num3 = 87; 1. if (num1 < MAX)
More informationAL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I
AL GHURAIR UNIVERSITY College of Computing CSC 209 JAVA I week 3- Control Statements: Part I Objectives: To use the if and if...else selection statements to choose among alternative actions. To use the
More informationComputer Programming. Decision Making (2) Loops
Computer Programming Decision Making (2) Loops Topics The Conditional Execution of C Statements (review) Making a Decision (review) If Statement (review) Switch-case Repeating Statements while loop Examples
More informationExample: Monte Carlo Simulation 1
Example: Monte Carlo Simulation 1 Write a program which conducts a Monte Carlo simulation to estimate π. 1 See https://en.wikipedia.org/wiki/monte_carlo_method. Zheng-Liang Lu Java Programming 133 / 149
More informationChapter 4: Control structures. Repetition
Chapter 4: Control structures Repetition Loop Statements After reading and studying this Section, student should be able to Implement repetition control in a program using while statements. Implement repetition
More informationCS Introduction to Programming Midterm Exam #1 - Prof. Reed Spring 2010
CS 102 - Introduction to Programming Midterm Exam #1 - Prof. Reed Spring 2010 What is your name?: There are two sections: I. True/False..................... 60 points; ( 30 questions, 2 points each) II.
More informationBasic Problem solving Techniques Top Down stepwise refinement If & if else.. While.. Counter controlled and sentinel controlled repetition Usage of
Basic Problem solving Techniques Top Down stepwise refinement If & if.. While.. Counter controlled and sentinel controlled repetition Usage of Assignment increment & decrement operators 1 ECE 161 WEEK
More informationSystem.out.printf("Please, enter the value of the base : \n"); base =input.nextint();
CS141 Programming Assignment #6 Due Thursday, Dec 1st. 1) Write a method integerpower(base, exponent) that returns the value of base exponent For example, integerpower(3, 4) calculates 34 (or 3 * 3 * 3
More information2.8. Decision Making: Equality and Relational Operators
Page 1 of 6 [Page 56] 2.8. Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. This section introduces a simple version of Java's if statement
More informationMotivation of Loops. Loops. The for Loop (1) Learning Outcomes
Motivation of Loops Loops EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG We may want to repeat the similar action(s) for a (bounded) number of times. e.g., Print the Hello World message
More informationCS111: PROGRAMMING LANGUAGE II
CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 1(c): Java Basics (II) Lecture Contents Java basics (part II) Conditions Loops Methods Conditions & Branching Conditional Statements A
More informationLoops. EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG
Loops EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG Learning Outcomes Understand about Loops : Motivation: Repetition of similar actions Two common loops: for and while Primitive
More informationCombined Assignment Operators. Flow of Control. Increment Decrement Operators. Operators Precedence (Highest to Lowest) Slide Set 05: Java Loops
Flow of control Flow of Control Program instruction execution sequence Sequential Control Structure Selection (Branching) Control Structure Repetition (Loop) Control Structure Operator Usage Relational
More informationTutorial # 4. Q1. Evaluate the logical (Boolean) expression in the following exercise
Tutorial # 4 Q1. Evaluate the logical (Boolean) expression in the following exercise 1 int num1 = 3, num2 = 2; (num1 > num2) 2 double hours = 12.8; (hours > 40.2) 3 int funny = 7; (funny!= 1) 4 double
More informationComputer programming Code exercises [1D Array]
Computer programming-140111014 Code exercises [1D Array] Ex#1: //Program to read five numbers, from user. public class ArrayInput public static void main(string[] args) Scanner input = new Scanner(System.in);
More information(c) ((!(a && b)) == (!a!b)) TRUE / FALSE. (f) ((!(a b)) == (!a &&!b)) TRUE / FALSE. (g) (!(!a) && (c-d > 0) && (b!b))
ComS 207: Programming I Midterm 2, Tue. Mar 21, 2006 Student Name: Student ID Number: Recitation Section: 1. True/False Questions (10 x 1p each = 10p) Determine the value of each boolean expression given
More informationExample. Write a program which sums two random integers and lets the user repeatedly enter a new answer until it is correct.
Example Write a program which sums two random integers and lets the user repeatedly enter a new answer until it is correct. 1... 2 Scanner input = new Scanner(System.in); 3 int x = (int) (Math.random()
More informationChapter 4: Control structures
Chapter 4: Control structures Repetition Loop Statements After reading and studying this Section, student should be able to Implement repetition control in a program using while statements. Implement repetition
More informationCS111: PROGRAMMING LANGUAGE II
1 CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 1: Introduction Lecture Contents 2 Course info Why programming?? Why Java?? Write once, run anywhere!! Java basics Input/output Variables
More information1 class Lecture4 { 2 3 "Loops" / References 8 [1] Ch. 5 in YDL 9 / Zheng-Liang Lu Java Programming 125 / 207
1 class Lecture4 { 2 3 "Loops" 4 5 } 6 7 / References 8 [1] Ch. 5 in YDL 9 / Zheng-Liang Lu Java Programming 125 / 207 Loops A loop can be used to make a program execute statements repeatedly without having
More informationWeb-CAT submission URL: CAT.woa/wa/assignments/eclipse
King Saud University College of Computer & Information Science CSC111 Lab10 Arrays II All Sections ------------------------------------------------------------------- Instructions Web-CAT submission URL:
More informationControl Statements: Part 1
4 Let s all move one place on. Lewis Carroll Control Statements: Part 1 The wheel is come full circle. William Shakespeare How many apples fell on Newton s head before he took the hint! Robert Frost All
More informationC212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.
C212 Early Evaluation Exam Mon Feb 10 2014 Name: Please provide brief (common sense) justifications with your answers below. 1. What is the type (and value) of this expression: 5 * (7 + 4 / 2) 2. What
More informationJava Assignment 3: Loop Practice Ver 3.0 Last Updated: 12/1/2015 8:57 AM
Java Assignment 3: Loop Practice Ver 3.0 Last Updated: 12/1/2015 8:57 AM Let s get some practice creating programs that repeat commands inside of a loop in order to accomplish a particular task. You may
More informationIntroduction to Computer Science Unit 2. Notes
Introduction to Computer Science Unit 2. Notes Name: Objectives: By the completion of this packet, students should be able to describe the difference between.java and.class files and the JVM. create and
More informationWelcome1.java // Fig. 2.1: Welcome1.java // Text-printing program.
1 Welcome1.java // Fig. 2.1: Welcome1.java // Text-printing program. public class Welcome1 // main method begins execution of Java application System.out.println( "Welcome to Java Programming!" ); } //
More informationdownload instant at
2 Introduction to Java Applications: Solutions What s in a name? That which we call a rose By any other name would smell as sweet. William Shakespeare When faced with a decision, I always ask, What would
More informationArray basics. How would you solve this? Arrays. What makes the problem hard? Array auto-initialization. Array declaration. Readings: 7.
How would you solve this? Array basics Readings:. Consider the following program: How many days' temperatures? Day 's high temp: Day 's high temp: Day 's high temp: Day 's high temp: Day 's high temp:
More informationBuilding Java Programs
Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1 self-check: Ch. 4 #2; Ch. 5 # 1-10 exercises: Ch. 4 #2, 4, 5, 8; Ch. 5 # 1-2 Copyright 2009
More informationLecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18
Assignment Lecture 9 Logical Operations Formatted Print Printf Increment and decrement Read through 3.9, 3.10 Read 4.1. 4.2, 4.3 Go through checkpoint exercise 4.1 Logical Operations - Motivation Logical
More informationChapter 3. Selections
Chapter 3 Selections 1 Outline 1. Flow of Control 2. Conditional Statements 3. The if Statement 4. The if-else Statement 5. The Conditional operator 6. The Switch Statement 7. Useful Hints 2 1. Flow of
More informationQuestion: Total Points: Score:
CS 170 Exam 1 Section 000 Spring 2014 Name (print):. Instructions Keep your eyes on your own paper and do your best to prevent anyone else from seeing your work. Do NOT communicate with anyone other than
More informationFLOW CONTROL. Author: Boaz Kantor The Interdisciplinary Center, Herzliya Introduction to Computer Science Winter Semester
Author: Boaz Kantor The Interdisciplinary Center, Herzliya Introduction to Computer Science Winter 2008-9 Semester FLOW CONTROL Flow Control Hold 2 balls in left hand, 1 ball in right Throw ball from left
More informationLoops. Repeat after me
Loops Repeat after me Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements repeat is determined by the value of a control variable,
More informationCAT.woa/wa/assignments/eclipse
King Saud University College of Computer & Information Science CSC111 Lab10 Arrays II All Sections ------------------------------------------------------------------- Instructions Web-CAT submission URL:
More informationJump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.
Jump Statements The keyword break and continue are often used in repetition structures to provide additional controls. break: the loop is terminated right after a break statement is executed. continue:
More informationOct Decision Structures cont d
Oct. 29 - Decision Structures cont d Programming Style and the if Statement Even though an if statement usually spans more than one line, it is really one statement. For instance, the following if statements
More informationAssignment 2.4: Loops
Writing Programs that Use the Terminal 0. Writing to the Terminal Assignment 2.4: Loops In this project, we will be sending our answers to the terminal for the user to see. To write numbers and text to
More informationCSCE 145 Exam 1 Review Answers. This exam totals to 100 points. Follow the instructions. Good luck!
CSCE 145 Exam 1 Review Answers This exam totals to 100 points. Follow the instructions. Good luck! Chapter 1 This chapter was mostly terms so expect a fill in the blank style questions on definition. Remember
More informationBuilding Java Programs
Building Java Programs Chapter 7 Lecture 7-3: Arrays for Tallying; Text Processing reading: 7.6, 4.3 A multi-counter problem Problem: Write a method mostfrequentdigit that returns the digit value that
More informationArrays OBJECTIVES. In this chapter you will learn:
7 Arrays Now go, write it before them in a table, and note it in a book. Isaiah 30:8 To go beyond is as wrong as to fall short. Confucius Begin at the beginning, and go on till you come to the end: then
More informationCSE 1223: Introduction to Computer Programming in Java Chapter 3 Branching
CSE 1223: Introduction to Computer Programming in Java Chapter 3 Branching 1 Flow of Control The order in which statements in a program are executed is called the flow of control So far we have only seen
More informationAhmadu Bello University Department of Mathematics First Semester Examinations June 2014 COSC211: Introduction to Object Oriented Programming I
Ahmadu Bello University Department of Mathematics First Semester Examinations June 2014 COSC211: Introduction to Object Oriented Programming I Attempt Four questions Time: 120 mins 1. Examine the following
More information1. Find the output of following java program. class MainClass { public static void main (String arg[])
1. Find the output of following java program. public static void main(string arg[]) int arr[][]=4,3,2,1; int i,j; for(i=1;i>-1;i--) for(j=1;j>-1;j--) System.out.print(arr[i][j]); 1234 The above java program
More informationLesson 14 - Activity 1
13 Lesson 14 - Activity 1 / Term 1: Lesson 14 Coding Activity 1 Test if an integer is not between 5 and 76 inclusive. Sample Run 1 Enter a number: 7 False Sample Run 2 Enter a number: 1 True / class Lesson_14_Activity_One
More informationCS141 Programming Assignment #10
CS141 Programming Assignment #10 Due Sunday, May 5th. 1) Write a class with the following methods: a) max( int [][] a) Returns the maximum integer in the array. b) min(int [][] a) Returns the minimum integer
More informationCS 170 Exam 2. Version: A Spring Name (as in OPUS) (print): Instructions:
CS 170 Exam 2 Version: A Spring 2016 Name (as in OPUS) (print): Section: Seat Assignment: Instructions: Keep your eyes on your own paper and do your best to prevent anyone else from seeing your work. Do
More informationEXCEPTION HANDLING. // code that may throw an exception } catch (ExceptionType parametername) {
EXCEPTION HANDLING We do our best to ensure program correctness through a rigorous testing and debugging process, but that is not enough. To ensure reliability, we must anticipate conditions that could
More informationChapter 4 Loops. int x = 0; while ( x <= 3 ) { x++; } System.out.println( x );
Chapter 4 Loops Sections Pages Review Questions Programming Exercises 4.1 4.7, 4.9 4.10 104 117, 122 128 2 9, 11 13,15 16,18 19,21 2,4,6,8,10,12,14,18,20,24,26,28,30,38 Loops Loops are used to make a program
More informationIntroduction to Computer Science Unit 2. Notes
Introduction to Computer Science Unit 2. Notes Name: Objectives: By the completion of this packet, students should be able to describe the difference between.java and.class files and the JVM. create and
More informationArray basics. Readings: 7.1
Array basics Readings: 7.1 1 How would you solve this? Consider the following program: How many days' temperatures? 7 Day 1's high temp: 45 Day 2's high temp: 44 Day 3's high temp: 39 Day 4's high temp:
More information1 Short Answer (10 Points Each)
1 Short Answer (10 Points Each) 1. Write a for loop that will calculate a factorial. Assume that the value n has been input by the user and have the loop create n! and store it in the variable fact. Recall
More informationWentworth Institute of Technology. Engineering & Technology WIT COMP1000. Methods
WIT COMP1000 Methods Methods Programs can be logically broken down into a set of tasks Example from horoscope assignment:» Get input (month, day) from user» Determine astrological sign based on inputs
More informationCS Week 5. Jim Williams, PhD
CS 200 - Week 5 Jim Williams, PhD The Study Cycle Check Am I using study methods that are effective? Do I understand the material enough to teach it to others? http://students.lsu.edu/academicsuccess/studying/strategies/tests/studying
More informationJava. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures
Chapter 5: Control Structures II Java Programming: Program Design Including Data Structures Chapter Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,
More information3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)
Structures Programming in C++ Sequential Branching Repeating Loops (Repetition) 2 1 Loops Repetition is referred to the ability of repeating a statement or a set of statements as many times this is necessary.
More informationTopic 23 arrays - part 3 (tallying, text processing)
Topic 23 arrays - part 3 (tallying, text processing) "42 million of anything is a lot." -Doug Burger (commenting on the number of transistors in the Pentium IV processor) Copyright Pearson Education, 2010
More informationGlossary. (Very) Simple Java Reference (draft, v0.2)
(Very) Simple Java Reference (draft, v0.2) (Very) Simple Java Reference (draft, v0.2) Explanations and examples for if and for (other things to-be-done). This is supposed to be a reference manual, so we
More informationSubject: PIC Chapter 2.
02 Decision making 2.1 Decision making and branching if statement (if, if-, -if ladder, nested if-) Switch case statement, break statement. (14M) 2.2 Decision making and looping while, do, do-while statements
More informationAssignment Checklist. Prelab Activities. Lab Exercises. Labs Provided by Instructor. Postlab Activities. Section:
7 Arrays Now go, write it before them in a table, and note it in a book. Isaiah 30:8 To go beyond is as wrong as to fall short. Confucius Begin at the beginning, and go on till you come to the end: then
More informationCSC 113 Tutorial 8. Interfaces and Exception handling
CSC 113 Tutorial 8 Interfaces and Exception handling Chargable {Interface + salarywithbenefits(days: int) : double + display() : void AuditDepartment + AuditDepartment() + addemployee(c: Chargable) : boolean
More informationFull file at
Chapter 1 Primitive Java Weiss 4 th Edition Solutions to Exercises (US Version) 1.1 Key Concepts and How To Teach Them This chapter introduces primitive features of Java found in all languages such as
More informationCS110: PROGRAMMING LANGUAGE I
CS110: PROGRAMMING LANGUAGE I Computer Science Department Lecture 4: Java Basics (II) A java Program 1-2 Class in file.java class keyword braces {, } delimit a class body main Method // indicates a comment.
More informationReview for Test 1 (Chapter 1-5)
Review for Test 1 (Chapter 1-5) 1. Introduction to Computers, Programs, and Java a) What is a computer? b) What is a computer program? c) A bit is a binary digit 0 or 1. A byte is a sequence of 8 bits.
More information