mirror of
https://github.com/System-End/APCSA.git
synced 2026-04-19 16:38:24 +00:00
u4
This commit is contained in:
parent
a213dc2f88
commit
cbe9180219
18 changed files with 472 additions and 0 deletions
78
U4/Assignment4.java
Normal file
78
U4/Assignment4.java
Normal file
|
|
@ -0,0 +1,78 @@
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
class Assignment4 {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
System.out.println("Type the message to be shortened");
|
||||||
|
String message = scanner.nextLine();
|
||||||
|
message = message.toLowerCase();
|
||||||
|
|
||||||
|
int vowelsRemoved = 0;
|
||||||
|
int repeatsRemoved = 0;
|
||||||
|
String alg1 = "";
|
||||||
|
String vowels = "aeiou";
|
||||||
|
|
||||||
|
for (int i = 0; i < message.length(); i++) {
|
||||||
|
String c = message.substring(i, i + 1);
|
||||||
|
boolean isVowel = vowels.contains(c);
|
||||||
|
boolean isAtStartOfWord =
|
||||||
|
(i == 0) || message.substring(i - 1, i).equals(" ");
|
||||||
|
|
||||||
|
if (isVowel && !isAtStartOfWord) {
|
||||||
|
vowelsRemoved++;
|
||||||
|
} else if (
|
||||||
|
!isVowel &&
|
||||||
|
i > 0 &&
|
||||||
|
!c.equals(" ") &&
|
||||||
|
c.equals(message.substring(i - 1, i))
|
||||||
|
) {
|
||||||
|
repeatsRemoved++;
|
||||||
|
} else {
|
||||||
|
alg1 += c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("\nAlgorithm 1");
|
||||||
|
System.out.println("Vowels removed: " + vowelsRemoved);
|
||||||
|
System.out.println("Repeats removed: " + repeatsRemoved);
|
||||||
|
System.out.println("Algorithm 1 message: " + alg1);
|
||||||
|
System.out.println(
|
||||||
|
"Algorithm 1 characters saved: " +
|
||||||
|
(message.length() - alg1.length())
|
||||||
|
);
|
||||||
|
|
||||||
|
String alg2 = "";
|
||||||
|
int uniqueChars = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < message.length(); i++) {
|
||||||
|
String c = message.substring(i, i + 1);
|
||||||
|
if (c.equals(" ")) continue;
|
||||||
|
|
||||||
|
boolean alreadySeen = false;
|
||||||
|
for (int j = 0; j < i; j++) {
|
||||||
|
if (c.equals(message.substring(j, j + 1))) {
|
||||||
|
alreadySeen = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!alreadySeen) {
|
||||||
|
uniqueChars++;
|
||||||
|
int count = 0;
|
||||||
|
for (int k = 0; k < message.length(); k++) {
|
||||||
|
if (c.equals(message.substring(k, k + 1))) count++;
|
||||||
|
}
|
||||||
|
alg2 += count + c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("\nAlgorithm 2");
|
||||||
|
System.out.println("Unique characters found: " + uniqueChars);
|
||||||
|
System.out.println("Algorithm 2 message: " + alg2);
|
||||||
|
System.out.println(
|
||||||
|
"Algorithm 2 characters saved: " +
|
||||||
|
(message.length() - alg2.length())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
18
U4/L1.5/U4_L1_5_Activity_One.java
Normal file
18
U4/L1.5/U4_L1_5_Activity_One.java
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
/* Lesson 1 1/2 Coding Activity Question 2 */
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class U4_L1_5_Activity_Two {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
int n = scanner.nextInt();
|
||||||
|
int sum = 0;
|
||||||
|
|
||||||
|
for (int i = 1; i <= n; i++) {
|
||||||
|
sum += i;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println(sum);
|
||||||
|
}
|
||||||
|
}
|
||||||
17
U4/L1.5/U4_L1_5_Activity_Two.java
Normal file
17
U4/L1.5/U4_L1_5_Activity_Two.java
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
/* Lesson 1 1/2 Coding Activity Question 2 */
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class U4_L1_5_Activity_Two {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int n = 5;
|
||||||
|
int sum = 0;
|
||||||
|
|
||||||
|
for (int i = 1; i <= n; i++) {
|
||||||
|
sum += i;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Sum of first " + n + " positive integers: " + sum);
|
||||||
|
}
|
||||||
|
}
|
||||||
21
U4/L1/U4_L1_Activity_One.java
Normal file
21
U4/L1/U4_L1_Activity_One.java
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
/* Lesson 1 Coding Activity Question 1 */
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class U4_L1_Activity_One {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scan = new Scanner(System.in);
|
||||||
|
int sum = 0;
|
||||||
|
|
||||||
|
System.out.println("Enter any numbers (Enter 5 to stop)");
|
||||||
|
int num = scan.nextInt();
|
||||||
|
|
||||||
|
while (num != 5) {
|
||||||
|
sum += num;
|
||||||
|
num = scan.nextInt();
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Sum is " + sum);
|
||||||
|
}
|
||||||
|
}
|
||||||
24
U4/L1/U4_L1_Activity_Three.java
Normal file
24
U4/L1/U4_L1_Activity_Three.java
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
/* Lesson 1 Coding Activity Question 3 */
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class U4_L1_Activity_Three {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scan = new Scanner(System.in);
|
||||||
|
System.out.println("Input a word:");
|
||||||
|
String word = scan.nextLine();
|
||||||
|
String result = "";
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
while (i + 1 < word.length()) {
|
||||||
|
result += word.substring(i, i + 2);
|
||||||
|
i += 3;
|
||||||
|
}
|
||||||
|
if (i < word.length()) {
|
||||||
|
result += word.substring(i, i + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
23
U4/L1/U4_L1_Activity_Two.java
Normal file
23
U4/L1/U4_L1_Activity_Two.java
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
/* Lesson 1 Coding Activity Question 2 */
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class U4_L1_Activity_Two {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scan = new Scanner(System.in);
|
||||||
|
int largest = Integer.MIN_VALUE;
|
||||||
|
|
||||||
|
System.out.println("Enter the Scores:");
|
||||||
|
int score = scan.nextInt();
|
||||||
|
|
||||||
|
while (score != -1) {
|
||||||
|
if (score > largest) {
|
||||||
|
largest = score;
|
||||||
|
}
|
||||||
|
score = scan.nextInt();
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("\nThe largest score is " + largest);
|
||||||
|
}
|
||||||
|
}
|
||||||
22
U4/L2/U4_L2_Activity_One.java
Normal file
22
U4/L2/U4_L2_Activity_One.java
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
/* Lesson 2 Coding Activity Question 1 */
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class U4_L2_Activity_One
|
||||||
|
{
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
System.out.println("Enter two numbers:");
|
||||||
|
int a = scanner.nextInt();
|
||||||
|
int b = scanner.nextInt();
|
||||||
|
|
||||||
|
int i = a;
|
||||||
|
while (i <= b) {
|
||||||
|
if (i % 2 != 0) {
|
||||||
|
System.out.print(i + " ");
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
44
U4/L2/U4_L2_Activity_Three.java
Normal file
44
U4/L2/U4_L2_Activity_Three.java
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
/* Lesson 2 Coding Activity Question 3 */
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class U4_L2_Activity_Three {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
|
||||||
|
double maxLat = -90;
|
||||||
|
double minLat = 90;
|
||||||
|
double maxLon = -180;
|
||||||
|
double minLon = 180;
|
||||||
|
|
||||||
|
int continueInput = 1;
|
||||||
|
|
||||||
|
while (continueInput == 1) {
|
||||||
|
System.out.println("Please enter the longitude:");
|
||||||
|
double lon = scanner.nextDouble();
|
||||||
|
|
||||||
|
System.out.println("Please enter the latitude:");
|
||||||
|
double lat = scanner.nextDouble();
|
||||||
|
|
||||||
|
if (lat < -90 || lat > 90 || lon < -180 || lon > 180) {
|
||||||
|
System.out.println("Incorrect Latitude or Longitude");
|
||||||
|
} else {
|
||||||
|
if (lat > maxLat) maxLat = lat;
|
||||||
|
if (lat < minLat) minLat = lat;
|
||||||
|
if (lon > maxLon) maxLon = lon;
|
||||||
|
if (lon < minLon) minLon = lon;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Would you like to enter another location?");
|
||||||
|
continueInput = scanner.nextInt();
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Farthest North: " + maxLat);
|
||||||
|
System.out.println("Farthest South: " + minLat);
|
||||||
|
System.out.println("Farthest East: " + maxLon);
|
||||||
|
System.out.println("Farthest West: " + minLon);
|
||||||
|
|
||||||
|
scanner.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
20
U4/L2/U4_L2_Activity_Two.java
Normal file
20
U4/L2/U4_L2_Activity_Two.java
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
/* Lesson 2 Coding Activity Question 2 */
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class U4_L2_Activity_Two {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
System.out.println("Enter a positive integer:");
|
||||||
|
int n = scanner.nextInt();
|
||||||
|
|
||||||
|
int place = 1;
|
||||||
|
while (n > 0) {
|
||||||
|
int digit = n % 10;
|
||||||
|
System.out.println(digit * place);
|
||||||
|
place *= 10;
|
||||||
|
n /= 10;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
23
U4/L3/U4_L3_Activity_Four.java
Normal file
23
U4/L3/U4_L3_Activity_Four.java
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
/* Lesson 3 Coding Activity Question 4 */
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class U4_L3_Activity_Four {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
|
||||||
|
System.out.println("Enter a positive integer:");
|
||||||
|
int num = scanner.nextInt();
|
||||||
|
|
||||||
|
if (num <= 0) {
|
||||||
|
System.out.println("error");
|
||||||
|
} else {
|
||||||
|
int start = num - (num % 3);
|
||||||
|
for (int i = start; i >= 0; i -= 3) {
|
||||||
|
System.out.print(i + " ");
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
12
U4/L3/U4_L3_Activity_One.java
Normal file
12
U4/L3/U4_L3_Activity_One.java
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
/* Lesson 3 Coding Activity Question 1 */
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class U4_L3_Activity_One {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
for (int i = 1; i <= 25; i += 2) {
|
||||||
|
System.out.println(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
28
U4/L3/U4_L3_Activity_Three.java
Normal file
28
U4/L3/U4_L3_Activity_Three.java
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
/* Lesson 3 Coding Activity Question 3 */
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class U4_L3_Activity_Three {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
|
||||||
|
System.out.println("Enter a number between 0 and 50:");
|
||||||
|
int num = scanner.nextInt();
|
||||||
|
|
||||||
|
if (num <= 0 || num >= 50) {
|
||||||
|
System.out.println("error");
|
||||||
|
} else {
|
||||||
|
for (int i = num; i <= 50; i++) {
|
||||||
|
System.out.print(i);
|
||||||
|
if (i == 50) {
|
||||||
|
System.out.println();
|
||||||
|
} else if ((i - num) % 5 == 4) {
|
||||||
|
System.out.println();
|
||||||
|
} else {
|
||||||
|
System.out.print(" ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
19
U4/L3/U4_L3_Activity_Two.java
Normal file
19
U4/L3/U4_L3_Activity_Two.java
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
/* Lesson 3 Coding Activity Question 2 */
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class U4_L3_Activity_Two {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
for (int i = 17; i <= 73; i++) {
|
||||||
|
System.out.print(i);
|
||||||
|
if (i == 73) {
|
||||||
|
System.out.println();
|
||||||
|
} else if ((i - 17) % 10 == 9) {
|
||||||
|
System.out.println();
|
||||||
|
} else {
|
||||||
|
System.out.print(" ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
34
U4/L4/U4_L4_Activity_One.java
Normal file
34
U4/L4/U4_L4_Activity_One.java
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
/* Lesson 4 Coding Activity Question 1 */
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class U4_L4_Activity_One {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
|
||||||
|
System.out.println("Input String:");
|
||||||
|
String input = scanner.nextLine();
|
||||||
|
String lower = input.toLowerCase();
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
|
for (int i = 0; i < lower.length() - 1; i++) {
|
||||||
|
if (lower.charAt(i) == 'p') {
|
||||||
|
char next = lower.charAt(i + 1);
|
||||||
|
if (
|
||||||
|
next == 'a' ||
|
||||||
|
next == 'e' ||
|
||||||
|
next == 'i' ||
|
||||||
|
next == 'o' ||
|
||||||
|
next == 'u'
|
||||||
|
) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println(
|
||||||
|
"Contains p followed by a vowel " + count + " times."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
25
U4/L4/U4_L4_Activity_Three.java
Normal file
25
U4/L4/U4_L4_Activity_Three.java
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
/* Lesson 4 Coding Activity Question 3 */
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class U4_L4_Activity_Three {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
|
||||||
|
System.out.println("Enter two strings:");
|
||||||
|
String first = scanner.nextLine();
|
||||||
|
String second = scanner.nextLine();
|
||||||
|
|
||||||
|
if (first.length() != second.length()) {
|
||||||
|
System.out.println("error");
|
||||||
|
} else {
|
||||||
|
String result = "";
|
||||||
|
for (int i = first.length() - 1; i >= 0; i--) {
|
||||||
|
result += second.charAt(i);
|
||||||
|
result += first.charAt(i);
|
||||||
|
}
|
||||||
|
System.out.println(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
24
U4/L4/U4_L4_Activity_Two.java
Normal file
24
U4/L4/U4_L4_Activity_Two.java
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
/* Lesson 4 Coding Activity Question 2 */
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class U4_L4_Activity_Two {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
|
||||||
|
System.out.println("Enter a string:");
|
||||||
|
String input = scanner.nextLine();
|
||||||
|
String lower = input.toLowerCase();
|
||||||
|
|
||||||
|
String result = "";
|
||||||
|
for (int i = 0; i < lower.length(); i++) {
|
||||||
|
char c = lower.charAt(i);
|
||||||
|
if (c != 'e' && c != 't' && c != 'a' && c != 'i' && c != 'o') {
|
||||||
|
result += c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
25
U4/L5/U4_L5_Activity_One.java
Normal file
25
U4/L5/U4_L5_Activity_One.java
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
/* Lesson 5 Coding Activity Question 1 */
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class U4_L5_Activity_One {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
|
||||||
|
System.out.println("Input a String:");
|
||||||
|
String input = scanner.nextLine();
|
||||||
|
|
||||||
|
System.out.println("Input an integer:");
|
||||||
|
int n = scanner.nextInt();
|
||||||
|
|
||||||
|
String result = "";
|
||||||
|
for (int i = 0; i < input.length(); i++) {
|
||||||
|
for (int j = 0; j < n; j++) {
|
||||||
|
result += input.charAt(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
15
U4/L5/U4_L5_Activity_Two.java
Normal file
15
U4/L5/U4_L5_Activity_Two.java
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
/* Lesson 5 Coding Activity Question 2 */
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class U4_L5_Activity_Two {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
for (int i = 10; i >= 1; i--) {
|
||||||
|
for (int j = 0; j < i; j++) {
|
||||||
|
System.out.print(i + " ");
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue