Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CS141 Programming Assignment #4 Due Monday, Oct 31. 1- Write a program to read in 10 numbers and compute the average, maximum and minimum values (using both while and for loop). Solution 1: /*Name: ....... * ID:......... * assignment 4 part 1 (using while loop) */ import java.util.Scanner; public class q1a { public static void main(String[] args) { Scanner input = new Scanner (System.in); int count =1 , x, max, min, sum ; double avg ; System.out.println("please Enter 10 number :"); x= input.nextInt (); // read the first number that entered max=x; min=x; sum=x; while (count < 10) { x= input.nextInt (); sum+=x ; if ( x > max) max=x; else if (x<min) min=x; count ++ ; } avg = (sum /10.0) ; System.out.printf("The average = %f\n",avg); System.out.printf( "The max = %d\n", max); System.out.printf( "The min = %d\n", min); } } Solution 2: /*Name: ....... * ID:......... * assignment 4 part 1 (using For loop) */ import java.util.Scanner; public class q1b { public static void main(String[] args) { Scanner input = new Scanner (System.in); int count , x, max, min, sum ; double avg=0 ; System.out.print ("please enter 10 numbers :"); x= input.nextInt (); //read first number that entered max=x; min=x; sum=x; for (count =1 ;count <10;count ++ ) { x= input.nextInt (); sum+=x ; if ( x > max) max=x; else if (x< min ) min =x; } avg = (sum /10.0); System.out.printf( "The avrage = %f\n",avg); System.out.printf( "The max = %d\n", max); System.out.printf( "The min = %d\n", min); } } 2- Write a program to read in numbers until the number -999 is encountered. The sum of all number read until this point should be printed out (using both while and for loop). Solution 1: /*Name: ....... * ID:......... * assignment 4 part 2 (using while loop) import java.util.Scanner; public class q2a { public static void main(String[] args) { Scanner input = new Scanner (System.in); int x ,sum=0; ; System.out.print("please enter numbers to find its sum ( To stop enter -999): "); x = input.nextInt(); while (x != -999) { sum+=x; x = input.nextInt(); } System.out.printf("The sum of the enterd numbers is : %d\n",sum); } } Solution 2: /*Name: ....... * ID:......... * assignment 4 part 2 (using For loop) */ import java.util.Scanner; public class q2b { public static void main(String[] args) { Scanner input = new Scanner (System.in); int x , sum=0; System.out.print("please enter numbers to find there sum ( To stop enter -999): "); x = input.nextInt(); for (int i =0; x != -999; i ++ ) { sum+=x; x = input.nextInt(); } System.out.printf("The sum of the enterd numbers is : %d\n",sum); } } 3- Write a program to count the vowels and letters in free text given as standard input. Read text a character at a time until you encounter end-of-data. Then print out the number of occurrences of each of the vowels a, e, i, o and u in the text, the total number of letters, and each of the vowels as an integer percentage of the letter total (using both while and for loop). Output format is: Numbers of characters: a 3 ; e 2 ; i 0 ; o 1 ; u 0 ; rest 17 Percentages of total: a 13%; e 8%; i 0%; o 4%; u 0%; rest 73% Solution 1: /*Name: ....... * ID:......... * assignment 4 part 3 (using while loop) import java.util.Scanner; public class q3a { public static void main(String[] args) { Scanner input = new Scanner(System.in); String line; int ctra = 0; int ctre = 0; int ctri = 0; int ctro = 0; int ctru = 0; int ctrrest = 0; int ctr = 0; int allChars; System.out.print("Enter your line of text: "); line = input.nextLine(); line= line.toLowerCase(); while (ctr < line.length()) { if (line.charAt(ctr) == 'a') ctra++; else if (line.charAt(ctr) == ctre++; else if (line.charAt(ctr) == ctri++; else if (line.charAt(ctr) == ctro++; else if (line.charAt(ctr) == ctru++; else ctrrest++; ctr++; } 'e') 'i') 'o') 'u') allChars = ctra + ctre + ctri + ctro + ctru + ctrrest; System.out.println("Numbers of characters:"); System.out.printf("a %2d ; e %2d ; i %2d ; o %2d ; u %2d ; rest %2d\n", ctra, ctre, ctri, ctro, ctru, ctrrest); System.out.println("Percentages of total:"); System.out.printf("a %2d%%; e %2d%%; i %2d%%; o %2d%%; u %2d%%; rest %2d%%\n",(ctra*100/allChars), (ctre*100/allChars),(ctri*100/allChars), (ctro*100/allChars),(ctru*100/allChars), (ctrrest*100/allChars)); } } Solution 2: /*Name: ....... * ID:......... * assignment 4 part 3 (using For loop) */ import java.util.Scanner; public class q3b { public static void main(String[] args) { Scanner input = new Scanner(System.in); String line; int ctra = 0; int ctre = 0; int ctri = 0; int ctro = 0; int ctru = 0; int ctrrest = 0; int allChars; System.out.print("Enter your line of text: "); line = input.nextLine(); line= line.toLowerCase(); for (int i =0 ; i < line.length(); i ++) { if (line.charAt(i) == 'a') ctra++; else if (line.charAt(i) == 'e') ctre++; else if (line.charAt(i) == 'i') ctri++; else if (line.charAt(i) == 'o') ctro++; else if (line.charAt(i) == 'u') ctru++; else ctrrest++; } allChars = ctra + ctre + ctri + ctro + ctru + ctrrest; System.out.println("Numbers of characters:"); System.out.printf("a %2d ; e %2d ; i %2d ; o %2d ; u %2d ; rest %2d\n", ctra, ctre, ctri, ctro, ctru, ctrrest); System.out.println("Percentages of total:"); System.out.printf("a %2d%%; e %2d%%; i %2d%%; o %2d%%; u %2d%%; rest %2d%%\n",(ctra*100/allChars), (ctre*100/allChars), (ctri*100/allChars), (ctro*100/allChars), (ctru*100/allChars), (ctrrest*100/allChars)); } } 4- Read a positive integer value, and compute the following sequence: If the number is even, halve it; if it's odd, multiply by 3 and add 1. Repeat this process until the value is 1, printing out each value. Finally print out how many of these operations you performed (using both while and for loop). Typical output might be: Initial value is 9 Next value is 28 Next value is 14 Next value is 7 Next value is 22 Next value is 11 Next value is 34 Next value is 17 Next value is 52 Next value is 26 Next value is 13 Next value is 40 Next value is 20 Next value is 10 Next value is 5 Next value is 16 Next value is 8 Next value is 4 Next value is 2 Final value 1, number of steps 19 if the input value is less than 1, print a message containing the word Error Solution 1: /*Name: ....... * ID:......... * assignment 4 part 4 (using while loop) import java.util.Scanner; public class q4a { public static void main(String[] args) { Scanner input = new Scanner(System.in); int value; int ctr = 0; System.out.print("Enter a positive integer value: "); value = input.nextInt(); if (value < 1) System.out.println("Error"); else{ System.out.printf("Initial value is %d\n", value); while (value > 1) { if (value %2 == 0) value = value/2; else value = value * 3 + 1; System.out.printf("Next value is %d\n", value); ctr++; } System.out.printf("Final value %d, number of steps %d\n", value, ctr); } } } Solution 2: /*Name: ....... * ID:......... * assignment 4 part 4 (using For loop) */ import java.util.Scanner; public class q4b { public static void main(String[] args) { Scanner input = new Scanner(System.in); int i, value; System.out.print("Enter a positive integer value: "); value = input.nextInt(); if (value < 1) System.out.println("Error"); else{ System.out.printf("Initial value is %d\n", value); for( i = 0 ; value > 1 ; i++) { if (value %2 == 0) value = value/2; else value = value * 3 + 1; System.out.printf("Next value is %d\n", value); } System.out.printf("Final value %d, number of steps %d\n", value , i); } } } 5- Write a class with methods to do the following output: a) 5 5 5 5 4 4 4 3 3 2 5 4 3 2 1 Solution: /*Name: ....... * ID:......... * assignment 4 part 5 (a) public class q5{ public static void main(String[] args) { int z=5; int n=5; for(int i=n ;i>=1 ;i--){ for(int j=n ;j>i ;j--){ System.out.print( " " );} for(int k=1;k<=z;k++){ System.out.print(i); } z--; System.out.println( " " ); } } } b) 1 2 1 2 1 2 1 2 1 * 1 2 1 2 1 2 1 2 3 3 3 * * * 3 3 3 4 4 * * * * * 4 4 5 * * * * * * * 5 4 4 * * * * * 4 4 3 3 3 * * * 3 3 3 2 2 2 2 * 2 2 2 2 1 1 1 1 1 1 1 1 1 Solution: /*Name: ....... * ID:......... * assignment 4 part 5 (b) public class q5b { public static void main(String[] args) { for (int i = 5; i >= 1; i--){ for (int j = 1; j <= i; j++) System.out.print(j+" "); for (int j = i; j < 5; j++) System.out.print("* "); for (int j = i; j < 4; j++) System.out.print("* "); for (int j = i; j >= 1; j--){ if (j == 5) continue; System.out.print(j+" "); } System.out.println(); } for (int i = 2; i <= 5; i++){ for (int j = 1; j <= i; j++) System.out.print(j+" "); for (int j = i; j < 5; j++) System.out.print("* "); for (int j = i; j < 4; j++) System.out.print("* "); for (int j = i; j >= 1; j--){ if (j == 5) continue; System.out.print(j+" "); } System.out.println(); } } } c) Read odd integer x and the output depended on x for example: x=5 x=7 * * *** *** ***** ***** *** ******* * ***** *** * Solution: /*Name: ....... * ID:......... * assignment 4 part 5 (c) import java.util.Scanner; public class q5c { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("please Enter odd integer: "); int x ; x=input.nextInt(); if(x%2 != 0) { for (int i = 1; i <=x; i+=2){ for (int j = 1; j <= (x - i)/2; j++) System.out.print(" "); for (int j = 1; j <= i; j++) System.out.print("*"); System.out.println(); } for (int i = x-2; i >= 1; i-=2){ for (int j = 1; j <= (x - i)/2; j++) System.out.print(" "); for (int j = 1; j <= i; j++) System.out.print("*"); System.out.println(); } } else System.out.printf("Error!! %d is not odd!!", x); } } 6- Write a program that have a menu of 4 options: Please 1234- choose from the menu: Print a number’ digits Calculate the sum between two integers Find all numbers divisible by a number in a given range Exit Option 1 asks for a positive integer number n and prints its digits. Example: Please enter a positive integer: 6545 6545= 6 5 4 5 4 Option 2 asks for two integers start and end then it finds the sum of all the integers between start and end. Example: Please enter the start and the end integers: 2 6 The sum for all numbers between 2 and 6 = 12 Option 3 asks for three integers start, end and a number then it finds those integers between start and end that are divisible by the number and prints them. Example: Please enter the start and the end integers: 1 10 Please enter the number you want to check for: 3 The numbers are divisible by 3 is : 3 6 9 Option 4 Exit the program. Solution: /*Name: ....... * ID:......... * assignment 4 part 6 import java.util.Scanner; public class q6 { public static void main(String[] args) { Scanner input = new Scanner(System.in); int num ,numcopy ,places=0, factor=1; int choice; int sum=0; do{ System.out.println(); System.out.println("Please choose from the menu:\n"); System.out.println("1-Print the number's digits."); System.out.println("2-Calculate the sum between two integers."); System.out.println("3-Find all numbers divisible by a number in a given range"); System.out.println("4-Exit."); choice = input.nextInt(); switch(choice){ case 1: System.out.println("Please enter a positive integer: "); num = input.nextInt(); numcopy= num; while(numcopy >0){ numcopy/=10; places++; } places--; while(places >0){ factor*=10; places--; } System.out.printf("%d=", num); while(num >0){ numcopy =num % factor ; num/= factor; System.out.printf(" %d", num); factor/=10; num=numcopy; } System.out.println(); break; case 2: int start, end; System.out.println("Please enter the start and the end integers:"); start = input.nextInt(); end = input.nextInt(); for (int i = start+1; i < end; i++) sum += i; System.out.printf("The sum for all numbers between %d and % d = %d\n",start, end, sum); break; case 3: int x,y,z; System.out.println("Please enter the start,end x = input.nextInt(); y= input.nextInt(); z= input.nextInt(); System.out.printf("The numbers divisible by %d for (int i = x ; i < y; i++) { if (i % z ==0) System.out.printf("%d ", i); } break; and a number:"); are : /n", z); case 4: System.out.println("Bye!!"); break; default:System.out.println("Invalid input. Try again!!");} } while (choice!= 4); } } 7- Write a class that reads 10 integers. The program should calculate and print the sum of even and odd numbers. You program should have three methods: - Read method that will be responsible for reading the numbers. - IsOdd a Boolean function that checks if a number is odd or not. - IsEven a Boolean function that checks if a number is even or not. Example: Please enter 10 integers: 1 2 3 4 5 6 7 8 9 10 The sum of odd elements is: 25 The sum of even elements is: 30 Solution: /*Name: ....... * ID:......... * assignment 4 part 7 import java.util.Scanner; public class q7{ public static void main(String[] args) { Scanner input = new Scanner(System.in); int sumEven =0, sumOdd = 0; System.out.println("Please enter 10 integers: "); for (int i = 1; i <= 10; i++){ int num = input.nextInt(); if(num%2==0) sumEven+=num; else sumOdd+=num; } System.out.printf("The sum of odd elements is: %d\n",sumOdd); System.out.printf("The sum of even elements is: %d\n",sumEven); } } 8- Write a class that reads 8 integers and finds the largest and the smallest numbers. The main should print the large and small numbers. Example: Please enter 8 integers: 5 -6 3 4 3 11 0 8 The largest number is: 11 The smallest number is: -6 Solution: /*Name: ....... * ID:......... * assignment 4 part 8 import java.util.Scanner; public class q8 { public static void main(String[] args) { Scanner input = new Scanner(System.in); int large,small; System.out.println("Please enter 8 integers:"); int num = input.nextInt(); large = small = num; for (int i = 2; i <=8; i++){ num = input.nextInt(); if(num >large) large = num; else if (num < small) small = num; } System.out.printf("The largest number is: %d\n", large); System.out.printf("The smallest number is: %d\n", small); } }