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
Lesson 1 /* Write a Java program that declares an array of 7 integer elements and assign values to the first, third and fifth elements. Next, your Java program prints from the third to the seventh elements. */ package array_elements_lesson1; public class Array_elements_lesson1 { public static void main(String[] args) { int price[] = new int[7] ; int i; price[0] = 10 ; price[2] = 20 ; price[4] = 30 ; System.out.println(price[2]); System.out.println(price[3]); System.out.println(price[4]); } } Lesson 2 /* Write a Java program to create an array called “sales” containing the following values: 20.50, 30.21, 40.85 and 50.25. Then the program prints the elements of this array. */ package print_arrays; public class Print_arrays { public static void main(String[] args) { double sales[] = { 20.50, 30.21, 40.85 , 50.25 }; int i; for (i=0; i<4 ; i++) { System.out.println(sales[i]); } } } /* Create a Java program that saves five elements in an array. The elements are determined based on this equation 2i +4, where (i) is the index of the array. */ package array_data; public class Array_data { public static void main(String[] args) { int x[] = new int [5]; int i; for (i=0; i<5 ; i++) { x[i] = 2*i + 4 ; } for (i=0; i<5 ; i++) { System.out.print(x[i]+"\t"); } } } /* Create a Java program that reads seven decimal numbers and saves them in an array. Next, your Java program finds and then prints the sum of the elements that are greater than 10. */ package assessment1_lesson2; import java.util.Scanner; public class Assessment1_lesson2 { public static void main(String[] args) { double high[] = new double [7]; double sum; int i; Scanner inputdata = new Scanner(System.in); for (i=0; i<7 ; i++) { System.out.print(" Enter number " + i + " : " ); high[i] = inputdata.nextDouble(); } sum = 0 ; for (i=0; i<7 ; i++) { if (high[i]>=10) sum = sum +high[i]; } System.out.println (" Sum = " + sum); for (i=0; i<7 ; i++) { System.out.print (high[i]+"\t"); } } } Lesson 3 /* Create a Java program that reads eight real number entered by the user and saves them in an array. Then search for a value entered by the user in the array and count the number of times it occurs in the array. */ package array_search1; import java.util.Scanner; public class Array_search1 { public static void main(String[] args) { double [] average = new double [8] ; int i , count , number ; Scanner inputdata = new Scanner(System.in); for (i=0; i<8 ; i++) { System.out.print(" Enter number " + i + " : average[i] = inputdata.nextDouble(); " ); } System.out.print( " Enter the number to be searched = " ); number = inputdata.nextInt(); i = 0 ; count = 0; while (i<8) { if (number == average[i] ) count++; i++ ; } if (count >0) { System.out.println ( " Found " + count + " times " ); } else System.out.println ( " Not Found " ); } } /* Create a Java program that reads ten real numbers and saves them in an array. Next, your Java program finds and prints both the maximum element and the index of it, in the array. */ package max_index; import java.util.Scanner; public class Max_index { public static void main(String[] args) { double speed[] = new double [10]; int i , index ; double max; Scanner inputdata = new Scanner(System.in); for (i=0; i<10 ; i++) { System.out.print(" Enter number " + i + " : speed[i] = inputdata.nextDouble(); " ); } max = speed[0]; index = 0 ; for (i=0; i<10; i++){ if (speed[i]>max) { max = speed[i]; index = i ; } } System.out.println(" Maximum Value = " + max + " index = index ); } } /* " + Create a Java program that reads 10 integer numbers entered by the user, save them in an array, and then counts how many are divisible by 5. */ package assessment1_l3; import java.util.Scanner; public class Assessment1_L3 { public static void main(String[] args) { int num [] = new int [10]; int i, count; Scanner inputdata = new Scanner(System.in); for (i=0; i<10 ; i++) { System.out.println(" Enter number num [i] = inputdata.nextInt(); } count = 0 ; for (i=0; i<10 ; i++) { if (num[i] % 5 == 0 ) count++; } System.out.print(" Count = " + count ); } } " + i + " : " ); /* Suppose that eight employees work for your company. Create a Java program that reads the number of worked hours and the rate per hour for each employee. Save this information in 2 arrays. Then your program computes the wages of the employees and saves them in a 3rd array. It also finds the total amount of money you have to pay and the avrages of these employees. Note: wage = worked hours * pay rate. */ package assessment2_l3; import java.util.Scanner; public class Assessment2_L3 { public static void main(String[] args) { double hours [] = new double [8]; int rates [] = new int [8]; double wages [] = new double [8]; int i; double sum , avg; Scanner inputdata = new Scanner(System.in); for (i=0; i<8 ; i++) { System.out.print(" Enter worked hours of employee (i+1) + " : " ); hours [i] = inputdata.nextDouble(); (i+1) + " : System.out.print(" \t Enter the rate of employee " ); rates [i] = inputdata.nextInt(); } for (i=0; i<8 ; i++) { wages[i] = hours[i] * rates [i] ; } sum = 0 ; for (i=0; i<8 ; i++) { sum = sum + wages[i] ; } avg = sum / 8.0 ; System.out.println (" The Total money is " + sum System.out.println (" The Average is " + avg ); } } ); " + " + Lesson 4 /* 1.Create a program that reads 10 numbers from the user and save them in an array 2. Modify your program to work for any numbers given by the user. 3. Extend your program to find the average of these numbers. 4. Extend your program to find the smallest number of these numbers. 5. Extend your program to find the largest number of these numbers. 6. Extend your program to print them in reverse order. 7. Extend your program to count how many elements are positive. 8. Extend your program to count how many elements are negative. */ package array_size; import java.util.Scanner; public class Array_size { public static void main(String[] args) { int arraysize , i , max , min , count_positive , count_negative ; double avg , sum; Scanner inputdata = new Scanner(System.in); System.out.print( "Enter the size of the array: " ); arraysize = inputdata.nextInt(); int[] list = new int[arraysize]; for (i=0; i<arraysize ; i++) { System.out.print(" Enter number list [i] = inputdata.nextInt(); " + i + " : } sum = 0 ; for (i=0; i<arraysize ; i++) { sum = sum +list[i]; } avg = sum / arraysize ; System.out.println (" average = " + avg ); min = list[0]; for (i=0; i<arraysize; i++){ if (list[i]<min) min = list[i]; } System.out.println(" Minmum Value = " + min max = list[0]; ); " ); for (i=0; i<arraysize; i++){ if (list[i]>max) max = list[i]; } System.out.println(" Maximum Value = " + max ); for (i=arraysize; i>0; i--){ System.out.print( list[i-1] + "\t" ); } System.out.println(); count_positive = 0 ; for (i=0; i<arraysize; i++){ if (list[i]>0) count_positive++; } count_negative = 0 ; for (i=0; i<arraysize; i++){ if (list[i]>0) count_negative++; } System.out.println( " Positive number = " + count_positive ); System.out.println( " Negative number = " + count_negative ); } } /* Create a Java program that makes a reverse copy of an array in another array (size of array is variable). */ package array_size_reverse; import java.util.Scanner; public class Array_size_reverse { public static void main(String[] args) { int arraysize , i , index ; Scanner inputdata = new Scanner(System.in); System.out.print( "Enter the size of the array: " ); arraysize = inputdata.nextInt(); int[] list = new int[arraysize]; int[] reverse_list = new int[arraysize]; index = arraysize-1 ; for (i=0; i<arraysize ; i++) { System.out.print(" Enter number list [i] = inputdata.nextInt(); reverse_list[index] = list[i]; index--; " + i + " : " ); } for (i=0; i<arraysize ; i++) { System.out.print(list[i] + "\t" ); } System.out.println(); for (i=0; i<arraysize ; i++) { System.out.print(reverse_list[i] + "\t" ); } } } /* Create a Java program that: a. Prompts the user to enter an integer N. b. Creates an array to hold N integers. c. Fills the array with odd numbers starting from 1 (i.e., 1 3 5 7 9 …). d. Prints the array. e. Rotates the numbers in the array one position to the right. (See example in the student book). */ package assessment2_l4; import java.util.Scanner; public class Assessment2_L4 { public static void main(String[] args) { int arraysize , i , j , temp1, temp2 ; Scanner inputdata = new Scanner(System.in); System.out.print( "Enter the size of the array: " ); arraysize = inputdata.nextInt(); int[] list = new int[arraysize]; j = 1; for (i=0; i<arraysize ; i++) { list [i] = j ; j = j + 2 ; } for (i=0; i<arraysize ; i++) { System.out.print( list[i]+ "\t" ); } temp1 = list [0] ; list [0] = list [arraysize-1] ; for (i=1; i<arraysize ; i++) { temp2 = list[i]; list [i] = temp1; temp1 = temp2; } System.out.println (); for (i=0; i<arraysize ; i++) { System.out.print( list[i]+ "\t" ); } } } Lesson 5 /* Create a Java program that reads 10 integer numbers, save them in an array, and sorts the numbers in descending order. Your program prints the elements both before and after the sort. */ package sort_des; import java.util.Scanner; public class Sort_des { public static void main(String[] args) { int grades[] = new int [10] ; int i,j; int temp; Scanner inputdata = new Scanner(System.in); for (i = 0; i<10 ; i++ ){ System.out.print(" Enter Array Element grades[i] = inputdata.nextInt(); " + i + " = "); } for (i=0; i<10 ; i++) System.out.print (grades[i]+ "\t"); for (i = 0; i<10 ; i++ ){ for (j=i+1; j<10; j++){ if (grades[i]<grades[j]){ temp = grades[i]; grades[i]=grades[j]; grades[j]=temp; } } } System.out.println(); for (i=0; i<10 ; i++) System.out.print (grades[i]+ "\t"); } } /* Create a Java program that reads the letters in your first name one letter at the time, saves them in an array, sorts these letters (ascending or descending) and prints them out. */ package assessment1_l5; import java.util.Scanner; public class Assessment1_L5 { public static void main(String[] args) { int arraysize; int i , j ; char temp; Scanner inputdata = new Scanner(System.in); System.out.print(" Enter No of letters arraysize = inputdata.nextInt(); char grade[] = new char [arraysize]; " ); for (i=0; i<arraysize ; i++) { System.out.print(" Enter Letter " + (i+1) + " : grade[i] = inputdata.next().charAt(0); } for (i = 0; i<arraysize ; i++ ){ for (j=i+1; j<arraysize; j++){ if (grade[i]>grade[j]){ temp = grade[i]; grade[i]=grade[j]; grade[j]=temp; } } } for (i=0; i<arraysize ; i++) System.out.print (grade[i]+ "\t"); } } " );