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...
Author: Rosa Cook
88 downloads 0 Views 1MB Size
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 * */ import java.util.Scanner; class Lesson_14_Activity_One { public static void main(String[] args) { //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 = 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

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, 128.253.21.58 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: 112.200.0.254 * */ import java.util.Scanner; class Lesson_14_Activity_Three { public static void main(String[] args) { //Declare a Scanner and input four octets. Scanner scan = new Scanner(System.in);

APCS Unit 2 Activity Guide

Version 1.0

© Edhesive

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 = 0 && o2 = 0 && o3 = 0 && o4 maxLon) maxLon if(lo < minLon) minLon

max or min and ask the continue. = = = =

la; la; lo; lo;

System.out.println( "Would you like to enter another location? "); rep = scan.nextInt(); } }//while //Print the results. System.out.println("Farthest System.out.println("Farthest System.out.println("Farthest System.out.println("Farthest }

North: " + maxLat); South: " + minLat); East: " + maxLon); West: " + minLon);

}

APCS Unit 2 Activity Guide

Version 1.0

© Edhesive

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.util.Scanner; import java.lang.Math; class Lesson_22_Activity_One { public static void main(String[] args) { //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

2

Lesson 22 - Activity 2 /* * Term 1: Lesson 22 Coding * Write the code to take a * * Sample run: * * Enter a string: * bought * b * o * u * g * h * t * Use a tab character for * * Hint: You may need more * */ import java.util.Scanner; import java.lang.Math;

Activity 2 String and print it diagonally.

every four spaces in the sample. than one loop.

class Lesson_22_Activity_Two { public static void main(String[] args) { //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

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.util.Scanner; 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 = 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

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. * */ import java.util.Scanner; 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

8

Lesson 1011 - 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: * 123 * 83 * * Sample Run 3: * Enter a number in base 8: * 1111111111 * ERROR: Incorrect Octal Format * */ import java.util.Scanner; 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)