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 public static void main(string[] args) //Declare a scanner and input an int. 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 public static void main(string[] args) //Declare a Scanner and input two integers. 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 public static void main(string[] args) //Declare a Scanner and input four octets. 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 / import java.lang.math; class Lesson_17_Activity_One public static void main(string[] args) //Declare a Scanner and prompt for scores. 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: / import java.lang.math; class Lesson_17_Activity_Two public static void main(string[] args) //Declare a Scanner and input two numbers. System.out.println("Enter two numbers: "); 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: / import java.lang.math; class Lesson_20_Activity public static void main(string[] args) //Declare a Scanner. //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 / import java.lang.math; class Lesson_22_Activity_One public static void main(string[] args) //Declare a Scanner and input a String. 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. / import java.lang.math; class Lesson_22_Activity_Two public static void main(string[] args) //Declare a Scanner and input a String. 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. / import java.lang.math; class Lesson_24_Activity_One public static void main(string[] args) //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. / import java.lang.math; class Lesson_24_Activity_Two public static void main(string[] args) //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 / import java.lang.math; class Lesson_24_Activity_Three public static void main(string[] args) //Declare a Scanner and input a number. 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 / import java.lang.math; 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"; public static void main(string[] args) //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 / import java.lang.math; 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"; public static void main(string[] args) //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"; public static void main(string[] args) //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 / import java.lang.math; 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

19 1 Lesson 32 - Activity 1 / Term 1: Lesson 32 Coding Activity 1 For the Lesson 32 activities, you will be asked to write one or more methods. Use the template to write a main method that tests each of your methods, then paste everything into the code runner box. Your submission should begin with the first import statement and end with the final. Write a method that takes a parameter for the number of a month and prints the month's name. This method must be called monthname() and it must have an integer parameter. Calling monthname(8) should print August to the screen. / import java.io.; class Lesson_32_Activity_One public static void monthname(int m) //Use if statements to check for each month individually. //Alternatively, students may create a String array and use //the variable m to access the month, as in the //following commented out code. / String [] months = "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"; return months[m - 1]; / if (m == 1) System.out.println("January"); if (m == 2) System.out.println("February"); if (m == 3) System.out.println("March"); if (m ==4) System.out.println("April"); if (m == 5) System.out.println("May"); if (m == 6) System.out.println("June"); if (m == 7) System.out.println("July"); if (m == 8) System.out.println("August"); if (m == 9) System.out.println("September"); if (m == 10) System.out.println("October");

20 2 if (m == 11) System.out.println("November"); if (m == 12) System.out.println("December"); //main method to test the program. This is not required by the //code runner, but is an important step in writing new methods. public static void main(string [] args) System.out.println("Enter a month number:"); int m = scan.nextint(); System.out.println("monthName(" + m + ") prints:"); monthname(m);

21 3 Lesson 32 - Activity 2 / Term 1: Lesson 32 Coding Activity 2 For the Lesson 32 activities, you will be asked to write one or more methods. Use the template to write a main method that tests each of your methods, then paste everything into the code runner box. Your submission should begin with the first import statement and end with the final. Write a method that takes a parameter for the number of a month and prints the number of days in the month. Assume that February will always have 28 days for this activity. This method must be called monthdays()and it must take an integer parameter. Calling monthdays(2) would print 28 and monthdays(9) would print 30. / import java.io.; class Lesson_32_Activity_Two public static void monthdays(int m) //There are three possible values for monthdays: 28, 30, or 31. //A three part if-else construction is used to return the appropriate //value. if (m == 4 m == 6 m == 9 m == 11) System.out.println(30); else if (m == 2 ) System.out.println(28); else System.out.println(31); //main method to test the program. This is not required by the //code runner, but is an important step in writing new methods. public static void main(string [] args) System.out.println("Enter a month number:"); int m = scan.nextint(); System.out.println("monthDays(" + m + ") prints:"); monthdays(m);

22 4 Lesson 32 - Activity 3 / Term 1: Lesson 32 Coding Activity 3 For the Lesson 32 activities, you will be asked to write one or more methods. Use the template to write a main method that tests each of your methods, then paste everything into the code runner box. Your submission should begin with the first import statement and end with the final. Write a method that takes two integer parameters and prints them in reverse. This method must be called swap and should take two integer parameters. Calling swap(3, 7) would print 7 3. / import java.io.; class Lesson_32_Activity_Three public static void swap (int a, int b) //Print a and b in reverse order System.out.println(b + " " + a); //main method to test the program. This is not required by the //code runner, but is an important step in writing new methods. public static void main(string [] args) System.out.println("Enter two numbers:"); int a = scan.nextint(); int b = scan.nextint(); System.out.println("Swap(" + a + ", " + b + ") prints:"); swap(a,b);

23 5 Lesson 32 - Activity 4 / Term 1: Lesson 32 Coding Activity 4 For the Lesson 32 activities, you will be asked to write one or more methods. Use the template to write a main method that tests each of your methods, then paste everything into the code runner box. Your submission should begin with the first import statement and end with the final. Write a method that accepts a number of seconds and prints the correct number of hours, minutes and seconds. This method must be called realtime() and its parameter must be an integer. Calling realtime(6342) would print the following: Hours: 1 Minutes: 45 Seconds: 42 / import java.io.; class Lesson_32_Activity_Four public static void realtime (int s) //There are 3600 seconds in an hour. Divide to determine the //number of hours. System.out.println("Hours: " + s / (3600) ); //Use modular division to recover the remaining seconds and //divide by 60 to convert to minutes. s = s % (3600); System.out.println("Minutes: " + s / 60); //Again, use mod to recover the remaining seconds. //Recall that s %= 60 is equivalent to s = s % 60 s %= 60; System.out.println("Seconds: " + s); //main method to test the program. This is not required by the //code runner, but is an important step in writing new methods. public static void main(string [] args) System.out.println("Enter a number of seconds:"); int s = scan.nextint(); System.out.println("realTime(" + s + ") prints:"); realtime(s);

24 6 Lesson 33 - Activity 1 / Term 1: Lesson 33 Coding Activity 1 For the Lesson 33 activities, you will be asked to write one or more methods. Use the template to write a main method that tests each of your methods, then paste everything into the code runner box. Your submission should begin with the first import statement and end with the final. For questions 2-5, you may want to start with the printit method and use it to test the other three. Write a method that takes an array of Strings and changes of the Strings to UPPER CASE. This method must be called upper() and it must take a String[] parameter. Use T1_L33_Reference_Tempate.java, which is included in this folder, as a reference. / import java.io.; class Lesson_33_Activity_One public static void upper(string [] a) //Use a for loop to process each index in an array for(int i = 0; i < a.length; i++) //At each i, change the ith string to uppercase by storing //the results of a[i].touppercase() in a[i]. a[i] = a[i].touppercase(); //main method to test the program. This is not required by the //code runner, but is an important step in writing new methods. public static void main(string [] args) System.out.println("Enter a positive number" + " indicating the length of an array:"); int n = scan.nextint(); String [] a = new String [n]; for(int i = 0; i < n; i++) System.out.println("Enter a String:"); a[i] = scan.next(); System.out.println("Calling upper() on the values you entered" + " changes the array to the following:"); upper(a); for(int i = 0; i < a.length; i++) System.out.println(a[i]);

25 7 Lesson 33 - Activity 2 / Term 1: Lesson 33 Coding Activity 2 For the Lesson 33 activities, you will be asked to write one or more methods. Use the template to write a main method that tests each of your methods, then paste everything into the code runner box. Your submission should begin with the first import statement and end with the final. For questions 2-5, you may want to start with the printit method and use it to test the other three. Write a method that takes an array of ints and stores random numbers between 10 and 99 in the array. Use Math.random() to generate random numbers and convert them to integers between 10 and 99 inclusive. This method must be called randomize() and it must take an int[] parameter. / import java.io.; class Lesson_33_Activity_Two public static void randomize(int [] a) //This is similar to problem 1, but we are storing the result of //Math.random() in an in array instead of processing Strings. for (int i = 0; i < a.length; i++) a[i] = (int) (Math.random()90) + 10; //main method to test the program. This is not required by the //code runner, but is an important step in writing new methods. public static void main(string [] args) System.out.println("Enter a positive number" + " indicating the length of an array:"); int n = scan.nextint(); int [] a = new int [n]; randomize(a); System.out.println("First call to randomize:"); for(int i = 0; i < a.length; ++i) System.out.println(a[i]); randomize(a); System.out.println("Second call to randomize:"); for(int i = 0; i < a.length; ++i) System.out.println(a[i]);

26 8 Lesson 33 - Activity 3 / Term 1: Lesson 33 Coding Activity 3 For the Lesson 33 activities, you will be asked to write one or more methods. Use the template to write a main method that tests each of your methods, then paste everything into the code runner box. Your submission should begin with the first import statement and end with the final. For questions 2-5, you may want to start with the printit method and use it to test the other three. Write a method that takes an array of ints and prints the array on a single line. Print one space between each number. This method must be called printit() and it must take an int[] parameter. / import java.io.; class Lesson_33_Activity_Three public static void printit(int [] a) for (int i = 0; i < a.length; i++) //Print the ith element of a followed by a single space, //using System.out.print so that all output is printed //on a single line. System.out.print(a[i] + " "); //Print a newline after running the loop System.out.println(); //main method to test the program. This is not required by the //code runner, but is an important step in writing new methods. public static void main(string [] args) System.out.println("Enter a postivite number" + " indicating the length of an array:"); int n = scan.nextint(); int [] a = new int [n]; for(int i = 0; i < n; ++i) System.out.println("Enter any integer:"); a[i] = scan.nextint(); System.out.println("Using printit to print an array of" + " those numbers prints the following:"); printit(a);

27 9 Lesson 33 - Activity 4 / Term 1: Lesson 33 Coding Activity 4 For the Lesson 33 activities, you will be asked to write one or more methods. Use the template to write a main method that tests each of your methods, then paste everything into the code runner box. Your submission should begin with the first import statement and end with the final. For questions 2-5, you may want to start with the printit method and use it to test the other three. Write a method that takes an array of ints and reverses the order of the values in the array. So the array 1, 2, 3 would be changed to 3, 2, 1 This method must be called reverse() and it must take an int[] parameter. / import java.io.; class Lesson_33_Activity_Four public static void reverse (int a[]) //Create a temporary array to store the values of a int temp[] = new int[a.length]; //Use a for loop to copy a into temp for (int i = 0; i < a.length; i++) temp[i] = a[i]; //Use a second for loop to copy temp back into a, //but using length-1-i to access the values of //temp in reverse order. for (int i = 0; i < a.length; i++) a[i] = temp[a.length i]; //main method to test the program. This is not required by the //code runner, but is an important step in writing new methods. public static void main(string [] args) System.out.println("Enter a postivite number" + " indicating the length of an array:"); int n = scan.nextint(); int [] a = new int [n];

28 10 for(int i = 0; i < n; ++i) System.out.println("Enter any integer:"); a[i] = scan.nextint(); System.out.println("Before calling reverse(a):"); printit(a); reverse(a); System.out.println("After calling reverse(a):"); printit(a); //include printit for testing public static void printit(int [] a) for (int i = 0; i < a.length; i++) System.out.print(a[i] + " "); System.out.println();

29 11 Lesson 33 - Activity 5 / Term 1: Lesson 33 Coding Activity 5 For the Lesson 33 activities, you will be asked to write one or more methods. Use the template to write a main method that tests each of your methods, then paste everything into the code runner box. Your submission should begin with the first import statement and end with the final. For questions 2-5, you may want to start with the printit method and use it to test the other three. Write a method that takes an array of ints, an integer value, and an integer index.the method should insert the value at the given index and move the values afterwards by one. This method must be called insertvalue() and must have the following parameter types: int[], integer, integer. Calling insertvalue(a, 100, 2) would change the array 1, 2, 3, 4, 5 to 1, 2, 100, 3, 4. / import java.io.; class Lesson_33_Activity_Five public static void insertvalue(int a[], int value, int loc) //This for loop starts at the end of the list and moves //backwards until it reaches loc. for ( int i = a.length - 1; i > loc; i--) //at each step in the loop, we are shifting one element forward //in the array in order to make room for the new value. a[i] = a[i - 1]; //Store the value after shifting the array. a[loc] = value; //main method to test the program. This is not required by the //code runner, but is an important step in writing new methods. public static void main(string [] args) System.out.println("Enter a positive number" + " indicating the length of an array:"); int n = scan.nextint(); int [] a = new int [n]; for(int i = 0; i < n; i++) System.out.println("Enter any integer:"); a[i] = scan.nextint();

30 12 System.out.println("You have entered the array:"); printit(a); System.out.println("Now enter a location:"); int loc = scan.nextint(); System.out.println("And a value to insert:"); int val = scan.nextint(); insertvalue(a, val, loc); System.out.println("After calling insertvalue with" + val + " and " + loc + ":"); printit(a); //include printit for testing public static void printit(int [] a) for (int i = 0; i < a.length; i++) System.out.print(a[i] + " "); System.out.println();

31 13 Lesson 34 - Activity 1 / Term 1: Lesson 34 Coding Activity 1 For the Lesson 34 activities, you will be asked to write one or more methods. Use the template to write a main method that tests each of your methods, then paste everything into the code runner box. Your submission should begin with the first import statement and end with the final. Write a method that takes an array of ints as a parameter and returns the sum of integers in the array. public static int sum(int [] a) For example, sum(a); would return 6, if a = 1, 2, 3, or 3, if a = 1, 1, 1. / import java.io.; class Lesson_34_Activity_One public static int sum(int [] a) //Declare s to store the sum int s = 0; //Loop through the array, adding each value to s for(int i =0; i < a.length; i++) s += a[i]; //return the sum return s; //main method to test the program. This is not required by the //code runner, but is an important step in writing new methods. public static void main(string [] args) System.out.println("Enter a positive number" + " indicating the length of an array:"); int n = scan.nextint(); int [] a = new int [n]; for(int i = 0; i < n; i++) System.out.println("Enter any integer:"); a[i] = scan.nextint(); System.out.println("Calling sum on this array returns " + sum(a));

32 14 Lesson 34 - Activity 2 / Term 1: Lesson 34 Coding Activity 2 For the Lesson 34 activities, you will be asked to write one or more methods. Use the template to write a main method that tests each of your methods, then paste everything into the code runner box. Your submission should begin with the first import statement and end with the final. Write a method that takes an array of ints as a parameter and returns the average value of the array as a double. public static double average(int [] a) For example, average(a) would return 2.0 if a = 1, 2, 3 or 1.0 if a = 1, 1, 1. / import java.io.; class Lesson_34_Activity_Two //Use sum from the previous activity public static int sum(int [] a) int s = 0; for(int i =0; i < a.length; i++) s += a[i]; return s; public static double average(int [] a) //Since we already have a method to sum an array, //we only need to divide that sum by the length. //Multiplying by 1.0 converts sum to a double so that the //result will be accurate. return 1.0 sum(a)/a.length; //main method to test the program. This is not required by the //code runner, but is an important step in writing new methods. public static void main(string [] args) System.out.println("Enter a positive number" + " indicating the length of an array:"); int n = scan.nextint(); int [] a = new int [n];

33 15 for(int i = 0; i < n; i++) System.out.println("Enter any integer:"); a[i] = scan.nextint(); System.out.println("Calling average on this array returns " + average(a));

34 16 Lesson 34 - Activity 3 / Term 1: Lesson 34 Coding Activity 3 For the Lesson 34 activities, you will be asked to write one or more methods. Use the template to write a main method that tests each of your methods, then paste everything into the code runner box. Your submission should begin with the first import statement and end with the final. Write a method that takes an array of ints and returns the largest value in the array. public static int findmax(int [] a) / import java.io.; class Lesson_34_Activity_Three public static int findmax(int []a) //Create a variable to store the max int max = a[0]; for(int i = 0; i < a.length; i++) //if the ith element of a is greater than the max, //then we have a new max, a[i]. if (max < a[i]) max = a[i]; return max; //main method to test the program. This is not required by the //code runner, but is an important step in writing new methods. public static void main(string [] args) System.out.println("Enter a positive number" + " indicating the length of an array:"); int n = scan.nextint(); int [] a = new int [n]; for(int i = 0; i < n; i++) System.out.println("Enter any integer:"); a[i] = scan.nextint(); System.out.println("Calling findmax on this array returns " + findmax(a));

35 17 Lesson 34 - Activity 4 / Term 1: Lesson 34 Coding Activity 4 For the Lesson 34 activities, you will be asked to write one or more methods. Use the template to write a main method that tests each of your methods, then paste everything into the code runner box. Your submission should begin with the first import statement and end with the final. Write a method that takes an array of ints and returns the smallest value in the array. public static int findmin(int [] a) / import java.io.; class Lesson_34_Activity_Four public static int findmin(int []a) //This method is identical to max, //except we reverse the comparison in our //if statement. int min = a[0]; for(int i = 0; i < a.length; i++) if (min > a[i]) min = a[i]; return min; //main method to test the program. This is not required by the //code runner, but is an important step in writing new methods. public static void main(string [] args) System.out.println("Enter a positive number" + " indicating the length of an array:"); int n = scan.nextint(); int [] a = new int [n]; for(int i = 0; i < n; i++) System.out.println("Enter any integer:"); a[i] = scan.nextint(); System.out.println("Calling findmin on this array returns " + findmin(a));

36 18 Lesson 34 - Activity 5 / Term 1: Lesson 34 Coding Activity 5 For the Lesson 34 activities, you will be asked to write one or more methods. Use the template to write a main method that tests each of your methods, then paste everything into the code runner box. Your submission should begin with the first import statement and end with the final. Write a method that takes an array of ints and returns a sum of only the even values. public static int sumeven(int [] a) For example, sumeven(a) would return 6 if a = 1, 2, 3, 4, 5. / import java.io.; class Lesson_34_Activity_Five public static int sumeven(int [] a) //As in the sum method, we need a variable to add values to. int sum = 0; for(int i =0; i < a.length; i++) //An additional if statement is used to only add //even values. if (a[i] %2 ==0) sum += a[i]; return sum; //main method to test the program. This is not required by the //code runner, but is an important step in writing new methods. public static void main(string [] args) System.out.println("Enter a positive number" + " indicating the length of an array:"); int n = scan.nextint(); int [] a = new int [n]; for(int i = 0; i < n; i++) System.out.println("Enter any integer:"); a[i] = scan.nextint(); System.out.println("Calling sumeven on this array returns " + sumeven(a));

37 19 Lesson 34 - Activity 6 / Term 1: Lesson 34 Coding Activity 6 For the Lesson 34 activities, you will be asked to write one or more methods. Use the template to write a main method that tests each of your methods, then paste everything into the code runner box. Your submission should begin with the first import statement and end with the final. Write a method that takes an array of ints and returns true if all of the values in the array are positive. If the array contains any negative integers, it should return false. public static boolean allpositive(int [] a) / import java.io.; class Lesson_34_Activity_Six public static boolean allpositive (int [] a) for(int i = 0; i < a.length; i++) //If a single value is negative, we return false if (a[i] < 0) return false; //If the loop has been run, and we have not returned, then no values //were negative, so we can return true. Students may feel more //comfortable working with a flag variable, but it is worth //discussing this approach to help with understanding //return statements. return true; //main method to test the program. This is not required by the //code runner, but is an important step in writing new methods. public static void main(string [] args) System.out.println("Enter a positive number" + " indicating the length of an array:"); int n = scan.nextint(); int [] a = new int [n]; for(int i = 0; i < n; i++) System.out.println("Enter any integer:"); a[i] = scan.nextint(); System.out.println("Calling allpositive on this array returns " + allpositive(a));

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

CONDITIONAL EXECUTION: PART 2

CONDITIONAL EXECUTION: PART 2 CONDITIONAL EXECUTION: PART 2 yes x > y? no max = x; max = y; logical AND logical OR logical NOT &&! Fundamentals of Computer Science I Outline Review: The if-else statement The switch statement A look

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

CSEN 202 Introduction to Computer Programming

CSEN 202 Introduction to Computer Programming CSEN 202 Introduction to Computer Programming Lecture 3: Decisions Prof. Dr. Slim Abdennadher and Dr Mohammed Abdel Megeed Salem, slim.abdennadher@guc.edu.eg German University Cairo, Department of Media

More information

10/30/2010. Introduction to Control Statements. The if and if-else Statements (cont.) Principal forms: JAVA CONTROL STATEMENTS SELECTION STATEMENTS

10/30/2010. Introduction to Control Statements. The if and if-else Statements (cont.) Principal forms: JAVA CONTROL STATEMENTS SELECTION STATEMENTS JAVA CONTROL STATEMENTS Introduction to Control statements are used in programming languages to cause the flow of control to advance and branch based on changes to the state of a program. In Java, control

More information

1. Introduction to Java for JAS

1. Introduction to Java for JAS Introduction to Java and Agent-Based Economic Platforms (CF-904) 1. Introduction to Java for JAS Mr. Simone Giansante Email: sgians@essex.ac.uk Web: http://privatewww.essex.ac.uk/~sgians/ Office: 3A.531

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

CS Programming I: Arrays

CS Programming I: Arrays CS 200 - Programming I: Arrays Marc Renault Department of Computer Sciences University of Wisconsin Madison Fall 2017 TopHat Sec 3 (PM) Join Code: 719946 TopHat Sec 4 (AM) Join Code: 891624 Array Basics

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

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

Learning the Java Language. 2.1 Object-Oriented Programming

Learning the Java Language. 2.1 Object-Oriented Programming Learning the Java Language 2.1 Object-Oriented Programming What is an Object? Real world is composed by different kind of objects: buildings, men, women, dogs, cars, etc. Each object has its own states

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

Intermediate Programming

Intermediate Programming Intermediate Programming Lecture 12 Interfaces What is an Interface? A Java interface specified a set of methods that any class that implements the interfacesmust have. An Interface is a type, which means

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

1 Short Answer (10 Points Each)

1 Short Answer (10 Points Each) 1 Short Answer (10 Points Each) 1. For the following one-dimensional array, show the final array state after each pass of the three sorting algorithms. That is, after each iteration of the outside loop

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

Sequential Search (Searching Supplement: 1-2)

Sequential Search (Searching Supplement: 1-2) (Searching Supplement: 1-2) A sequential search simply involves looking at each item in an array in turn until either the value being searched for is found or it can be determined that the value is not

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

COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz

COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz The in-class quiz is intended to give you a taste of the midterm, give you some early feedback about

More information

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018 Birkbeck (University of London) Software and Programming 1 In-class Test 2.1 22 Mar 2018 Student Name Student Number Answer ALL Questions 1. What output is produced when the following Java program fragment

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

Name CIS 201 Midterm II: Chapters 1-8

Name CIS 201 Midterm II: Chapters 1-8 Name CIS 201 Midterm II: Chapters 1-8 December 15, 2010 Directions: This is a closed book, closed notes midterm. Place your answers in the space provided. The point value for each question is indicated.

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

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Computer Programming, I. Laboratory Manual. Experiment #3. Selections 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 #3

More information

Tutorial 8 (Array I)

Tutorial 8 (Array I) Tutorial 8 (Array I) 1. Indicate true or false for the following statements. a. Every element in an array has the same type. b. The array size is fixed after it is created. c. The array size used to declare

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

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

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

Control Flow Statements

Control Flow Statements Control Flow Statements The statements inside your source files are generally executed from top to bottom, in the order that they appear. Control flow statements, however, break up the flow of execution

More information

Arrays and Array Lists. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington

Arrays and Array Lists. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington Arrays and Array Lists CSE 1310 Introduction to Computers and Programming Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington 1 Motivation Current limitation: We cannot record multiple

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

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

Tutorial 11. Exercise 1: CSC111 Computer Programming I. A. Write a code snippet to define the following arrays:

Tutorial 11. Exercise 1: CSC111 Computer Programming I. A. Write a code snippet to define the following arrays: College of Computer and Information Sciences CSC111 Computer Programming I Exercise 1: Tutorial 11 Arrays: A. Write a code snippet to define the following arrays: 1. An int array named nums of size 10.

More information

JAVA OPERATORS GENERAL

JAVA OPERATORS GENERAL JAVA OPERATORS GENERAL Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators

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

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

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

CHAPTER 3,4 & 5. Contents:

CHAPTER 3,4 & 5. Contents: CHAPTER 3,4 & 5 Contents: *Understanding a Simple java program *Basic Program elements Character set, comments, java tokens, main method, data types. *Variable/identifiers and rules for identifier *Constants/literals,

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

Section 003 Fall CS 170 Exam 2. Name (print): Instructions:

Section 003 Fall CS 170 Exam 2. Name (print): Instructions: CS 170 Exam 2 Section 003 Fall 2012 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

CS141 Programming Assignment #8

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

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

Module 7: Arrays (Single Dimensional)

Module 7: Arrays (Single Dimensional) Module 7: Arrays (Single Dimensional) Objectives To describe why arrays are necessary in programming ( 7.1). To declare array reference variables and create arrays ( 7.2.1 7.2.2). To obtain array size

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

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

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

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

Lab Assignment Three

Lab Assignment Three Lab Assignment Three C212/A592 Fall Semester 2010 Due in OnCourse by Friday, September 17, 11:55pm (Dropbox will stay open until Saturday, September 18, 11:55pm) Abstract Read and solve the problems below.

More information

Assignment2013 Please use this document only for verifying if your programs are right. Do not blindly copy paste and waste your time.

Assignment2013 Please use this document only for verifying if your programs are right. Do not blindly copy paste and waste your time. Please use this document only for verifying if your programs are right. Do not blindly copy paste and waste your time. 11/3/2013 TechSparx Computer Training Center Saravanan.G Please use this document

More information

CSE 142, Autumn 2010 Final Exam Wednesday, December 15, Name:

CSE 142, Autumn 2010 Final Exam Wednesday, December 15, Name: CSE 142, Autumn 2010 Final Exam Wednesday, December 15, 2010 Name: Section: Student ID #: TA: Rules: You have 110 minutes to complete this exam. You may receive a deduction if you keep working after 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

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

Sample Midterm Exam #2

Sample Midterm Exam #2 Sample Midterm Exam #2 1. Expressions For each expression in the left-hand column, indicate its value in the right-hand column. Be sure to list a constant of appropriate type (e.g., 7.0 rather than 7 for

More information

CSEN 202: Introduction to Computer Programming Spring term Final Exam

CSEN 202: Introduction to Computer Programming Spring term Final Exam Page 0 German University in Cairo May 28, 2016 Media Engineering and Technology Prof. Dr. Slim Abdennadher Dr. Wael Aboul Saadat CSEN 202: Introduction to Computer Programming Spring term 2015-2016 Final

More information

CMPS 12A Winter 2006 Prof. Scott A. Brandt Final Exam, March 21, Name:

CMPS 12A Winter 2006 Prof. Scott A. Brandt Final Exam, March 21, Name: CMPS 12A Winter 2006 Prof. Scott A. Brandt Final Exam, March 21, 2006 Name: Email: This is a closed note, closed book exam. There are II sections worth a total of 200 points. Plan your time accordingly.

More information

COE 212 Engineering Programming. Welcome to Exam I Tuesday November 11, 2014

COE 212 Engineering Programming. Welcome to Exam I Tuesday November 11, 2014 1 COE 212 Engineering Programming Welcome to Exam I Tuesday November 11, 2014 Instructors: Dr. Bachir Habib Dr. George Sakr Dr. Joe Tekli Dr. Wissam F. Fawaz Name: Student ID: Instructions: 1. This exam

More information

Condi(onals and Loops

Condi(onals and Loops Condi(onals and Loops 1 Review Primi(ve Data Types & Variables int, long float, double boolean char String Mathema(cal operators: + - * / % Comparison: < > = == 2 A Founda(on for Programming any program

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

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

5. PLEASE TAKE HOME the question bundle, but turn in 2 paper sheets: The scantron AND the paper where you wrote your programming question solution!

5. PLEASE TAKE HOME the question bundle, but turn in 2 paper sheets: The scantron AND the paper where you wrote your programming question solution! FINAL EXAM Introduction to Computer Science UA-CCI- ICSI 201--Fall13 This is a closed book and note examination, except for one 8 1/2 x 11 inch paper sheet of notes, both sides. There is no interpersonal

More information

AP Computer Science Unit 1. Writing Programs Using BlueJ

AP Computer Science Unit 1. Writing Programs Using BlueJ AP Computer Science Unit 1. Writing Programs Using BlueJ 1. Open up BlueJ. Click on the Project menu and select New Project. You should see the window on the right. Navigate to wherever you plan to save

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 "pancakes" true 45.0 "14" 87.98515 "false"

More information

Question: Total Points: Score:

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

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

CS111: PROGRAMMING LANGUAGE II

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

CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam

CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam Seat Number Name CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam This is a closed book exam. Answer all of the questions on the question paper in the space provided. If

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

Top-down programming design

Top-down programming design 1 Top-down programming design Top-down design is a programming style, the mainstay of traditional procedural languages, in which design begins by specifying complex pieces and then dividing them into successively

More information

Arrays. What if you have a 1000 line file? Arrays

Arrays. What if you have a 1000 line file? Arrays Arrays Chapter 8 page 477 11/8/06 CS150 Introduction to Computer Science 1 1 What if you have a 1000 line file? Read in the following file and print out a population graph as shown below. The maximum value

More information

CS Computers & Programming I Review_01 Dr. H. Assadipour

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

More on methods and variables. Fundamentals of Computer Science Keith Vertanen

More on methods and variables. Fundamentals of Computer Science Keith Vertanen More on methods and variables Fundamentals of Computer Science Keith Vertanen Terminology of a method Goal: helper method than can draw a random integer between start and end (inclusive) access modifier

More information

Lecture 8 " INPUT " Instructor: Craig Duckett

Lecture 8  INPUT  Instructor: Craig Duckett Lecture 8 " INPUT " Instructor: Craig Duckett Assignments Assignment 2 Due TONIGHT Lecture 8 Assignment 1 Revision due Lecture 10 Assignment 2 Revision Due Lecture 12 We'll Have a closer look at Assignment

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

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

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

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

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2013

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2013 CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2013 Name: This exam consists of 7 problems on the following 6 pages. You may use your single- side hand- written 8 ½ x 11 note sheet during the

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

Introduction to Computer Science Unit 2. Notes

Introduction 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 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

(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

COE 212 Engineering Programming. Welcome to Exam II Thursday April 21, Instructors: Dr. Salim Haddad Dr. Joe Tekli Dr. Wissam F.

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

H212 Introduction to Software Systems Honors

H212 Introduction to Software Systems Honors Introduction to Software Systems Honors Lecture #07: September 21, 2015 1/30 We explained last time that an array is an ordered list of values. Each value is stored at a specific, numbered position in

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

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: JAVA OPERATORS GENERAL Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators

More information

Lesson 36: for() Loops (W11D1)

Lesson 36: for() Loops (W11D1) Lesson 36: for() Loops (W11D1) Balboa High School Michael Ferraro October 26, 2015 1 / 27 Do Now Create a new project: Lesson36 Write class FirstForLoop: Include a main() method: public static void main(string[]

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

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007 Name: Please fill in your Student Number and Name. Student Number : Student Number: University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ 2007 Final Examination Question Max

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

You must bring your ID to the exam.

You must bring your ID to the exam. Com S 227 Spring 2017 Topics and review problems for Exam 2 Monday, April 3, 6:45 pm Locations, by last name: (same locations as Exam 1) A-E Coover 2245 F-M Hoover 2055 N-S Physics 0005 T-Z Hoover 1213

More information

CSE 142, Autumn 2007 Midterm Exam, Friday, November 2, 2007

CSE 142, Autumn 2007 Midterm Exam, Friday, November 2, 2007 CSE 142, Autumn 2007 Midterm Exam, Friday, November 2, 2007 Name: Section: Student ID #: TA: Rules: You have 50 minutes to complete this exam. You may receive a deduction if you keep working after the

More information

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail. OOP in Java 1 Outline 1. Getting started, primitive data types and control structures 2. Classes and objects 3. Extending classes 4. Using some standard packages 5. OOP revisited Parts 1 to 3 introduce

More information

Tony Valderrama, SIPB IAP 2009

Tony Valderrama, SIPB IAP 2009 Wake up and smell the coffee! Software Java Development Kit (JDK) - http://java.sun.com/javase/downloads/index.jsp Eclipse Platform - http://www.eclipse.org/ Reference The Java Tutorial - http://java.sun.com/docs/books/tutorial/index.html

More information

CSE 142 Sample Final Exam #2

CSE 142 Sample Final Exam #2 CSE 142 Sample Final Exam #2 1. Expressions (5 points) For each expression in the left-hand column, indicate its value in the right-hand column. Be sure to list a constant of appropriate type (e.g., 7.0

More information

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Basic Operators Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators

More information

Chapter 7: Iterations

Chapter 7: Iterations Chapter 7: Iterations Objectives Students should Be able to use Java iterative constructs, including do-while, while, and for, as well as the nested version of those constructs correctly. Be able to design

More information

COE 212 Engineering Programming. Welcome to Exam II Tuesday November 28, 2018

COE 212 Engineering Programming. Welcome to Exam II Tuesday November 28, 2018 1 COE 212 Engineering Programming Welcome to Exam II Tuesday November 28, 2018 Instructors: Dr. Dima El-khalil Dr. Jawad Fahs Dr. Joe Tekli Dr. Wissam F. Fawaz Name: Student ID: Instructions: 1. This exam

More information

Array basics. Readings: 7.1

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

Arrays. Arrays (8.1) Arrays. One variable that can store a group of values of the same type. Storing a number of related values.

Arrays. Arrays (8.1) Arrays. One variable that can store a group of values of the same type. Storing a number of related values. Arrays Chapter 8 page 471 Arrays (8.1) One variable that can store a group of values of the same type Storing a number of related values o all grades for one student o all temperatures for one month o

More information