Lesson 14 - Activity 1

Size: px
Start display at page:

Download "Lesson 14 - Activity 1"

Transcription

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 //Declare a scanner and input an int. Scanner scan = new Scanner(System.in); System.out.println("Please enter an integer:"); int n = scan.nextint(); //If n is not in the range, print True. if (!( n >= 5 && n <= 76)) System.out.println("True"); //Otherwise, print False. else System.out.println("False"); APCS Unit 2 Activity Guide Version 1.0 Edhesive

2 14 Lesson 14 - Activity 2 / Term 1: Lesson 14 Coding Activity 2 Write a program to input two integers and print "Both are positive or zero." to the screen, if both are positive or zero. Print "One or both are negative." otherwise. / class Lesson_14_Activity_Two //Declare a Scanner and input two integers. Scanner scan = new Scanner(System.in); System.out.println("Please enter two integers:"); int x = scan.nextint(); int y = scan.nextint(); //Use if-else to produce the necessary output. if(x >= 0 && y >= 0) System.out.println("Both are positive or zero."); else System.out.println("One or both are negative."); APCS Unit 2 Activity Guide Version 1.0 Edhesive

3 15 Lesson 14 - Activity 3 / Term 1: Lesson 14 Coding Activity 3 The Internet runs on web addresses.the addresses we type represent the IP address for each site and how the computer finds an individual web page. IP addresses are made up of four numbers, each between 0 and 255 separated by a period. For example, is an IP address. Write a program to enter four numbers and test if they make up a valid IP address. In other words, test to see if the numbers entered are between 0 and 255 inclusive. Sample Run 1 Please enter the first octet: 898 Please enter the second octet: 34 Please enter the third octet: 712 Please enter the fourth octet: 45 Octet 1 is incorrect Octet 3 is incorrect Sample Run 2 Please enter the first octet: 112 Please enter the second octet: 200 Please enter the third octet: 0 Please enter the fourth octet: 254 IP Address: / class Lesson_14_Activity_Three //Declare a Scanner and input four octets. Scanner scan = new Scanner(System.in); APCS Unit 2 Activity Guide Version 1.0 Edhesive

4 16 System.out.println("Please enter the first octet: "); int o1 = scan.nextint(); System.out.println("Please enter the second octet: "); int o2 = scan.nextint(); System.out.println("Please enter the third octet: "); int o3 = scan.nextint(); System.out.println("Please enter the fourth octet: "); int o4 = scan.nextint(); //Set up a flag variable for correct input. int correct = 1; //Check octet 1. if (!(o1 >= 0 && o1 <= 255)) System.out.println("Octet 1 is incorrect"); correct = 0; //Check octet 2. if (!(o2 >= 0 && o2 <= 255)) System.out.println("Octet 2 is incorrect"); correct = 0; //Check octet 3. if (!(o3 >= 0 && o3 <= 255)) System.out.println("Octet 3 is incorrect"); correct = 0; // Check octet 4. if (!(o4 >= 0 && o4 <= 255)) System.out.println("Octet 4 is incorrect"); correct = 0; //Check the flag for a correct IP address. if (correct == 1) System.out.println("IP Address: " + o1 + "." + o2 + "." + o3 + "." + o4); APCS Unit 2 Activity Guide Version 1.0 Edhesive

5 17 Lesson 17 - Activity 1 / Term 1: Lesson 17 Coding Activity 1 Write a program that will input a list of test scores in from the keyboard. When the user enters -1, print the average. What do you need to be careful about when using -1 to stop a loop? Sample Run: Enter the Scores: The average is: 72.5 / class Lesson_17_Activity_One //Declare a Scanner and prompt for scores. Scanner scan = new Scanner(System.in); System.out.println("Enter the Scores: "); //Input the first score and declare sum and count variables. int test = scan.nextint(); int sum = 0; int c = 0; //While the input is not -1, increase the count, //add to the sum, and read the next input. while (test!= -1) sum += test; c++; test = scan.nextint(); //Calculate and print the average. System.out.println("The average is: " + 1.0sum/c); APCS Unit 2 Activity Guide Version 1.0 Edhesive

6 18 Lesson 17 - Activity 2 / Term 1: Lesson 17 Coding Activity 2 Ask the user for two numbers. Print only the even numbers between them, you should also print the two numbers if they are even. Sample Run 1: Enter two numbers: Sample Run 2: Enter two numbers: / class Lesson_17_Activity_Two //Declare a Scanner and input two numbers. System.out.println("Enter two numbers: "); Scanner scan = new Scanner(System.in); int a = scan.nextint(); int b = scan.nextint(); //Create a new variable, start, which is a rounded //up to the nearest even number. int start = a + (a%2); //While start is in range, print it and increase by 2. while (start <= b) System.out.print(start + " "); start += 2; APCS Unit 2 Activity Guide Version 1.0 Edhesive

7 19 Lesson 20 - Activity 1 / Term 1: Lesson 20 Coding Activity Computer science jobs are in demand. Right now we have a shortage of people that can do computer programming, and one of the fastest growing areas of new jobs in the sector are so-called hybrid jobs. This means you specialize in an area like biology, and then use computer programming to do your job. These hybrid jobs exist in the arts, sciences, economics, healthcare, and entertainment fields. One of these jobs is computational biology. Computational Biology, sometimes referred to as bioinformatics, is the science of using biological data to develop algorithms and relations among various biological systems. In this lab we are going to investigate the data from a grey seal named Gracie. We ll input the longitude and latitude data from a tracking device. We want to investigate the farthest north, south, east and west Gracie has been. We will use the latitude to measure this. Write a program to enter Gracie s longitude and Latitude data. Each time through the loop it should ask if you want to continue. Enter 1 to repeat, 0 to stop. Any value for latitude not between -90 and 90 inclusive should be ignored. Any value for longitude not between -180 and 180 inclusive should be ignored. Sample Run: Please enter the latitude: Please enter the longitude: Would you like to enter another location? 1 Please enter the latitude: Please enter the longitude: Would you like to enter another location? 1 Please enter the latitude: Please enter the longitude: APCS Unit 2 Activity Guide Version 1.0 Edhesive

8 20 Would you like to enter another location? 1 Please enter the latitude: 300 Please enter the longitude: Incorrect Latitude or Longitude Please enter the latitude: Please enter the longitude: Would you like to enter another location? 0 Farthest North: Farthest South: Farthest East: Farthest West: / class Lesson_20_Activity //Declare a Scanner. Scanner scan = new Scanner(System.in); //Set a flag variable, rep, to control the loop. int rep = 1; //Set up temporary variables to store the current location. double lo = 0; double la = 0; //Set up a max and min for latitude and longitude. double maxlat = -90; double minlat = 90; double maxlon = -180; double minlon = 180; APCS Unit 2 Activity Guide Version 1.0 Edhesive

9 21 //While rep == 1, continue the loop. while (rep == 1) //Input a lat and long value. System.out.println("Please enter the latitude: "); la = scan.nextdouble(); System.out.println("Please enter the longitude: "); lo = scan.nextdouble(); //If the values are invalid, print an error, //and continue the loop. if (!(la >= -90 && la <= 90)!(lo >= -180 && lo <= 180)) System.out.println( "Incorrect Latitude or Longitude"); //Otherwise, check for a new max or min and ask the //user if they would like to continue. else if(la > maxlat) maxlat = la; if(la < minlat) minlat = la; if(lo > maxlon) maxlon = lo; if(lo < minlon) minlon = lo; //while System.out.println( "Would you like to enter another location? "); rep = scan.nextint(); //Print the results. System.out.println("Farthest North: " + maxlat); System.out.println("Farthest South: " + minlat); System.out.println("Farthest East: " + maxlon); System.out.println("Farthest West: " + minlon); APCS Unit 2 Activity Guide Version 1.0 Edhesive

10 1 Lesson 22 - Activity 1 / Term 1: Lesson 22 Coding Activity 1 Write the code to take a String and print it with one letter per line. Sample run: Enter a string: bought b o u g h t / class Lesson_22_Activity_One //Declare a Scanner and input a String. Scanner scan = new Scanner(System.in); System.out.println("Enter a string:"); String h = scan.nextline(); //Loop through the String, printing each character. int i = 0; while (i < h.length()) System.out.println(h.charAt(i)); i++; APCS Unit 3 Activity Guide Version 1.0 Edhesive

11 2 Lesson 22 - Activity 2 / Term 1: Lesson 22 Coding Activity 2 Write the code to take a String and print it diagonally. Sample run: Enter a string: bought b o u g h t Use a tab character for every four spaces in the sample. Hint: You may need more than one loop. / class Lesson_22_Activity_Two //Declare a Scanner and input a String. Scanner scan = new Scanner(System.in); System.out.println("Enter a string:"); String h = scan.nextline(); //For each character, use a loop to print tabs //so that the ith character is preceded by //i tabs. int i = 0; while (i < h.length()) int j = 0; while(j < i) System.out.print("\t"); j++; System.out.println(h.charAt(i)); i++; APCS Unit 3 Activity Guide Version 1.0 Edhesive

12 3 Lesson 24 - Activity 1 / Term 1: Lesson 24 Coding Activity 1 Use a for loop to print all of the numbers from 23 to 89, with 10 numbers on each line. Print one space between each number. / class Lesson_24_Activity_One //Loop from 23 to 89. for (int i = 23; i <= 89; i++) //Print each number followed by a space. System.out.print(i + " "); //Use % to print a new line every 10 numbers. if( i % 10 == 2) System.out.println(); Lesson 24 - Activity 2 / Term 1: Lesson 24 Coding Activity 2 Use a for loop to print the even numbers between 1 and 50. Print each number on a new line. / class Lesson_24_Activity_Two //Loop through the numbers from 1 to 50. for (int i = 1; i <= 50; i++) //If a number is even, print it. if (i%2 == 0) System.out.println(i); APCS Unit 3 Activity Guide Version 1.0 Edhesive

13 4 Lesson 24 - Activity 3 / Term 1: Lesson 24 Coding Activity 3 Input an int between 0 and 100 and print the numbers between it and 100. If the number is not between 0 and 100 print "error". Print 20 numbers per line. Sample Run 1: Enter a number between 0 and 100: Sample Run 2: Enter a number between 0 and 100: 105 error / class Lesson_24_Activity_Three //Declare a Scanner and input a number. Scanner scan = new Scanner(System.in); System.out.println("Enter a number between 0 and 100"); int x = scan.nextint(); //If x is out of range, print error. if( x < 0 x > 100) System.out.println("error"); //Otherwise, use a for loop to print every number from x to 100. //Use modular division to print 20 numbers per line. else for(int i = x; i <= 100; i++) System.out.print(i + " "); if(i%20 == 0) System.out.println(); APCS Unit 3 Activity Guide Version 1.0 Edhesive

14 5 Lesson 29 - Activity 1 / Term 1: Lesson 29 Coding Activity 1 A student wants an algorithm to find the hardest spelling word in a list of vocabulary. They define hardest by the longest word. Write the code to find the longest word stored in an array of Strings called list. If several words have the same length it should print the first word in list with the longest length. For example, if the following list were declared: String list [] = "high", "every", "nearing", "checking", "food ", "stand", "value", "best", "energy", "add", "grand", "notation", "abducted", "food ", "stand"; It would print: checking / class Lesson_29_Activity_One / Fill this list with values that will be useful for you to test. A good idea may be to copy/paste the list in the example above. Do not make any changes to this list in your main method. You can print values from list, but do not add or remove values to this variable. / public static String [] list = "This","is","a","test","list"; //Declare a variable to store the location of the longest String. int longest = 0; //Loop through the list, searching for a String longer than //longest. for(int i = 0; i < list.length; i++) if(list[i].length() > list[longest].length()) longest = i; //Print the longest value in the list. System.out.println(list[longest]); APCS Unit 3 Activity Guide Version 1.0 Edhesive

15 6 Lesson 29 - Activity 2 / Term 1: Lesson 29 Coding Activity 2 Write a loop that processes an array of strings. Each String should be printed backwards on its own line. For example, if the list contains: "every", "nearing", "checking", "food", "stand", "value" It should output: yreve gniraen gnikcehc doof dnats eulav / class Lesson_29_Activity_Two / Fill this list with values that will be useful for you to test. A good idea may be to copy/paste the list in the example above. Do not make any changes to this list in your main method. You can print values from list, but do not add or remove values to this variable. / public static String [] list = "siht","si","a","tset","tsil"; //Use a for loop to access each String in the list. for(int i = 0; i < list.length; i++) //Loop through each String backwards, printing the //characters. for(int j = list[i].length() - 1; j >= 0; j--) System.out.print(list[i].charAt(j)); //print a new line after each String. System.out.println(); APCS Unit 3 Activity Guide Version 1.0 Edhesive

16 7 Lesson 30 - Activity 1 / Term 1: Lesson 30 Coding Activity Due to a problem with a scanner an array of words was created with spaces in incorrect places. Write the code to process the list of words and trim any spaces out of the words. So if the list contains: "every", " near ing ", " checking", "food ", "stand", "value " It should be changed to hold: "every", "nearing", "checking", "food", "stand", "value" Note that this activity does not require you to print anything. Your code should end with the array list still declared and containing the resulting words. / class Lesson_30_Activity / Your code should end with the following array modified as the instructions above specify. You may modify the elements in this list but make sure you do not add or remove anything from it. / public static String [] list = "Th is"," is","a ","t es t","li st"; //Loop through the list to access each String. for(int i = 0; i < list.length; i++) //Declare a new String to include only the non-space //characters. String tmp = ""; //For each character in the current String, if it is not //a space, add it to the temporary String, tmp. for(int j = 0; j < list[i].length(); j++) if( list[i].charat(j)!= ' ') tmp += list[i].charat(j); //Set the current String to the value of tmp. list[i] = tmp; APCS Unit 3 Activity Guide Version 1.0 Edhesive

17 8 Lesson Activity 1 / Term 1: Lesson 1011 Coding Activity Input a String to represent the octal number and translate to the base ten number. The octal number must be 8 digits or less. Your program should also check that all the digits are 0-7, then translate the number to base ten. Sample Run 1: Enter a number in base 8: 1287 ERROR: Incorrect Octal Format Sample Run 2: Enter a number in base 8: Sample Run 3: Enter a number in base 8: ERROR: Incorrect Octal Format / class Lesson_1011_Activity public static void main (String str[]) //Set up a Scanner and input a base 8 number as a String. Scanner scan = new Scanner (System.in); System.out.println("Enter a number in base 8: "); String oct1 = scan.nextline(); //Use a loop to check for valid input. for (int i = 0; i < oct1.length(); ++i) //Check for invalid ith character. if(i >= 8!(oct1.charAt(i) >= '0' && oct1.charat(i) <= '7')) //Print an error and return. System.out.println("ERROR: Incorrect Octal Format"); return; APCS Unit 3 Activity Guide Version 1.0 Edhesive

18 9 //Declare an int to store the value of our base 8 number. int a = 0; //Get the highest power of 8 int highestpower = oct1.length()-1; //use a loop to sum the value of each digit, multiplied //by 8 to the power of that digit. for (int i = 0; i < oct1.length(); ++i) a += ((oct1.charat(i)) - 48) Math.pow(8, highestpower-i); //Print the result. System.out.println(a); APCS Unit 3 Activity Guide Version 1.0 Edhesive

Lesson 14 - Activity 1

Lesson 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 information

Term 1 Unit 1 Week 1 Worksheet: Output Solution

Term 1 Unit 1 Week 1 Worksheet: Output Solution 4 Term 1 Unit 1 Week 1 Worksheet: Output Solution Consider the following what is output? 1. System.out.println("hot"); System.out.println("dog"); Output hot dog 2. System.out.print("hot\n\t\t"); System.out.println("dog");

More information

Introduction to Computer Science Unit 2. Exercises

Introduction to Computer Science Unit 2. Exercises Introduction to Computer Science Unit 2. Exercises Note: Curly brackets { are optional if there is only one statement associated with the if (or ) statement. 1. If the user enters 82, what is 2. If the

More information

COMP 202 Java in one week

COMP 202 Java in one week CONTENTS: Basics of Programming Variables and Assignment Data Types: int, float, (string) Example: Implementing a calculator COMP 202 Java in one week The Java Programming Language A programming language

More information

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

More information

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

More information

COMP 202. Java in one week

COMP 202. Java in one week COMP 202 CONTENTS: Basics of Programming Variables and Assignment Data Types: int, float, (string) Example: Implementing a calculator Java in one week The Java Programming Language A programming language

More information

Oct Decision Structures cont d

Oct 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 information

Chapter 3. Selections

Chapter 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 information

STUDENT LESSON A7 Simple I/O

STUDENT LESSON A7 Simple I/O STUDENT LESSON A7 Simple I/O Java Curriculum for AP Computer Science, Student Lesson A7 1 STUDENT LESSON A7 Simple I/O INTRODUCTION: The input and output of a program s data is usually referred to as I/O.

More information

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG Selections EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG Learning Outcomes The Boolean Data Type if Statement Compound vs. Primitive Statement Common Errors

More information

COE 211/COE 212 Computer/Engineering Programming. Welcome to Exam II Thursday December 20, 2012

COE 211/COE 212 Computer/Engineering Programming. Welcome to Exam II Thursday December 20, 2012 1 COE 211/COE 212 Computer/Engineering Programming Welcome to Exam II Thursday December 20, 2012 Instructor: Dr. George Sakr Dr. Wissam F. Fawaz Dr. Maurice Khabbaz Name: Student ID: Instructions: 1. This

More information

Exam 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. 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 information

Iteration: Intro. Two types of loops: 1. Pretest Condition precedes body Iterates 0+ times. 2. Posttest Condition follows body Iterates 1+ times

Iteration: Intro. Two types of loops: 1. Pretest Condition precedes body Iterates 0+ times. 2. Posttest Condition follows body Iterates 1+ times Iteration: Intro Two types of loops: 1. Pretest Condition precedes body Iterates 0+ times 2. Posttest Condition follows body Iterates 1+ times 1 Iteration: While Loops Pretest loop Most general loop construct

More information

LAB 12: ARRAYS (ONE DIMINSION)

LAB 12: ARRAYS (ONE DIMINSION) Statement Purpose: The purpose of this Lab. is to practically familiarize student with the concept of array and related operations performed on array. Activity Outcomes: Student will understand the concept

More information

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018 Motivating Examples (1.1) Selections EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG 1 import java.util.scanner; 2 public class ComputeArea { 3 public static void main(string[] args)

More information

CS 101 Spring 2007 Midterm 2 Name: ID:

CS 101 Spring 2007 Midterm 2 Name:  ID: You only need to write your name and e-mail ID on the first page. This exam is CLOSED text book, closed-notes, closed-calculator, closed-neighbor, etc. Questions are worth different amounts, so be sure

More information

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2 CONTENTS: Compilation Data and Expressions COMP 202 More on Chapter 2 Programming Language Levels There are many programming language levels: machine language assembly language high-level language Java,

More information

Introduction to Computer Science, Shimon Schocken, IDC Herzliya. Lectures Control Structures

Introduction to Computer Science, Shimon Schocken, IDC Herzliya. Lectures Control Structures Introduction to Computer Science, Shimon Schocken, IDC Herzliya Lectures 3.1 3.2 Control Structures Control Structures, Shimon Schocken IDC Herzliya, www.intro2cs.com slide 1 Control structures A program

More information

Computer Applications Answer Key Class IX December 2017

Computer Applications Answer Key Class IX December 2017 Computer Applications Answer Key Class IX December 2017 Question 1. a) What are the default values of the primitive data type int and float? Ans : int = 0 and float 0.0f; b) Name any two OOP s principle.

More information

COE 212 Engineering Programming. Welcome to the Final Exam Monday May 18, 2015

COE 212 Engineering Programming. Welcome to the Final Exam Monday May 18, 2015 1 COE 212 Engineering Programming Welcome to the Final Exam Monday May 18, 2015 Instructors: Dr. Joe Tekli Dr. George Sakr Dr. Wissam F. Fawaz Name: Student ID: Instructions: 1. This exam is Closed Book.

More information

Web-CAT submission URL: CAT.woa/wa/assignments/eclipse

Web-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 information

while (/* array size less than 1*/){ System.out.print("Number of students is invalid. Enter" + "number of students: "); /* read array size again */

while (/* 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 information

Computer Programming, I. Laboratory Manual. Experiment #6. Loops

Computer 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 information

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 24, Name: KEY 1

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 24, Name: KEY 1 CSC 1051 Algorithms and Data Structures I Midterm Examination February 24, 2014 Name: KEY 1 Question Value Score 1 10 2 10 3 10 4 10 5 10 6 10 7 10 8 10 9 10 10 10 TOTAL 100 Please answer questions in

More information

Example Program. public class ComputeArea {

Example Program. public class ComputeArea { COMMENTS While most people think of computer programs as a tool for telling computers what to do, programs are actually much more than that. Computer programs are written in human readable language for

More information

Arrays in Java Using Arrays

Arrays in Java Using Arrays Recall Length of an Array Arrays in Java Using Arrays Once an array has been created in memory, its size is fixed for the duration of its existence. Every array object has a length field whose value can

More information

Building Java Programs

Building 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 information

CSE 20. SAMPLE FINAL Version A Time: 180 minutes. The following precedence table is provided for your use:

CSE 20. SAMPLE FINAL Version A Time: 180 minutes. The following precedence table is provided for your use: CSE 20 SAMPLE FINAL Version A Time: 180 minutes Name The following precedence table is provided for your use: Precedence of Operators ( ) - (unary),!, ++, -- *, /, % +, - (binary) = = =,!= &&

More information

Module Contact: Dr Gavin Cawley, CMP Copyright of the University of East Anglia Version 1

Module Contact: Dr Gavin Cawley, CMP Copyright of the University of East Anglia Version 1 UNIVERSITY OF EAST ANGLIA School of Computing Sciences Main Series UG Examination 2017-18 PROGRAMMING 1 CMP-4008Y Time allowed: 2 hours Answer FOUR questions. All questions carry equal weight. Notes are

More information

Chapter 4: Control Structures I

Chapter 4: Control Structures I Chapter 4: Control Structures I Java Programming: From Problem Analysis to Program Design, Second Edition Chapter Objectives Learn about control structures. Examine relational and logical operators. Explore

More information

Problem Solving With Loops

Problem Solving With Loops To appreciate the value of loops, take a look at the following example. This program will calculate the average of 10 numbers input by the user. Without a loop, the three lines of code that prompt the

More information

CS141 Programming Assignment #6

CS141 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 information

Programming with Java

Programming 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 information

CAT.woa/wa/assignments/eclipse

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 information

Final. Your Name CS Fall 2014 December 13, points total Your Instructor and Section

Final. Your Name CS Fall 2014 December 13, points total Your Instructor and Section Final Your Name CS 1063 - Fall 2014 December 13, 2014 100 points total Your Instructor and Section I. (10 points, 1 point each) Match each of the terms on the left by choosing the upper case letter of

More information

CMPT 125: Lecture 4 Conditionals and Loops

CMPT 125: Lecture 4 Conditionals and Loops CMPT 125: Lecture 4 Conditionals and Loops Tamara Smyth, tamaras@cs.sfu.ca School of Computing Science, Simon Fraser University January 17, 2009 1 Flow of Control The order in which statements are executed

More information

COE 212 Engineering Programming. Welcome to Exam II Monday May 13, 2013

COE 212 Engineering Programming. Welcome to Exam II Monday May 13, 2013 1 COE 212 Engineering Programming Welcome to Exam II Monday May 13, 2013 Instructors: Dr. Randa Zakhour Dr. Maurice Khabbaz Dr. George Sakr Dr. Wissam F. Fawaz Name: Solution Key Student ID: Instructions:

More information

LAB 13: ARRAYS (ONE DIMINSION)

LAB 13: ARRAYS (ONE DIMINSION) Statement Purpose: The purpose of this Lab. is to practically familiarize student with the concept of array and related operations performed on array. Activity Outcomes: As a second Lab on Chapter 7, this

More information

H212 Introduction to Software Systems Honors

H212 Introduction to Software Systems Honors Introduction to Software Systems Honors Lecture #04: Fall 2015 1/20 Office hours Monday, Wednesday: 10:15 am to 12:00 noon Tuesday, Thursday: 2:00 to 3:45 pm Office: Lindley Hall, Room 401C 2/20 Printing

More information

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1 Name SOLUTION Page Points Score 2 15 3 8 4 18 5 10 6 7 7 7 8 14 9 11 10 10 Total 100 1 P age 1. Program Traces (41 points, 50 minutes)

More information

Lectures 3-1, 3-2. Control Structures. Control Structures, Shimon Schocken IDC Herzliya, slide 1

Lectures 3-1, 3-2. Control Structures. Control Structures, Shimon Schocken IDC Herzliya,  slide 1 Introduction to Computer Science Shimon Schocken IDC Herzliya Lectures 3-1, 3-2 Control Structures Control Structures, Shimon Schocken IDC Herzliya, www.intro2cs.com slide 1 Shorthand operators (increment

More information

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous CS256 Computer Science I Kevin Sahr, PhD Lecture 25: Miscellaneous 1 main Method Arguments recall the method header of the main method note the argument list public static void main (String [] args) we

More information

St. Edmund Preparatory High School Brooklyn, NY

St. Edmund Preparatory High School Brooklyn, NY AP Computer Science Mr. A. Pinnavaia Summer Assignment St. Edmund Preparatory High School Name: I know it has been about 7 months since you last thought about programming. It s ok. I wouldn t want to think

More information

Lab1 Solution. Lab2 Solution. MathTrick.java. CoinFlip.java

Lab1 Solution. Lab2 Solution. MathTrick.java. CoinFlip.java Lab1 Solution MathTrick.java /** * MathTrick Lab 1 * * @version 8/25/11 * Completion time: 10-15 minutes public class MathTrick public static void main(string [] args) int num = 34; //Get a positive integer

More information

Selection Statements and operators

Selection Statements and operators Selection Statements and operators CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/

More information

CS212 Midterm. 1. Read the following code fragments and answer the questions.

CS212 Midterm. 1. Read the following code fragments and answer the questions. CS1 Midterm 1. Read the following code fragments and answer the questions. (a) public void displayabsx(int x) { if (x > 0) { System.out.println(x); return; else { System.out.println(-x); return; System.out.println("Done");

More information

Repetition CSC 121 Fall 2014 Howard Rosenthal

Repetition CSC 121 Fall 2014 Howard Rosenthal Repetition CSC 121 Fall 2014 Howard Rosenthal Lesson Goals Learn the following three repetition methods, their similarities and differences, and how to avoid common errors when using them: while do-while

More information

! definite loop: A loop that executes a known number of times. " The for loops we have seen so far are definite loops. ! We often use language like

! definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. ! We often use language like Indefinite loops while loop! indefinite loop: A loop where it is not obvious in advance how many times it will execute.! We often use language like " "Keep looping as long as or while this condition is

More information

CS110 Programming Language I. Lab 6: Multiple branching Mechanisms

CS110 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 information

Midterm Examination (MTA)

Midterm 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 information

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 26, Name: Key

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 26, Name: Key CSC 1051 Algorithms and Data Structures I Midterm Examination February 26, 2015 Name: Key Question Value 1 10 Score 2 10 3 10 4 10 5 10 6 10 7 10 8 10 9 10 10 10 TOTAL 100 Please answer questions in the

More information

Loops and Expression Types

Loops and Expression Types Software and Programming I Loops and Expression Types Roman Kontchakov / Carsten Fuhs Birkbeck, University of London Outline The while, for and do Loops Sections 4.1, 4.3 and 4.4 Variable Scope Section

More information

CS141 Programming Assignment #5

CS141 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 information

Darrell Bethea May 10, MTWRF 9:45-11:15 AM Sitterson 011

Darrell Bethea May 10, MTWRF 9:45-11:15 AM Sitterson 011 Darrell Bethea May 10, 2011 MTWRF 9:45-11:15 AM Sitterson 011 1 Office hours: MW 1-2 PM If you still cannot make it to either office hour, email me to set up an appointment if you need help with an assignment.

More information

Write 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. 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 information

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A CSC 1051 Algorithms and Data Structures I Midterm Examination February 25, 2016 Name: KEY A Question Value Score 1 10 2 10 3 10 4 10 5 10 6 10 7 10 8 10 9 10 10 10 TOTAL 100 Please answer questions in

More information

Advanced if/else & Cumulative Sum

Advanced if/else & Cumulative Sum Advanced if/else & Cumulative Sum Subset of the Supplement Lesson slides from: Building Java Programs, Chapter 4 by Stuart Reges and Marty Stepp (http://www.buildingjavaprograms.com/ ) Questions to consider

More information

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Repe$$on CSC 121 Fall 2015 Howard Rosenthal Repe$$on CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Learn the following three repetition methods, their similarities and differences, and how to avoid common errors when using them: while do-while

More information

Loops. Eng. Mohammed Abdualal. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Department

Loops. 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 information

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved.

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved. Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Slide 3-3 Flow Of Control Flow of control refers to the

More information

CSCE 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! 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 information

AP COMPUTER SCIENCE A DIAGNOSTIC EXAM. Multiple Choice Section Time - 1 hour and 15 minutes Number of questions - 40 Percent of total grade - 50

AP COMPUTER SCIENCE A DIAGNOSTIC EXAM. Multiple Choice Section Time - 1 hour and 15 minutes Number of questions - 40 Percent of total grade - 50 AP COMPUTER SCIENCE A DIAGNOSTIC EXAM Multiple Choice Section Time - 1 hour and 15 minutes Number of questions - 40 Percent of total grade - 50 Directions: Determine the answer to each of the following

More information

2.8. Decision Making: Equality and Relational Operators

2.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 information

COMP102: Test July, 2006

COMP102: Test July, 2006 Name:.................................. ID Number:............................ COMP102: Test 1 26 July, 2006 Instructions Time allowed: 45 minutes. Answer all the questions. There are 45 marks in total.

More information

Chapter 1 Introduction to Java

Chapter 1 Introduction to Java Chapter 1 Introduction to Java Lesson page 0-1. Introduction to Livetexts Question 1. A livetext is a text that relies not only on the printed word but also on graphics, animation, audio, the computer,

More information

CS141 Programming Assignment #4

CS141 Programming Assignment #4 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

More information

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Simple Control Flow: if-else statements

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Simple Control Flow: if-else statements WIT COMP1000 Simple Control Flow: if-else statements Control Flow Control flow is the order in which program statements are executed So far, all of our programs have been executed straight-through from

More information

Practice Midterm 1 Answer Key

Practice Midterm 1 Answer Key CS 120 Software Design I Fall 2018 Practice Midterm 1 Answer Key University of Wisconsin - La Crosse Due Date: October 5 NAME: Do not turn the page until instructed to do so. This booklet contains 10 pages

More information

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Dr. Marenglen Biba (C) 2010 Pearson Education, Inc. All rights reserved. Java application A computer program that executes when you use the java command to launch the Java Virtual Machine

More information

Question 1 [20 points]

Question 1 [20 points] Question 1 [20 points] a) Write the following mathematical expression in Java. c=math.sqrt(math.pow(a,2)+math.pow(b,2)- 2*a*b*Math.cos(gamma)); b) Write the following Java expression in mathematical notation.

More information

Handout 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. 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 information

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Repe$$on CSC 121 Spring 2017 Howard Rosenthal Repe$$on CSC 121 Spring 2017 Howard Rosenthal Lesson Goals Learn the following three repetition structures in Java, their syntax, their similarities and differences, and how to avoid common errors when

More information

Question: Total Points: Score:

Question: Total Points: Score: CS 170 Exam 1 Section 001 Fall 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 information

LOOPS. 1- Write a program that prompts user to enter an integer N and determines and prints the sum of cubes from 5 to N (i.e. sum of 5 3 to N 3 ).

LOOPS. 1- Write a program that prompts user to enter an integer N and determines and prints the sum of cubes from 5 to N (i.e. sum of 5 3 to N 3 ). LOOPS 1- Write a program that prompts user to enter an integer N and determines and prints the sum of cubes from 5 to N (i.e. sum of 5 3 to N 3 ). 2-Give the result of the following program: #include

More information

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution Ch 5 Arrays Multiple Choice Test 01. An array is a ** (A) data structure with one, or more, elements of the same type. (B) data structure with LIFO access. (C) data structure, which allows transfer between

More information

Adding Integers. Unit 1 Lesson 6

Adding Integers. Unit 1 Lesson 6 Unit 1 Lesson 6 Students will be able to: Add integers using rules and number line Key Vocabulary: An integer Number line Rules for Adding Integers There are two rules that you must follow when adding

More information

STUDENT LESSON A12 Iterations

STUDENT 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 information

Web-CAT submission URL: CAT.woa/wa/assignments/eclipse

Web-CAT submission URL:  CAT.woa/wa/assignments/eclipse King Saud University College of Computer & Information Science CSC111 Lab04 Conditional Statements All Sections ------------------------------------------------------------------- Instructions Web-CAT

More information

Data Conversion & Scanner Class

Data Conversion & Scanner Class Data Conversion & Scanner Class Quick review of last lecture August 29, 2007 ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor: Alexander Stoytchev Numeric Primitive Data Storing

More information

M105: 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 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 information

Selection Statements and operators

Selection Statements and operators Selection Statements and operators CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/

More information

Over and Over Again GEEN163

Over and Over Again GEEN163 Over and Over Again GEEN163 There is no harm in repeating a good thing. Plato Homework A programming assignment has been posted on Blackboard You have to convert three flowcharts into programs Upload the

More information

Repetition. Chapter 6

Repetition. Chapter 6 Chapter 6 Repetition Goals This chapter introduces the third major control structure repetition (sequential and selection being the first two). Repetition is discussed within the context of two general

More information

Section 1.2: Points and Lines

Section 1.2: Points and Lines Section 1.2: Points and Lines Objective: Graph points and lines using x and y coordinates. Often, to get an idea of the behavior of an equation we will make a picture that represents the solutions to the

More information

Section 004 Spring CS 170 Exam 1. Name (print): Instructions:

Section 004 Spring CS 170 Exam 1. Name (print): Instructions: CS 170 Exam 1 Section 004 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 information

Repetition. Chapter 6

Repetition. Chapter 6 Chapter 6 Repetition Goals This chapter introduces the third major control structure repetition (sequential and selection being the first two). Repetition is discussed within the context of two general

More information

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs CSC 1051 Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova

More information

Control Statements: Part 1

Control 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 information

AP Computer Science A

AP Computer Science A AP Computer Science A 1st Quarter Notes Table of Contents - section links Click on the date or topic below to jump to that section Date : 9/8/2017 Aim : Java Basics Objects and Classes Data types: Primitive

More information

Lesson 10..The switch Statement and char

Lesson 10..The switch Statement and char Lesson 10..The switch Statement and char 10-1 The if statement is the most powerful and often used decision-type command. The switch statement is useful when we have an integer variable that can be one

More information

Introduction to Java & Fundamental Data Types

Introduction to Java & Fundamental Data Types Introduction to Java & Fundamental Data Types LECTURER: ATHENA TOUMBOURI How to Create a New Java Project in Eclipse Eclipse is one of the most popular development environments for Java, as it contains

More information

Ch. 6. User-Defined Methods

Ch. 6. User-Defined Methods Ch. 6 User-Defined Methods Func5onal Abstrac5on Func5onal regarding func5ons/methods Abstrac5on solving a problem in a crea5ve way Stepwise refinement breaking down large problems into small problems The

More information

Repetition, Looping. While Loop

Repetition, Looping. While Loop Repetition, Looping Last time we looked at how to use if-then statements to control the flow of a program. In this section we will look at different ways to repeat blocks of statements. Such repetitions

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

COMP 202. Programming With Iterations. CONTENT: The WHILE, DO and FOR Statements. COMP Loops 1

COMP 202. Programming With Iterations. CONTENT: The WHILE, DO and FOR Statements. COMP Loops 1 COMP 202 Programming With Iterations CONTENT: The WHILE, DO and FOR Statements COMP 202 - Loops 1 Repetition Statements Repetition statements or iteration allow us to execute a statement multiple times

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 4: Conditional Execution 1 loop techniques cumulative sum fencepost loops conditional execution Chapter outline the if statement and the if/else statement relational expressions

More information

Java 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 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 information

Practice with variables and types

Practice with variables and types Practice with variables and types 1. Types. For each literal or expression, state its type (String, int, double, or boolean). Expression Type Expression Type 387 int "pancakes" String true boolean 45.0

More information

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs CSC 1051 Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova

More information