Java Coding activities

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

T1L2 Coding activity 3 Write the code to output: - / \ | | \ / -

class Lesson_2_Activity_Three { public static void main(String[] args) { System.out.println (" _"); System.out.println (" / \\"); System.out.println ("| |"); System.out.println (" \\ _ /"); } }

T1L12 Coding activity 4 You are running an experiment that involves hatching chicken eggs. Bird eggs are very sensitive to temperature and chickens' eggs will hatch between 99 and 102 degrees Fahrenheit. Write the code for the sensor that will be tracking the temperature. If the temperature falls below 99 or above 102 your code should print "WARNING". The input should be in doubles.

import java.util.Scanner; class Lesson_12_Activity_Four{ public static void main(String[] args) { Scanner scan = new Scanner(System.in); double temp; System.out.print("What is the temperature?"); temp = scan.nextDouble(); if (temp<99 || temp>102){ System.out.println("WARNING"); } else{ System.out.println(temp); } } }

T1L14 Coding activity 1 Test if an integer is not between 5 and 76 inclusive.

import java.util.Scanner; class Lesson_14_Activity_One { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter a number:"); int x = scan.nextInt(); if (x>=5 && x<=76) System.out.println("False"); else System.out.println("True"); } }

T1L11 Coding activity 4 Test if an integer input from the keyboard is odd or even.

import java.util.Scanner; import java.lang.Math; class Lesson_11_Activity_Four { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Please enter an integer."); int X = scan.nextInt(); int Y = (X % 2); if (Y==0){ System.out.println ("Even"); } else { System.out.println ("Odd"); } } }

T1L2 Coding activity 2 Using only one "System.out.print" command, print the following quote. Make sure to include the quote marks (") in your output. "I do not fear computers. I fear the lack of them." Isaac Asimov

import java.util.Scanner; import java.lang.Math; class Lesson_2_Activity_Two { public static void main(String[] args) { System.out.println ("\"I do not fear computers. I fear the lack of them.\"\nIsaac Asimov"); } }

T1L5 Coding activity 1 Input two double values and print the difference between them. (The difference can be found by subtracting the second value from the first).

import java.util.Scanner; import java.lang.Math; class Lesson_5_Activity_One { public static void main(String[] args) { Scanner scan = new Scanner (System.in); double num1 = scan.nextDouble(); double num2 = scan.nextDouble(); System.out.println( 1.0 * (num2-num1)); } }

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

import java.util.Scanner; import java.lang.Math; class Lesson_17_Activity_Two { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println ("Enter two numbers:"); int x = scan.nextInt(); int y = scan.nextInt(); int d = x; while ( d <= y) { if ( d % 2 == 0) System.out.print( d + " "); d ++; } } }

T1L33 Coding Activity 2 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.lang.Math; import java.util.Scanner; class Lesson_33_Activity_Two { public static void randomize(int x []) { for (int i = 0; i < x.length; i++) { x[i] = (int)(Math.random() * 90 ) + 10; } } public static void main(String[] args) { int list[] = {1,2,3,4,5,6,7,8,9}; randomize(list); } }

T1L30 Coding activity 1 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", "fo od ", "stand", "valu e "} It should be changed to hold: {"every", "nearing", "checking", "food", "stand", "value"}

import java.util.Scanner ;import java.util.Arrays; class Lesson_30_Activity { public static String [] list = {"every", " near ing ", " checking", "food ", "stand", "value "}; public static void main(String[] args) { int y = 0; for(int i = 0; i < list.length; i++) { for (int x = 0; x < list[i].length(); x++) { if(list[i].charAt(x)== ' '){ list[i] = list[i].substring(0,x) + list[i].substring(x+1); } } } } }

T1L34 Coding Activity 1 Write a method that takes an array of ints as a parameter and returns the sum of integers in the array. This method must be named sum() and it must have an int[] parameter. This method must return an int. Calling sum(a) would return 6 if a = {1, 2, 3} or 3 if a = {1, 1, 1}.

import java.util.Scanner; //class Main { class Lesson_34_Activity_One { public static int sum(int [] a) { int sums = 0; for (int i = 0; i < a.length; i++) sums += a[i]; return sums; } public static void main(String[] args) { int a []= {1,2,3,4}; int x = sum(a); System.out.println(x); } }

T1L12 Coding activity 1 Write a program to input three integers. Test if the average is greater than or equal to 89.5. If so, print out the phrase "ABOVE AVERAGE"

import java.util.Scanner; class Lesson_12_Activity_One { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println ("Please input three integers"); int x = scan.nextInt(); int y = scan.nextInt(); int z = scan.nextInt(); double avg = 1.0 * (x + y + z)/3; if ( avg >= 89.5){ System.out.println ("ABOVE AVERAGE"); } } }

T1L12 Coding activity 3 Test if a number input from the keyboard is a valid test score (between 0 and 100 inclusive).

import java.util.Scanner; class Lesson_12_Activity_Three { public static void main(String[] args) { Scanner scan = new Scanner(System.in); double test; System.out.print("Enter test score"); test=scan.nextDouble(); System.out.println(test); if(test>=0 && test<=100){ System.out.print("Valid"); } else{ System.out.print("Not Valid"); } } }

T1L12 Coding activity 2 Input two decimal numbers and print the largest. If the numbers are equal, print one of them.

import java.util.Scanner; class Lesson_12_Activity_Two { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println ("Please enter two numbers:"); double x = scan.nextDouble(); double y = scan.nextDouble(); if (x>y) { System.out.println ("Largest is: " + x ); } if (y>x) { System.out.println ("Largest is: " + y ); } if (x==y) { System.out.println ("Largest is: " + x ); } } }

T1L13 Coding activity 5 Create a program to let the user practice their multiplication tables. Print two random integers between 1 and 12 each on a new line. Then, ask the user to input the multiplication of the two numbers. If they enter the correct product print "Correct!" otherwise print "Wrong".

import java.util.Scanner; class Lesson_13_Activity_Five { public static void main(String[] args) { Scanner scan = new Scanner(System.in); double x = Math.random(); int range = 12; int min = 1; int y = (int)(x*12)+1; System.out.println(y); double z = Math.random(); int b = (int)(z * 12)+1; System.out.println(b); int a = (b * y); int ab = scan.nextInt(); if (a == ab) { System.out.println ("Correct!"); } else System.out.println("Wrong"); } }

T1L13 Coding activity 4 Input an integer grade from the keyboard and translate it to a letter grade. For example, if a user enters 4, print "A", and if they enter 0 print "F." You can assume that an integer between 0 and 4 will be input.

import java.util.Scanner; class Lesson_13_Activity_Four { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int x = scan.nextInt(); if (x == 0) { System.out.println("F"); } if (x == 1) { System.out.println("D"); } if (x == 2) { System.out.println("C"); } if (x == 3) { System.out.println("B"); } if (x == 4) { System.out.println("A"); } } }

T1L13 Coding activity 1 Take input of an integer number from the keyboard and print "Positive" if it's positive or zero, and print "Negative" otherwise.

import java.util.Scanner; class Lesson_13_Activity_One { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int x = scan.nextInt(); if (x >= 0) { System.out.println("Positive"); } else System.out.println("Negative"); } }

T1L13 Coding activity 3 Input two integers and print the largest integer (print just the number, no text). If they are equal, print only the word "EQUAL". You should be able to do this with only one if statement but you may use multiple else statements.

import java.util.Scanner; class Lesson_13_Activity_Three { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int x = scan.nextInt(); int y = scan.nextInt(); if (x > y) { System.out.println(x); } if (x < y) { System.out.println(y); } if (x == y) { System.out.println("EQUAL"); } } }

T1L13 Coding activity 2 Take input of an integer number from the keyboard and print "passing" if it's greater than or equal to 60, and print "failing" otherwise.

import java.util.Scanner; class Lesson_13_Activity_Two { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int x = scan.nextInt(); if (x >= 60) { System.out.println("passing"); } else System.out.println("failing"); } }

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

import java.util.Scanner; class Lesson_14_Activity_Two { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println("Enter two integers: "); int a = s.nextInt(); int b = s.nextInt(); if (a >= 0 && b >= 0) { System.out.println("Both are positive or zero."); } else { System.out.println("One or both are negative."); } } }

T1L32 Coding Activity 4 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:

import java.util.Scanner; class Lesson_32_Activity_Four { public static void realTime(int a) { System.out.println("Hours: " + a / 3600); int b = a % 3600; System.out.println("Minutes: " + b / 60); int c = b % 60; System.out.println("Seconds: " + c); } public static void main(String[] args) { Scanner scan = new Scanner(System.in); int a = scan.nextInt(); realTime(a); } }

T1L32 Coding Activity 1 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.util.Scanner; class Lesson_32_Activity_One { public static void monthName(int x ) { String months [] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; System.out.println (months[x-1]); } public static void main(String[] args) { Scanner scan = new Scanner(System.in); int x = scan.nextInt(); monthName(x); } }

T1L32 Coding Activity 3 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.util.Scanner; class Lesson_32_Activity_Three { public static void swap(int a, int b ) { System.out.print (b); System.out.print (" " + a); } public static void main(String[] args) { Scanner scan = new Scanner(System.in); int a = scan.nextInt(); int b = scan.nextInt(); swap(a, b); } }

T1L32 Coding Activity 2 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 a integer parameter. Calling monthDays(2) would print 28 and monthDays(9) would print 30.

import java.util.Scanner; class Lesson_32_Activity_Two { public static void monthDays(int x ) { int monthdays [] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; System.out.println (monthdays[x-1]); } public static void main(String[] args) { Scanner scan = new Scanner(System.in); int x = scan.nextInt(); monthDays(x); } }

T1L33 Coding Activity 5 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:

import java.util.Scanner; class Lesson_33_Activity_Five { public static void insertValue(int[] intarray, int value, int index){ for(int i = (intarray.length-1); i > index; i--){ intarray[i]= intarray[(i-1)]; } intarray[index] = value; } public static void main(String[] args) { int[] numbers = {1,2,3,4,5,6}; insertValue(numbers, 2, 2); for(int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } } }

T1L33 Coding Activity 4 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.util.Scanner; class Lesson_33_Activity_Four { public static void reverse(int list[]){ int[] newArray = new int[list.length]; for(int i=0; i<list.length;i++){ newArray[i]=list[i]; } for(int y=0; y<list.length;y++){ list[y]=newArray[list.length-(y+1)]; } } public static void main(String[] args) { int [] list = {1,2,3,4,5,6}; reverse(list); for (int x=0; x < list.length; x++) { System.out.print(list[x] + " "); } } }

T1L33 Coding Activity 1 Write a method that takes an array of Strings and changes the Strings to UPPER CASE. This method must be called upper() and it must take a String[] parameter.

import java.util.Scanner; class Lesson_33_Activity_One { public static void upper(String x[]) { for (int i = 0; i < x.length; i ++) { x[i] = x[i].toUpperCase(); } } public static void main(String[] args) { String list[] = {"Karina", "Carlos", "Yulong"}; upper(list); } }

T1L33 Coding Activity 3 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.util.Scanner; class Lesson_33_Activity_Three { public static void printit(int list[]){ for (int i=0; i<list.length; i++){ System.out.print(list[i] + " "); } } public static void main(String[] args) { } }

T1L34 Coding Activity 2 Write a method that takes an array of ints as a parameter and returns the average value of the array as a double. This method must be named average() and it must have an int[] parameter. This method must return a double. Calling average(a) would return 2.0 if a = {1, 2, 3} or 1.0 if a = {1, 1, 1}.

import java.util.Scanner; class Lesson_34_Activity_Two {// class Main { public static double average(int [] a) { int sum = 0; for (int i = 0; i < a.length; i++){ sum += a[i]; } double ave = (1.0) * (sum)/(a.length); return ave; } public static void main(String[] args) { int list [] = {1,2,3,4}; System.out.println((double)(average(list))); } }

T1L11 Coding activity 1 Test if an integer input from the keyboard is equal to the integer 176. If it is, print "YES" (without the quotes).

import java.util.Scanner; import java.lang.Math; class Lesson_11_Activity_One { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Please enter an integer."); int X = scan.nextInt(); if(X==176){ System.out.print ("YES"); } } }

T1L11 Coding activity 3 Get two integers from the keyboard and test if they are equal. If they are, print "YES"

import java.util.Scanner; import java.lang.Math; class Lesson_11_Activity_Three { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Please enter two integers."); double X = scan.nextDouble(); double Y = scan.nextDouble(); if(X==Y){ System.out.print ("YES"); } } }

T1L11 Coding activity 2 Test if a decimal value input from the keyboard is equal to 48.729. If it is, print "YES" (without the quotes).

import java.util.Scanner; import java.lang.Math; class Lesson_11_Activity_Two { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Please enter an integer."); double X = scan.nextDouble(); if(X==48.729){ System.out.print ("YES"); } } }

T1L1 Coding activity 1 Write a program to print your name to the center of the output screen

import java.util.Scanner; import java.lang.Math; class Lesson_1_Activity_One { public static void main(String[] args) { System.out.println(" Hello"); System.out.println(" Carlos"); } }

T1L1 Coding activity 3 Write a program to output the following: ******** **java** ********

import java.util.Scanner; import java.lang.Math; class Lesson_1_Activity_Three { public static void main(String[] args) { System.out.println("********"); System.out.println("* java *"); System.out.println("********"); } }

T1L1 Coding activity 2 Write a program to print your name to the center of the output screen on three different lines

import java.util.Scanner; import java.lang.Math; class Lesson_1_Activity_Two { public static void main(String[] args) { System.out.println(" Car "); System.out.println(" Los"); System.out.println(" Jurado"); } }

T1L2 Coding activity 1 Print the following shape: /\ \/

import java.util.Scanner; import java.lang.Math; class Lesson_2_Activity_One { public static void main(String[] args) { System.out.println ("/\\"); System.out.println ("\\/"); } }

T1L3 Coding activity 1 Write the code to ask the user to enter their name and print the following message: Hi ______, nice to see you.

import java.util.Scanner; import java.lang.Math; class Lesson_3_Activity_One { public static void main(String[] args) { Scanner scan= new Scanner (System.in); String n; System.out.println ("What is your name?"); n = scan.nextLine(); System.out.println ("Hi " + n + ", nice to see you."); } }

T1L3 Coding activity 3 Write a program that will ask the user to enter an adjective and a name. Print the following sentence, replacing the ______ with the words they entered. My name is _____. I am _____.

import java.util.Scanner; import java.lang.Math; class Lesson_3_Activity_Three { public static void main(String[] args) { Scanner scan = new Scanner (System.in); String name; String adjective; System.out.println("Hi there. What is your name?"); name = scan.nextLine(); System.out.println("What adjective describes you?"); adjective = scan.nextLine(); System.out.println("My name is " + name + ". I am " + adjective + "."); } }

T1L3 Coding activity 2 Write a program that asks the user for three names, then prints the names in reverse order. Sample Run: Please enter three names: Zoey Zeb Zena Zena Zeb Zoey

import java.util.Scanner; import java.lang.Math; class Lesson_3_Activity_Two { public static void main(String[] args) { Scanner scan= new Scanner (System.in); String name1; String name2; String name3; System.out.println ("Please enter three names:"); name1 =scan.nextLine(); name2 =scan.nextLine(); name3 =scan.nextLine(); System.out.println (name3+" "+name2+" "+name1); } }

T1L4 Coding activity 1 Input two doubles and print them backward on the screen. So if the user enters: 1.3 6.8 It should output: 6.8 1.3

import java.util.Scanner; import java.lang.Math; class Lesson_4_Activity_One { public static void main(String[] args) { Scanner scan = new Scanner (System.in); double X = scan.nextDouble(); double Y = scan.nextDouble(); System.out.println (Y + " " + X); } }

T1L4 Coding activity 2 Ask the user their name and age and print out how many years until they are 100. Note that age should be an integer, not a double. Sample run: Hi there. What is your name? Pascal Hi Pascal. How old are you? 16 Pascal, you will be 100 in 84 years.

import java.util.Scanner; import java.lang.Math; class Lesson_4_Activity_Two { public static void main(String[] args) { Scanner scan = new Scanner (System.in); System.out.println ("Hi there. What is your name?"); String x = scan.nextLine(); System.out.println ("Hi " + x + ". How old are you?"); int y = scan.nextInt(); System.out.println (x + ", you will be 100 in " + (100-y) + " years."); } }

T1L5 Coding activity 4 Ask the user to enter the price of an item and tell them how much money they would get back from $20. It's okay to return a negative number.

import java.util.Scanner; import java.lang.Math; class Lesson_5_Activity_Four { public static void main(String[] args) { Scanner scan = new Scanner(System.in); Double price; System.out.println ("Enter a price:"); price = scan.nextDouble(); System.out.println ("Change from $20: $" + (20 - price)); } }

T1L5 Coding activity 3 Input the radius of a circle and print the circumference and area. The answer should be a decimal value. Sample Run: Enter a radius:4 Circumference: 25.12 Area: 50.24

import java.util.Scanner; import java.lang.Math; class Lesson_5_Activity_Three { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println ("Enter a radius"); double radius; radius = scan.nextDouble(); System.out.println ("Circumference: " + (1.0 * radius * 2 * 3.14)); System.out.println ("Area: " + ( 1 * 3.14 * radius * radius)); } }

T1L5 Coding activity 2 Input four integer values and print the sum of all four values.

import java.util.Scanner; import java.lang.Math; class Lesson_5_Activity_Two { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int num1 = scan.nextInt(); int num2 = scan.nextInt(); int num3 = scan.nextInt(); int num4 = scan.nextInt(); System.out.println ( 1.0 * (num1 + num2+ num3 + num4)); } }

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

import java.util.Scanner; import java.lang.Math; class Lesson_29_Activity_One { public static String [] list = {}; public static void main(String[] args) { int s = 0; for(int i=0;i<list.length;i++) { if (list[i].length()>list[s].length()) s = i; } System.out.print(list[s]); } }

T1L6 Coding activity 1 Write a Java program that precisely calculates the average between two integers that input from the keyboard, and prints the answer to the screen. The average must include the integer and decimal portions of the calculation. Sample Run: Please enter two integers: 4 11 The average is: 7.5

import java.util.Scanner; import java.lang.Math; class Lesson_6_Activity_One { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println ("Please enter two intergers"); int num1 = scan.nextInt(); int num2 = scan.nextInt(); int num3 = (num1 + num2); Double num4 = (double)num3/2; System.out.println("The average is: " + num4); } }

T1L6 Coding activity 3 Input a double and print the first two digits after the decimal point. Sample run: Please input a decimal number: 57.8934 Answer: 89

import java.util.Scanner; import java.lang.Math; class Lesson_6_Activity_Three { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println ("Please input a decimal number:"); double x = scan.nextDouble(); int y = (int)x; double z = (x - y); double h = (100 * z); System.out.println ("Answer: " + (int)h); } }

T1L6 Coding activity 2 Input a double value and print only the integer part. Sample run: Please input a decimal number: 57.8934 Answer: 57

import java.util.Scanner; import java.lang.Math; class Lesson_6_Activity_Two { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println ("Please input a decimal number:"); double x = scan.nextDouble(); int y = (int)x; System.out.println ("Answer: " + y); } }

T1L7 Coding activity 1 Input a positive three digit integer. Print out the digits one per line.

import java.util.Scanner; import java.lang.Math; class Lesson_7_Activity_One { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println ("Please enter a three digit number:"); int x = scan.nextInt(); System.out.println ("Here are the digits:"); System.out.println(x / 100); int y = (x/10 ); System.out.println(y % 10 ); System.out.println( x % 10); } }

T1L7 Coding activity 2 Change the last problem so that it also prints the sum of the digits. Use the format shown below. Make sure your output is printed in the same order as the sample run.

import java.util.Scanner; import java.lang.Math; class Lesson_7_Activity_Two { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Please enter a positive three digit number:"); int num = scan.nextInt(); System.out.println("Here are the digits:"); int d1 = num /100; num = num % 100; int d2 = num /10; num = num % 10; int d3 = num / 1; num = num % 1; System.out.println(d1); System.out.println(d2); System.out.println(d3); int sum = d1 + d2 +d3; System.out.println(d1 + " + "+ d2 +" + "+ d3 + " = " + sum); } }

T1L9 Coding activity 1 Write the code to print a random integer from 1 to 10 inclusive using Math.random().

import java.util.Scanner; import java.lang.Math; class Lesson_9_Activity_One { public static void main(String[] args) { double answer1 = Math.random(); int range = 10; int min = 1; int answer2 = (int)(answer1 * range) + min; System.out.println (answer2); } }

T1L9 Coding activity 3 Write the code to print a random integer from 20 to 40 inclusive using Math.random().

import java.util.Scanner; import java.lang.Math; class Lesson_9_Activity_Three { public static void main(String[] args) { double answer1 = Math.random(); int answer2 = (int)(answer1 * 21) + 20; System.out.println (answer2); } }

T1L9 Coding activity 2 Write the code to print a random integer from -20 to 20 inclusive using Math.random().

import java.util.Scanner; import java.lang.Math; class Lesson_9_Activity_Two { public static void main(String[] args) { double answer1 = Math.random(); int answer2 = (int)(answer1 * 41) + -20; System.out.println (answer2); } }

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

import java.util.Scanner; import java.lang.Math; class Lesson_17_Activity_One { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println ("Enter the Scores:"); int c = 0; int sum = 0; int x = 0; while (x != -1){ System.out.println (x); sum = sum + x; x = scan.nextInt(); c ++; } c--; System.out.println ("Average: " + (double)sum/c); } }

T1L22 Coding activity 1 Write the code to take a String and print it with one letter per line.

import java.util.Scanner; import java.lang.Math; class Lesson_22_Activity_One { public static void main(String[] args) { Scanner Scan = new Scanner(System.in); System.out.println("Enter a string:"); String num = Scan.nextLine(); int w = num.length(); int p = 0; while (p < w) { System.out.println(num.charAt(p)); p ++; } } }

T1L22 Coding activity 2 Write the code to take a String and print it diagonally.

import java.util.Scanner; import java.lang.Math; class Lesson_22_Activity_Two { public static void main(String[] args) { Scanner Scan = new Scanner(System.in); System.out.println("Enter a string:"); String s = Scan.nextLine(); int w = s.length(); int counter = 0; String space = "\t"; String tab = ""; int x = 0; while (x < s.length()) { System.out.println(tab + s.charAt(x));tab += space;x ++; } } }

T1L24 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) { int counter = 0; for (int i = 23; i <= 89; i++) { counter ++; if (counter == 10) { System.out.print (i + "\n"); counter = 0; } else { System.out.print(i + " "); } } } }

T1L24 Coding activity 3 Input an int between 0 and 100 and print the numbers between it and 100, including the number itself and the number 100. If the number is not between 0 and 100 print "error". Print 20 numbers per line.

import java.util.Scanner; import java.lang.Math; class Lesson_24_Activity_Three { public static void main(String[] args){ Scanner scan = new Scanner(System.in); System.out.println ("Enter a number between 0 and 100:"); int x = scan.nextInt(); if ( x <= 0 || x >= 100) { System.out.println("error"); } else { for (int i = x; i <= 100; i ++) { System.out.print( i + " "); } } } }

T1L24 Coding activity 2 Use a for loop to print the even numbers between 1 and 50, including the first even number, and the number 50. Print each number on a new line.

import java.util.Scanner; import java.lang.Math; class Lesson_24_Activity_Two { public static void main(String[] args) { for (int i=2; i <= 50; i +=2) System.out.println(i); } }

T1L29 Coding activity 2 Write a loop that processes an array of strings. Each String should be printed backwards on its own line.

import java.util.Scanner; import java.util.Arrays; class Lesson_29_Activity_Two { public static String [] list = {}; public static void main(String[] args) { for (int i = 0; i < list.length; i++) { int x = list[i].length()-1; for (int y = list[i].length()-1; y >= 0; y--) { System.out.print(list[i].charAt(x)); x--; } System.out.println(); } } }


Kaugnay na mga set ng pag-aaral

Chapter 17: Public Goods and Common Resources

View Set

Prinicples of Economics - Chapter 7

View Set

investment banking: accounting questions (basic)

View Set

Sociology Unit 1: Sociology and Sociological Theories

View Set