Assignment2013 Please use this document only for verifying if your programs are right. Do not blindly copy paste and waste your time.

Size: px
Start display at page:

Download "Assignment2013 Please use this document only for verifying if your programs are right. Do not blindly copy paste and waste your time."

Transcription

1 Please use this document only for verifying if your programs are right. Do not blindly copy paste and waste your time. 11/3/2013 TechSparx Computer Training Center Saravanan.G

2 Please use this document only for verifying if your programs are right. Do not blindly copy paste and waste your time. TechSparx Computer Training Center Page 1

3 /** An employee is entitled to pay an income tax based on hsi gross anual income as follows: WAP to compute and print the tax payable by a salaried person import java.io.*; public class Program01 BufferedReader br = new BufferedReader(new new InputStreamReader(System.in)); public void main() throws IOException System.out.println("Enter the annual gross " + " income of the employee : "); int gross = Integer.parseInt(br.readLine()); double tax = 0; if( gross <= ) tax = 0; else if ( gross >= && gross <= ) tax = ( (10.0/100.0) * (gross ) ); else if ( gross >= && gross <= ) tax = ( (20.0/100.0) * (gross ) ); else if (gross >= ) tax = ( (30.0/100.0) * (gross ) ); System.out.println("Tax payable by the employee is : " + tax); /* OUTPUT: Enter the annual gross income of the employee : Tax payable by the employee is : /* TechSparx Computer Training Center Page 2

4 NOTE: I have changed the question Program that translates a word into Pig-Latin form. import java.io.*; public class Program02 public static void main() throws IOException BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); System.out.println("Enter a word : "); String str = br.readline(); String firstword = ""; String secondword = ""; //Loop to generate pig-latin for(int i = 0; i < str.length(); i++) char ch = str.charat(i); if ( ch == 'A' ch == 'a' ch == 'E' ch == 'e' ch == 'I' ch == 'i' ch == 'O' ch == 'o' ch == 'U' ch == 'u' ) //collect all the character from this character secondword = str.substring(i); break; else firstword = firstword + ch; String outstr = secondword + firstword + "AY"; System.out.println("Given word : " + str); System.out.println("Pig-Latin form is : " + outstr); TechSparx Computer Training Center Page 3

5 /* OUTPUT: Enter a word : KING Given word : KING Pig-Latin form is : INGKAY TechSparx Computer Training Center Page 4

6 /* Write a menu driven program to display the following patterns according to the user's choice Choice : 1 A ABC ABCDE ABCDEFG Choice : 2 ABCDEDCBA ABCD DCBA ABC CBA AB BA A A import java.io.*; public class Program03 BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); public void main() throws IOException System.out.println("*****MENU*****"); System.out.println("1. First Pattern"); System.out.println("2. Second Pattern"); System.out.println("Enter your choice : "); int choice = Integer.parseInt(br.readLine()); switch(choice) case 1 : case 2 : printfirstpattern(); break; printsecondpattern(); break; default: System.out.println("Invalid choice"); break; public void printfirstpattern() int n = 4; int k = 1; TechSparx Computer Training Center Page 5

7 int spaces = n-1; for(int i = 1; i <= n; i++) //Loop to print the spaces for(int j = 1; j <= spaces; j++) System.out.print(" "); char ch = 'A'; //Loop to print the alphabet for(int j = 1; j <= k; j++) System.out.print(ch); ch++; k = k + 2; spaces--; System.out.println(); void printsecondpattern() int spaces = -1; int n = 5; //Loop to print the pattern for(int i = n; i >= 1; i--) char ch = 'A'; //Loop to print the left half of the current row for(int j = 1; j <= i; j++) System.out.print(ch); ch++; //Loop to print the spaces in the middle of the current row for(int j = 1; j <= spaces; j++) System.out.print(" "); ch--; TechSparx Computer Training Center Page 6

8 if (i == n) ch--; //Loop to print the right half of the current row for(int j = 1; j <= i-1; j++) System.out.print(ch); ch--; else //Loop to print the right half of the current row for(int j = 1; j <= i; j++) System.out.print(ch); ch--; spaces += 2; System.out.println(); /* *****MENU***** 1. First Pattern 2. Second Pattern Enter your choice : 1 A ABC ABCDE ABCDEFG *****MENU***** 1. First Pattern 2. Second Pattern Enter your choice : 2 ABCDEDCBA ABCD DCBA ABC CBA AB BA A A TechSparx Computer Training Center Page 7

9 /** Design a class with 3 overloaded functions according to the following specifications i. to find and print the largest out of two integers ii. to find and print the alrgest out of the three integers iii. to find and print the largest out of the three real numbers public class Program04 public void findlargest(int a, int b) if ( a > b ) System.out.println(a + " is greater " + b); else if ( b > a ) System.out.println( b + " is greater " + a); else System.out.println( a + " is equal to " + b); public void findlargest(int a, int b, int c) if ( a > b && a > c ) System.out.println(a + " is greater than " + b + " and " + c); else if ( b > a && b > c ) System.out.println( b + " is greater than " + a + " and " + c); else if ( c > a && c > b ) System.out.println( c + " is greater than " + a + " and " + b); public void findlargest(double a, double b) if ( a > b ) System.out.println(a + " is greater than " + b); TechSparx Computer Training Center Page 8

10 else if ( b > a ) System.out.println( b + " is greater than " + a); else System.out.println( a + " is equal to " + b); public void main() Program04 obj = new Program04(); obj.findlargest(10, 20); obj.findlargest(100, 200, 300); obj.findlargest(101.5, ); /* 20 is greater is greater than 100 and is greater than TechSparx Computer Training Center Page 9

11 /*Program 5 import java.io.*; public class Amicable int a, b; BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); Amicable() a = 0; b = 0; Amicable(int x, int y) a = x; b = y; boolean checkamicable(int x, int y) int sumx = 0; int sumy = 0; //Sum of the factors of x for(int i = 1; i < x; i++) if ( x % i == 0 ) sumx = sumx + i; //Sum of the factors of y for(int i = 1; i < y; i++) if ( y % i == 0 ) sumy = sumy + i; if ( x == sumy && y == sumx ) return true; else TechSparx Computer Training Center Page 10

12 return false; public void main() throws IOException System.out.println("Enter two numbers : "); int m = Integer.parseInt(br.readLine()); int n = Integer.parseInt(br.readLine()); Amicable obj1 = new Amicable(); if (obj1.checkamicable(m, n)) System.out.println(m + " and " + n + " are amicable"); else System.out.println(m + " and " + n + " are NOT amicable"); System.out.println("Enter two numbers : "); int x = Integer.parseInt(br.readLine()); int y = Integer.parseInt(br.readLine()); Amicable obj2 = new Amicable(x, y); if (obj2.checkamicable(x, y)) System.out.println(x + " and " + y + " are amicable"); else System.out.println(x + " and " + y + " are NOT amicable"); /* Enter two numbers : and 200 are NOT amicable TechSparx Computer Training Center Page 11

13 Enter two numbers : and 284 are amicable TechSparx Computer Training Center Page 12

14 /** Design a class to accept name, class roll number and marks in phy, chem, bio and comp of students until zero is entered as the roll number. Calculate and print the following : i. names of students who score less than 40 in any of the given subject ii. All over average of the student iii. Name and roll number of the student with the highest average import java.io.*; public class Program06 BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); public void main() throws IOException int size = 100; String names[] = new String[size]; int rollnumber[] = new int[size]; double physics[] = new double[size]; double chemistry[] = new double[size]; double biology[] = new double[size]; double comp[] = new double[size]; double average[] = new double[size]; int numofstudents = 0; for(int i = 0; i < size; i++) System.out.println(" "); System.out.println("Enter the details of student " + (i+1) ); System.out.print("Roll number : "); int temp = Integer.parseInt(br.readLine()); if ( temp == 0 ) numofstudents = i; break; rollnumber[i] = temp; System.out.print("Full Name : "); names[i] = br.readline(); TechSparx Computer Training Center Page 13

15 System.out.print("Marks in Physics : "); physics[i] = Double.parseDouble(br.readLine()); System.out.print("Marks in Chemistry : "); chemistry[i] = Double.parseDouble(br.readLine()); System.out.print("Marks in Biology : "); biology[i] = Double.parseDouble(br.readLine()); System.out.print("Marks in Computers : "); comp[i] = Double.parseDouble(br.readLine()); if ( numofstudents == 0) System.out.println("Thank you for using our software!!!"); System.exit(0); //this causes the flow of control to exit the //program //Display names of students who have scored less than 40 System.out.println("Do you want to see the list of students who " + " has scored less than 40 in any of the following subjects : "); System.out.println("1. Physics"); System.out.println("2. Chemistry"); System.out.println("3. Biology"); System.out.println("4. Computers"); System.out.println("Enter your choice (1, 2, 3 or 4) : " ); int choice = Integer.parseInt(br.readLine()); int flag = 0; switch (choice) case 1 : System.out.println("List of students who have scored " + " less than 40 in Physics are : "); for(int i = 0; i < numofstudents; i++) if( physics[i] < 40 ) flag = 1; System.out.println(names[i]); break; TechSparx Computer Training Center Page 14

16 case 2 : System.out.println("List of students who have scored " + " less than 40 in Chemistry are : "); for(int i = 0; i < numofstudents; i++) if( chemistry[i] < 40 ) flag = 1; System.out.println(names[i]); break; case 3 : System.out.println("List of students who have scored " + " less than 40 in Biology are : "); for(int i = 0; i < numofstudents; i++) if( biology[i] < 40 ) flag = 1; System.out.println(names[i]); break; case 4 : System.out.println("List of students who have scored " + " less than 40 in Computer are : "); for(int i = 0; i < numofstudents; i++) if( comp[i] < 40 ) flag = 1; System.out.println(names[i]); break; default : System.out.println("Invalid choice"); if ( flag == 0 ) System.out.println("NO one has scored less than 40"); TechSparx Computer Training Center Page 15

17 System.out.println(" "); //Calculate and display all over average of each student System.out.println("Names \t\t All over average"); for(int i = 0; i < numofstudents; i++) average[i] = (physics[i] + chemistry[i] + biology[i] + comp[i])/4.0; System.out.println(names[i] + "\t\t " + average[i]); System.out.println(" "); double highestaverage = average[0]; int indexofhighest = 0; //Loop to find the highest average for(int i = 1; i < numofstudents; i++) if ( average[i] > highestaverage ) highestaverage = average[i]; indexofhighest = i; System.out.println("Student who has the highest average is : " ); System.out.println("Name : " + names[indexofhighest]); System.out.println("Roll Number : " + rollnumber[indexofhighest]); System.out.println("Average : " + average[indexofhighest]); /* Enter the details of student 1 Roll number : 10 Full Name : Ram Marks in Physics : 99 Marks in Chemistry : 98 Marks in Biology : 97 Marks in Computers : Enter the details of student 2 Roll number : 11 Full Name : Sita Marks in Physics : 95 Marks in Chemistry : 94 TechSparx Computer Training Center Page 16

18 Marks in Biology : 90 Marks in Computers : Enter the details of student 3 Roll number : 12 Full Name : Krishna Marks in Physics : 100 Marks in Chemistry : 100 Marks in Biology : 90 Marks in Computers : Enter the details of student 4 Roll number : 13 Full Name : Jhon Marks in Physics : 80 Marks in Chemistry : 90 Marks in Biology : 30 Marks in Computers : Enter the details of student 5 Roll number : 14 Full Name : Akhil Marks in Physics : 30 Marks in Chemistry : 40 Marks in Biology : 50 Marks in Computers : Enter the details of student 6 Roll number : 0 Do you want to see the list of students who has scored less than 40 in any of the following subjects : 1. Physics 2. Chemistry 3. Biology 4. Computers Enter your choice (1, 2, 3 or 4) : 4 List of students who have scored less than 40 in Computer are : NO one has scored less than Names All over average Ram 98.5 Sita 91.0 Krishna 92.5 Jhon 75.0 Akhil Student who has the highest average is : TechSparx Computer Training Center Page 17

19 Name : Ram Roll Number : 10 Average : 98.5 TechSparx Computer Training Center Page 18

20 /** * Design a class to accept the values of a, b, c and n. * Calculate and print x and y, where : * x = (abc)^2 / 1! + (abc)^4 / 2! (abc)^(n*2) / n! * y = 2 + (2*4) + (2*4*6) +... (2*4*...n) import java.io.*; public class Program07 BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); public void main() throws IOException System.out.println("Enter the value of a, b, c and n : "); int a = Integer.parseInt(br.readLine()); int b = Integer.parseInt(br.readLine()); int c = Integer.parseInt(br.readLine()); int n = Integer.parseInt(br.readLine()); double x = 0; //Calculate the sum of the first series for(int i = 1; i <= n; i++) double numerator = Math.pow((a * b * c), i*2); //Loop to find the factorial of i int factorial = 1; for(int j = 1; j <= i; j++) factorial = factorial * j; double term = numerator / factorial; x = x + term; int term = 1; int y = 0; //Calculate the sum of the second series for(int i = 2; i <= n; i=i+2) term = term * i; y = y + term; TechSparx Computer Training Center Page 19

21 System.out.println("x = " + x); System.out.println("y = " + y); /* Enter the value of a, b, c and n : x = E53 y = 4282 TechSparx Computer Training Center Page 20

22 /* Design a class which accepts an integer array and its size. * Exchanges the values of the first half side with the second * half side elements of the array. * Example: if an array of eight elements intially contains the elements * as 2, 4, 1, 6, 7, 9, 23, 10 * Then the function should rearrange the array as * 7, 9, 23, 10, 2, 4, 1 6 import java.io.*; public class Program08 BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); public void main() throws IOException System.out.println("Enter the number of elements : "); int n = Integer.parseInt(br.readLine()); //Declaration int a[] = new int[n]; //Read an array System.out.println("Enter " + n + " integers : "); for(int i = 0; i < a.length; i++) a[i] = Integer.parseInt(br.readLine()); int mid = a.length/2; //Shift array elements for(int i = 0, j = mid+1; i <= mid && j < n; i++, j++) //swap int temp = a[i]; a[i] = a[j]; a[j] = temp; //Display after shifting System.out.println("Array elements after shifting"); for(int i = 0; i < a.length; i++) System.out.print(a[i] + " "); TechSparx Computer Training Center Page 21

23 /* Program to find the Sum of principle diagonal elements and sum of the other diagonal elements. import java.io.*; public class Program09 public static void main(string[] args) throws IOException BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //Input number of rows and columns from the user System.out.println("Enter the number of rows and columns : "); int rows = Integer.parseInt(br.readLine()); int cols = Integer.parseInt(br.readLine()); int A[][] = new int[rows][cols]; //Read the elements of the MATRIX System.out.println("Enter the elements of the MATRIX"); for (int i = 0; i < rows; i++) for(int j = 0; j < cols; j++) A[i][j] = Integer.parseInt(br.readLine()); //Display the elements of the MATRIX System.out.println("The MATRIX is as follows : "); for (int i = 0; i < rows; i++) for(int j = 0; j < cols; j++) System.out.print(A[i][j] + "\t"); System.out.println(); //Sum of each row of the matrix for(int i = 0; i < rows; i++) int sum = 0; for(int j = 0; j < cols; j++) sum = sum + A[i][j]; TechSparx Computer Training Center Page 22

24 System.out.println("The sum of row " + i + " is : " + sum); //Sum of each column of the matrix for(int i = 0; i < rows; i++) int sum = 0; for(int j = 0; j < cols; j++) sum = sum + A[j][i]; System.out.println("The sum of column " + i + " is : " + sum); //Sum of principle diagonal elements int sumprincipaldiagonal = 0; for(int i = 0; i < rows; i++) sumprincipaldiagonal += A[i][i]; System.out.println("The sum of the Principal diagonal " + " elements is : " + sumprincipaldiagonal); //Sum of the other diagonal elements int sumotherdiagonal = 0; for(int i = 0, j = cols - 1; (i < rows && j >= 0 ); i++, j--) sumotherdiagonal += A[i][j]; System.out.println("The sum of the Other diagonal elements is : " + sumotherdiagonal); /* Enter the number of rows and columns : 3 3 Enter the elements of the MATRIX TechSparx Computer Training Center Page 23

25 The MATRIX is as follows : The sum of row 0 is : 60 The sum of row 1 is : 150 The sum of row 2 is : 240 The sum of column 0 is : 120 The sum of column 1 is : 150 The sum of column 2 is : 180 The sum of the Principal diagonal elements is : 150 The sum of the Other diagonal elements is : 150 TechSparx Computer Training Center Page 24

26 /* Write a program to input an array of 10 elements. During input care should be taken that 0 or negative numbers are not allowed. Read the user s choice and perform selection sort or bubble sort on it for ascending order. import java.io.*; public class Program10 BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); public void main() throws IOException int n = 10; int a[] = new int[n]; System.out.println("Enter " + n + " elements "); for(int i = 0; i < n; i++) int temp = 0; do temp = Integer.parseInt(br.readLine()); if (temp > 0) a[i] = temp; break; else System.out.println("Array element should be greater than 0"); System.out.println("Please re-enter : "); while(temp <= 0); //Display the array System.out.println("The given array elements are : " ); for(int i = 0; i < n; i++) System.out.print(a[i] + " "); TechSparx Computer Training Center Page 25

27 System.out.println("\nWhich sorting technique do you want to perform? "); System.out.println("1. Bubble Sort"); System.out.println("2. Selection Sort"); System.out.println("Enter your choice : "); int choice = Integer.parseInt(br.readLine()); switch(choice) case 1 : bubblesort(a); break; case 2 : selectionsort(a); break; default: System.out.println("Invalid choice"); //Display the array System.out.println("The array elements after sorting are : " ); for(int i = 0; i < n; i++) System.out.print(a[i] + " "); void bubblesort(int a[]) int n = a.length; //Bubble sort for(int i = 0; i < n - 1; i++) for(int j = 0; j < n - i - 1; j++) if( a[j] > a[j+1] ) //swap int temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; void selectionsort(int a[]) int n = a.length; TechSparx Computer Training Center Page 26

28 //selection sort for(int i = 0; i < n; i++) int smallest = a[i]; int indexofsmallest = i; for(int j = i+1; j < n; j++) if( a[j] < smallest ) smallest = a[j]; indexofsmallest = j; //swap int temp = a[i]; a[i] = a[indexofsmallest]; a[indexofsmallest] = temp; /* Enter 10 elements Array element should be greater than 0 Please re-enter : Array element should be greater than 0 Please re-enter : The given array elements are : Which sorting technique do you want to perform? 1. Bubble Sort 2. Selection Sort Enter your choice : TechSparx Computer Training Center Page 27

29 1 The array elements after sorting are : TechSparx Computer Training Center Page 28

30 /** Design a class to enter a sentence in lower case. Encrypt the string and print. For example : Input: "where words fail. music speaks." Output : "yj$t$@y$tfu@h$$n.@o$u$e@ur$$mu." import java.io.*; public class Program11 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public void main() throws IOException System.out.println("Enter a string in lower case : "); String str = br.readline(); str = str.tolowercase(); String outstr = ""; for(int i = 0; i < str.length(); i++) char ch = str.charat(i); //encrypt if (ch == ' ') ch = '@'; if(ch == 'a' ch == 'e' ch == 'i' ch == 'o' ch == 'u') ch = '$'; else if(character.islowercase(ch)) ch = (char)(ch + 2); outstr = outstr + ch; System.out.println("The given string is : " + str); System.out.println("The output string is : " + outstr); TechSparx Computer Training Center Page 29

31 /* Enter a string in lower case : where words fail. music speaks. The given string is : where words fail. music speaks. The output string is : yj$t$@y$tfu@h$$n.@o$u$e@ur$$mu. TechSparx Computer Training Center Page 30

32 /** Write a program to accept a line of text from the user and create a new word formed out of the first letter of each word. After creating the new word reverse it and check whether it is a Palindrome or not import java.io.*; public class Program12 BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); public void main() throws IOException System.out.println("Enter a string : " ); String str = br.readline(); str = str + " "; String outstr = ""; String curword = ""; //Loop to extract first character of every word for(int i = 0; i < str.length(); i++) char ch = str.charat(i); if(ch == ' ') outstr = outstr + curword.charat(0); curword = ""; else curword += ch; //Loop to reverse the outstr String rev = ""; for(int i = 0; i < outstr.length(); i++) char ch = outstr.charat(i); rev = ch + rev; if( outstr.equals(rev) ) TechSparx Computer Training Center Page 31

33 System.out.println(outstr + " is a palindrome"); else System.out.println(outstr + " is NOT a palindrome"); /* Run 1: Enter a string : Mangoes Are Deliverd After Midnight MADAM is a palindrome Run 2: Enter a string : A Brick In The Wall ABITW is NOT a palindrome TechSparx Computer Training Center Page 32

34 /* Write a menu driven program to generate the following patterns import java.io.*; public class Program13 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public void main() throws IOException System.out.println("*******MENU*******"); System.out.println("1. First Pattern"); System.out.println("2. Second Pattern"); System.out.println("Enter your choice : "); int choice = Integer.parseInt(br.readLine()); switch(choice) case 1 : System.out.println("The pattern is : "); printfirstpattern(); break; case 2 : System.out.println("The pattern is : "); printsecondpattern(); break; TechSparx Computer Training Center Page 33

35 default: System.out.println("Invalid Choice"); public void printfirstpattern() int end = 9; //Loop to print the upper half for(int i = 1; i <= 5; i++) for(int j = i; j <= end; j++) System.out.print(j); end--; System.out.println(); end = end + 2; //Loop to print the lower half for(int i = 4; i >= 1; i--) for(int j = i; j <= end; j++) System.out.print(j); end++; System.out.println(); public void printsecondpattern() int spaces = 0; //Loop to print the upper half of the pattern for(int i = 5; i >= 1; i--) //Loop to print the left half of the current row for(int j = 1; j <= i; j++) System.out.print(j); //Loop to print spaces TechSparx Computer Training Center Page 34

36 for(int j = 1; j <= spaces; j++) System.out.print(" "); //Loop to print the right half of the current row for(int j = i; j >= 1; j--) System.out.print(j); spaces = spaces + 2; System.out.println(); spaces = spaces - 4; //Loop to print the lower half of the pattern for(int i = 2; i <= 5; i++) //Loop to print the left half of the current row for(int j = 1; j <= i; j++) System.out.print(j); //Loop to print spaces for(int j = 1; j <= spaces; j++) System.out.print(" "); //Loop to print the right half of the current row for(int j = i; j >= 1; j--) System.out.print(j); spaces = spaces - 2; System.out.println(); /* *******MENU******* 1. First Pattern 2. Second Pattern TechSparx Computer Training Center Page 35

37 Enter your choice : 1 The pattern is : *******MENU******* 1. First Pattern 2. Second Pattern Enter your choice : 2 The pattern is : TechSparx Computer Training Center Page 36

38 /** Input an array of 10 integers. a) Input a number n. search for n in the array, and delete it from the array if it is present. Perform all necessary shifting of elements. You are not allowed to use a new array for the result. b) Input a number n and position pos. Insert the number in the specified position and display the array. import java.io.*; public class Program14 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public void main() throws IOException int n = 10; int a[] = new int[n]; System.out.println("Enter " + n + " elements : "); for(int i = 0; i < n; i++) a[i] = Integer.parseInt(br.readLine()); System.out.println("Enter the element to be deleted from the array : "); int key = Integer.parseInt(br.readLine()); //Loop to delete key from array a for(int i = 0; i < n; i++) if ( key == a[i] ) //delete the element by shifting for(int j = i+1; j < n; j++) a[j-1] = a[j]; : "); //Loop to display System.out.println("The array elements after deleting " + key + " are for(int i = 0; i < n-1; i++) System.out.print(a[i] + " "); TechSparx Computer Training Center Page 37

39 System.out.println("Enter the element to be inserted : "); int keyinsert = Integer.parseInt(br.readLine()); System.out.println("Enter the position where " + keyinsert + " should be inserted"); int pos = Integer.parseInt(br.readLine()); for(int i = n-2; i >= pos-1; i--) a[i+1] = a[i]; a[pos-1] = keyinsert; //Loop to display System.out.println("The array elements after inserting " + keyinsert + " at position " + pos + " are : "); for(int i = 0; i < n; i++) System.out.print(a[i] + " "); TechSparx Computer Training Center Page 38

40 /** class : Array Data Members : int arr[] int size char choice Member functions: void input() --- to accept the array elements int search() --- Read an element. Search and display the position of the elemtn. Choice 'b' : Binary Search Choice 'l' : Linear Search import java.io.*; public class Array int arr[]; int size; int choice; BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); void input() throws IOException System.out.println("Enter the size of the array : "); size = Integer.parseInt(br.readLine()); arr = new int[size]; System.out.println("Enter " + size + " elements : "); for(int i = 0; i < size; i++) arr[i] = Integer.parseInt(br.readLine()); void search() throws IOException System.out.println("Enter the element to be searched : "); int key = Integer.parseInt(br.readLine()); System.out.println("Type \'b\' for using Binary Search"); System.out.println("Type \'l\' for using Linear Search"); choice = br.readline().charat(0); TechSparx Computer Training Center Page 39

41 switch(choice) case 'b' : int flag = 0; int low = 0; int high = size-1; int pos = 0; while(low <= high) int mid = (low + high)/2; if(key == arr[mid]) flag = 1; pos = mid + 1; break; else if( key < arr[mid] ) high = mid - 1; else if ( key > arr[mid] ) low = mid + 1; if(flag == 1) System.out.println(key + " is found at position " + pos); else System.out.println(key + " is found NOT found"); break; case 'l' : int flag = 0; int pos = 0; for(int i = 0; i < size; i++) if(key == arr[i]) TechSparx Computer Training Center Page 40

42 flag = 1; pos = i+1; break; if(flag == 1) System.out.println(key + " is found at position " + pos); else System.out.println(key + " is found NOT found"); break; default: System.out.println("Invalid choice"); public void main() throws IOException Array obj = new Array(); obj.input(); obj.search(); /* Run 1: Enter the size of the array : 10 Enter 10 elements : Enter the element to be searched : 45 TechSparx Computer Training Center Page 41

43 Type 'b' for using Binary Search Type 'l' for using Linear Search b 45 is found at position 5 Run 2: Enter the size of the array : 10 Enter 10 elements : Enter the element to be searched : 70 Type 'b' for using Binary Search Type 'l' for using Linear Search l 70 is found at position 8 TechSparx Computer Training Center Page 42

/ Download the Khateeb Classes App

/ Download the Khateeb Classes App Q) Write a program to read and display an array on screen. class Data public static void main(string args[ ]) throws IOException int i,n; InputStreamReader r = new InputStreamReader(System.in); BufferedReader

More information

ICSE Solved Paper, 2018

ICSE Solved Paper, 2018 ICSE Solved Paper, 2018 Class-X Computer Applications (Maximum Marks : 100) (Time allowed : Two Hours) Answers to this Paper must be written on the paper provided separately. You will not be allowed to

More information

Problems with simple variables

Problems with simple variables Problems with simple variables Hard to give up values high number of variables. Complex to sort a high number of variables by value. Impossible to use loops. Example problem: Read one hundred numbers,

More information

Computer Applications Answer Key Class IX December 2017

Computer Applications Answer Key Class IX December 2017 Computer Applications Answer Key Class IX December 2017 Question 1. a) What are the default values of the primitive data type int and float? Ans : int = 0 and float 0.0f; b) Name any two OOP s principle.

More information

LECTURE 17. Array Searching and Sorting

LECTURE 17. Array Searching and Sorting LECTURE 17 Array Searching and Sorting ARRAY SEARCHING AND SORTING Today we ll be covering some of the more common ways for searching through an array to find an item, as well as some common ways to sort

More information

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 2.A. Design a superclass called Staff with details as StaffId, Name, Phone, Salary. Extend this class by writing three subclasses namely Teaching (domain, publications), Technical (skills), and Contract

More information

Good Earth School Naduveerapattu Date: Marks: 70

Good Earth School Naduveerapattu Date: Marks: 70 Good Earth School Naduveerapattu Date:.2.207 Marks: 70 Class: XI Second Term Examination Computer Science Answer Key Time: 3 hrs. Candidates are allowed additional 5 minutes for only reading the paper.

More information

Recursion. General Algorithm for Recursion. When to use and not use Recursion. Recursion Removal. Examples

Recursion. General Algorithm for Recursion. When to use and not use Recursion. Recursion Removal. Examples Recursion General Algorithm for Recursion When to use and not use Recursion Recursion Removal Examples Comparison of the Iterative and Recursive Solutions Exercises Unit 19 1 General Algorithm for Recursion

More information

It is a constructor and is called using the new statement, for example, MyStuff m = new MyStuff();

It is a constructor and is called using the new statement, for example, MyStuff m = new MyStuff(); COSC 117 Exam 3 Key Fall 2012 Part 1: Definitions & Short Answer (3 Points Each) 1. A method in a class that has no return type and the same name as the class is called what? How is this type of method

More information

FIFO PAGE REPLACEMENT : import java.io.*; public class FIFO {

FIFO PAGE REPLACEMENT : import java.io.*; public class FIFO { FIFO PAGE REPLACEMENT : import java.io.*; public class FIFO public static void main(string[] args) throws IOException BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int frames,

More information

1 Short Answer (10 Points Each)

1 Short Answer (10 Points Each) 1 Short Answer (10 Points Each) 1. For the following one-dimensional array, show the final array state after each pass of the three sorting algorithms. That is, after each iteration of the outside loop

More information

Guru Gobind Singh Public School Sector V,B Bokaro Steel City Annual IP Assignment Class 11

Guru Gobind Singh Public School Sector V,B Bokaro Steel City Annual IP Assignment Class 11 Guru Gobind Singh Public School Sector V,B Bokaro Steel City Annual IP Assignment Class 11 1. What will be the output of given expression : int a=7; System.out.println(++a + + a-- + + a+1 + +a++); System.out.println(a);

More information

1. What is the difference between a compiler and an interpreter? Also, discuss Java s method.

1. What is the difference between a compiler and an interpreter? Also, discuss Java s method. Name: Write all of your responses on these exam pages. 1 Short Answer (5 Points Each) 1. What is the difference between a compiler and an interpreter? Also, discuss Java s method. 2. Java is a platform-independent

More information

4. Finding & Displaying Record of Salesman with minimum net income. 5. Finding & Displaying Record of Salesman with maximum net income.

4. Finding & Displaying Record of Salesman with minimum net income. 5. Finding & Displaying Record of Salesman with maximum net income. Solution of problem#55 of Lab Assignment Problem Statement: Design & Implement a java program that can handle salesmen records of ABC Company. Each salesman has unique 4 digit id #, name, salary, monthly

More information

Chapter 7: Iterations

Chapter 7: Iterations Chapter 7: Iterations Objectives Students should Be able to use Java iterative constructs, including do-while, while, and for, as well as the nested version of those constructs correctly. Be able to design

More information

ICSE Class 10 Computer Applications ( Java ) 2012 Solved Question...

ICSE Class 10 Computer Applications ( Java ) 2012 Solved Question... 1 of 1 05-11-015 16: ICSE J Java for Class X Computer Applications ICSE Class 10 Computer Applications ( Java ) 01 Solved Question Paper COMPUTER APPLICATIONS (Theory) (Two Hours) Answers to this Paper

More information

A sample print out is: is is -11 key entered was: w

A sample print out is: is is -11 key entered was: w Lab 9 Lesson 9-2: Exercise 1, 2 and 3: Note: when you run this you may need to maximize the window. The modified buttonhandler is: private static class ButtonListener implements ActionListener public void

More information

Java Programs. System.out.println("Dollar to rupee converion,enter dollar:");

Java Programs. System.out.println(Dollar to rupee converion,enter dollar:); Java Programs 1.)Write a program to convert rupees to dollar. 60 rupees=1 dollar. class Demo public static void main(string[] args) System.out.println("Dollar to rupee converion,enter dollar:"); int rs

More information

ESC101 : Fundamental of Computing

ESC101 : Fundamental of Computing ESC101 : Fundamental of Computing End Semester Exam 19 November 2008 Name : Roll No. : Section : Note : Read the instructions carefully 1. You will lose 3 marks if you forget to write your name, roll number,

More information

AN IMPROTANT COLLECTION OF JAVA IO RELATED PROGRAMS

AN IMPROTANT COLLECTION OF JAVA IO RELATED PROGRAMS JAVALEARNINGS.COM AN IMPROTANT COLLECTION OF JAVA IO RELATED PROGRAMS Visit for more pdf downloads and interview related questions JAVALEARNINGS.COM /* Write a program to write n number of student records

More information

Introducing arrays. Week 4: arrays and strings. Creating arrays. Declaring arrays. Initialising arrays. Declaring and creating in one step

Introducing arrays. Week 4: arrays and strings. Creating arrays. Declaring arrays. Initialising arrays. Declaring and creating in one step School of Computer Science, University of Birmingham Java Lecture notes. M. D. Ryan. September 2001. Introducing arrays Week 4: arrays and strings Arrays: a data structure used to store multiple data,

More information

Lecture 6 Sorting and Searching

Lecture 6 Sorting and Searching Lecture 6 Sorting and Searching Sorting takes an unordered collection and makes it an ordered one. 1 2 3 4 5 6 77 42 35 12 101 5 1 2 3 4 5 6 5 12 35 42 77 101 There are many algorithms for sorting a list

More information

class Ideone { public static void main (String[] args) throws java.lang.exception {

class Ideone { public static void main (String[] args) throws java.lang.exception { 1. wap to enter a string then convert it into upper case Input:- san deep output : SaN DEEp import java.util.*; import java.lang.*; class Ideone public static void main (String[] args) throws java.lang.exception

More information

1 Short Answer (10 Points Each)

1 Short Answer (10 Points Each) COSC 117 Exam # Solutions Fall 01 1 Short Answer (10 Points Each) 1. Write a declaration for a two dimensional array of doubles that has 1 rows and 17 columns. Then write a nested for loop that populates

More information

Subject: Computer Science

Subject: Computer Science Subject: Computer Science Topic: Data Types, Variables & Operators 1 Write a program to print HELLO WORLD on screen. 2 Write a program to display output using a single cout statement. 3 Write a program

More information

Computer programming Code exercises [1D Array]

Computer programming Code exercises [1D Array] Computer programming-140111014 Code exercises [1D Array] Ex#1: //Program to read five numbers, from user. public class ArrayInput public static void main(string[] args) Scanner input = new Scanner(System.in);

More information

Suggestive List of C++ Programs

Suggestive List of C++ Programs Suggestive List of C++ Programs 1. Write a C++ program to display Hello World! on the output screen. 2. Write a program to display Multiplication Table of a number inputted by the user. 3. Write a program

More information

Esc101 Mid Semester Exam - II

Esc101 Mid Semester Exam - II Esc101 Mid Semester Exam - II Time Allowed: 1 Hour Max Marks: 75 Instructions: 1. Do not turn this page until the bell rings. 2. YOU MUST WRITE YOUR NAME, ROLL NUMBER & SECTION ON EACH SHEET. 3. Please

More information

Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013

Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013 Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013 One Dimensional Q1: Write a program that declares two arrays of integers and fills them from the user. Then exchanges their values and display the

More information

Exercise 4: Loops, Arrays and Files

Exercise 4: Loops, Arrays and Files Exercise 4: Loops, Arrays and Files worth 24% of the final mark November 4, 2004 Instructions Submit your programs in a floppy disk. Deliver the disk to Michele Zito at the 12noon lecture on Tuesday November

More information

I. True/False: (2 points each)

I. True/False: (2 points each) CS 102 - Introduction to Programming Midterm Exam #2 - Prof. Reed Spring 2008 What is your name?: (2 points) There are three sections: I. True/False..............54 points; (27 questions, 2 points each)

More information

Recursion. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

Recursion. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG Recursion EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG Recursion: Principle Recursion is useful in expressing solutions to problems that can be recursively defined: Base Cases:

More information

Recursion: Factorial (1) Recursion. Recursion: Principle. Recursion: Factorial (2) Recall the formal definition of calculating the n factorial:

Recursion: Factorial (1) Recursion. Recursion: Principle. Recursion: Factorial (2) Recall the formal definition of calculating the n factorial: Recursion EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG Recursion: Factorial (1) Recall the formal definition of calculating the n factorial: 1 if n = 0 n! = n (n 1) (n 2) 3 2

More information

COMPUTER APPLICATIONS

COMPUTER APPLICATIONS COMPUTER APPLICATIONS (Theory) (Two hours) Answers to this Paper must be written on the paper provided separately. You will not be allowed to write during the first 15 minutes. This time is to be spent

More information

Practical No: 1 Aim: Demonstrate round - robin scheduling using threads.

Practical No: 1 Aim: Demonstrate round - robin scheduling using threads. Practical No: 1 Aim: Demonstrate round - robin scheduling using threads. Source Code: RR.java import java.io.*; class job implements Runnable int process_id, no_of_instr,time_quantum; Thread t; job(int

More information

Write a program which converts all lowercase letter in a sentence to uppercase.

Write a program which converts all lowercase letter in a sentence to uppercase. Write a program which converts all lowercase letter in a sentence to uppercase. For Example: 1) Consider a sentence "welcome to Java Programming!" its uppercase conversion is " WELCOME TO JAVA PROGRAMMING!"

More information

COE 212 Engineering Programming. Welcome to the Final Exam Tuesday December 15, 2015

COE 212 Engineering Programming. Welcome to the Final Exam Tuesday December 15, 2015 1 COE 212 Engineering Programming Welcome to the Final Exam Tuesday December 15, 2015 Instructors: Dr. Salim Haddad Dr. Bachir Habib Dr. Joe Tekli Dr. Wissam F. Fawaz Name: Student ID: Instructions: 1.

More information

Byte and Character Streams. Reading and Writing Console input and output

Byte and Character Streams. Reading and Writing Console input and output Byte and Character Streams Reading and Writing Console input and output 1 I/O basics The io package supports Java s basic I/O (input/output) Java does provide strong, flexible support for I/O as it relates

More information

Arrays in Java Using Arrays

Arrays in Java Using Arrays Recall Length of an Array Arrays in Java Using Arrays Once an array has been created in memory, its size is fixed for the duration of its existence. Every array object has a length field whose value can

More information

INDIAN SCHOOL MUSCAT COMPUTER SCIENCE(083) CLASS XI

INDIAN SCHOOL MUSCAT COMPUTER SCIENCE(083) CLASS XI INDIAN SCHOOL MUSCAT COMPUTER SCIENCE(083) CLASS XI 2017-2018 Worksheet No. 1 Topic : Getting Started With C++ 1. Write a program to generate the following output: Year Profit% 2011 18 2012 27 2013 32

More information

Oct Decision Structures cont d

Oct Decision Structures cont d Oct. 29 - Decision Structures cont d Programming Style and the if Statement Even though an if statement usually spans more than one line, it is really one statement. For instance, the following if statements

More information

Program 20: //Design an Applet program to handle Mouse Events. import java.awt.*; import java.applet.*; import java.awt.event.*;

Program 20: //Design an Applet program to handle Mouse Events. import java.awt.*; import java.applet.*; import java.awt.event.*; Program 20: //Design an Applet program to handle Mouse Events. import java.awt.*; import java.applet.*; import java.awt.event.*; /* */ public

More information

COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz

COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz The in-class quiz is intended to give you a taste of the midterm, give you some early feedback about

More information

MYcsvtu Notes. Experiment-1. AIM: Write a program to implement fiestal cipher structure PROGRAM: import java.io.*; class functions

MYcsvtu Notes. Experiment-1. AIM: Write a program to implement fiestal cipher structure PROGRAM: import java.io.*; class functions Experiment-1 AIM: Write a program to implement fiestal cipher structure PROGRAM: import java.io.*; class functions String Xor(String s1,string s2) char s1array[]=s1.tochararray(); char s2array[]=s2.tochararray();

More information

Programming Questions and Solutions in Java (15CS561)

Programming Questions and Solutions in Java (15CS561) This document can be downloaded from www.chetanahegde.in with most recent updates. 1 Programming Questions and Solutions in Java (15CS561) 1. Write a program to find area of circle. class Circle int r=3;

More information

Question 1a) Trace of program

Question 1a) Trace of program Question 1a) What is printed by the following Java program? int s; int r; int i; int [] x = 4, 8, 2, -9, 6; s = 1; r = 0; i = x.length - 1; while (i > 0) s = s * -1; i = i - 1; r = r + s * x[i]; System.out.println(r);

More information

CS141 Programming Assignment #5

CS141 Programming Assignment #5 CS141 Programming Assignment #5 Due Wednesday, Nov 16th. 1) Write a class that asks the user for the day number (0 to 6) and prints the day name (Saturday to Friday) using switch statement. Solution 1:

More information

VARIABLE, OPERATOR AND EXPRESSION [SET 1]

VARIABLE, OPERATOR AND EXPRESSION [SET 1] VARIABLE, OPERATOR AND EXPRESSION Question 1 Write a program to print HELLO WORLD on screen. Write a program to display the following output using a single cout statement. Subject Marks Mathematics 90

More information

Please note that if you write the mid term in pencil, you will not be allowed to submit a remark request.

Please note that if you write the mid term in pencil, you will not be allowed to submit a remark request. University of Toronto CSC148 Introduction to Computer Science Fall 2001 Mid Term Test Section L5101 Duration: 50 minutes Aids allowed: none Make sure that your examination booklet has 8 pages (including

More information

1 Short Answer (10 Points Each)

1 Short Answer (10 Points Each) Name: Write all of your responses on these exam pages. 1 Short Answer (10 Points Each) 1. What is the difference between a compiler and an interpreter? Also, discuss how Java accomplishes this task. 2.

More information

Top-down programming design

Top-down programming design 1 Top-down programming design Top-down design is a programming style, the mainstay of traditional procedural languages, in which design begins by specifying complex pieces and then dividing them into successively

More information

ICSE 2015 COMPUTER APPLICATIONS. Md. Zeeshan Akhtar

ICSE 2015 COMPUTER APPLICATIONS. Md. Zeeshan Akhtar ICSE 2015 COMPUTER APPLICATIONS Md. Zeeshan Akhtar COMPUTER APPLICATIONS Question 1 (a) (b) What are the default values of primitive data type int and float? Name any two OOP s principles. [2] [2] (c)

More information

Lab Assignment Three

Lab Assignment Three Lab Assignment Three C212/A592 Fall Semester 2010 Due in OnCourse by Friday, September 17, 11:55pm (Dropbox will stay open until Saturday, September 18, 11:55pm) Abstract Read and solve the problems below.

More information

Recitation: Loop Jul 7, 2008

Recitation: Loop Jul 7, 2008 Nested Loop Recitation: Loop Jul 7, 2008 1. What is the output of the following program? Use pen and paper only. The output is: ****** ***** **** *** ** * 2. Test this program in your computer 3. Use "for

More information

COE 212 Engineering Programming. Welcome to the Final Exam Monday May 18, 2015

COE 212 Engineering Programming. Welcome to the Final Exam Monday May 18, 2015 1 COE 212 Engineering Programming Welcome to the Final Exam Monday May 18, 2015 Instructors: Dr. Joe Tekli Dr. George Sakr Dr. Wissam F. Fawaz Name: Student ID: Instructions: 1. This exam is Closed Book.

More information

e) Implicit and Explicit Type Conversion Pg 328 j) Types of errors Pg 371

e) Implicit and Explicit Type Conversion Pg 328 j) Types of errors Pg 371 Class IX HY 2013 Revision Guidelines Page 1 Section A (Power Point) Q1.What is PowerPoint? How are PowerPoint files named? Q2. Describe the 4 different ways of creating a presentation? (2 lines each) Q3.

More information

Search,Sort,Recursion

Search,Sort,Recursion Search,Sort,Recursion Searching, Sorting and Recursion Searching Linear Search Inserting into an Array Deleting from an Array Selection Sort Bubble Sort Binary Search Recursive Binary Search Searching

More information

ICSE Class 10 Computer Applications ( Java ) 2014 Solved Question Paper

ICSE Class 10 Computer Applications ( Java ) 2014 Solved Question Paper 1 of 10 05-11-015 16:1 ICSE J Java for Class X Computer Applications ICSE Class 10 Computer Applications ( Java ) 014 Solved Question Paper ICSE Question Paper 014 (Solved) Computer Applications Class

More information

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #04

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #04 Department of Computer Science & Engineering Indian Institute of Technology Kharagpur Topic: Arrays and Strings Practice Sheet #04 Date: 24-01-2017 Instructions: For the questions consisting code segments,

More information

Structured programming

Structured programming Exercises 9 Version 1.0, 13 December, 2016 Table of Contents 1. Remainders from lectures.................................................... 1 1.1. What is a pointer?.......................................................

More information

Programming: Java. Chapter Objectives. Chapter Objectives (continued) Program Design Including Data Structures. Chapter 7: User-Defined Methods

Programming: Java. Chapter Objectives. Chapter Objectives (continued) Program Design Including Data Structures. Chapter 7: User-Defined Methods Chapter 7: User-Defined Methods Java Programming: Program Design Including Data Structures Chapter Objectives Understand how methods are used in Java programming Learn about standard (predefined) methods

More information

COMPUTER APPLICATIONS

COMPUTER APPLICATIONS COMPUTER APPLICATIONS (Theory) (Two Hours) Answers to this Paper must be written on the paper provided separately. You will not be allowed to write during the first 15 minutes. This time is to be spent

More information

B.Sc (Computer Science) Object Oriented Programming with Java and Data Structures Lab Programs- Data Structures

B.Sc (Computer Science) Object Oriented Programming with Java and Data Structures Lab Programs- Data Structures B.Sc (Computer Science) Object Oriented Programming with Java and Data Structures Lab Programs- Data Structures 1 1. Program to implement Bubble Sort. class Prog37 public static void main(string[] args)

More information

CS212 Midterm. 1. Read the following code fragments and answer the questions.

CS212 Midterm. 1. Read the following code fragments and answer the questions. CS1 Midterm 1. Read the following code fragments and answer the questions. (a) public void displayabsx(int x) { if (x > 0) { System.out.println(x); return; else { System.out.println(-x); return; System.out.println("Done");

More information

1 Short Answer (7 Points Each)

1 Short Answer (7 Points Each) 1 Short Answer ( Points Each) 1. Write a linear search method for an integer array that takes in an array and target value as parameters and returns the first position of the target in the array. If the

More information

1 Short Answer (10 Points Each)

1 Short Answer (10 Points Each) 1 Short Answer (10 Points Each) 1. Write a for loop that will calculate a factorial. Assume that the value n has been input by the user and have the loop create n! and store it in the variable fact. Recall

More information

CPSC 319. Week 2 Java Basics. Xiaoyang Liu & Sorting Algorithms

CPSC 319. Week 2 Java Basics. Xiaoyang Liu & Sorting Algorithms CPSC 319 Week 2 Java Basics Xiaoyang Liu xiaoyali@ucalgary.ca & Sorting Algorithms Java Basics Variable Declarations Type Size Range boolean 1 bit true, false char 16 bits Unicode characters byte 8 bits

More information

Chapter 10: Recursive Problem Solving

Chapter 10: Recursive Problem Solving 2400 COMPUTER PROGRAMMING FOR INTERNATIONAL ENGINEERS Chapter 0: Recursive Problem Solving Objectives Students should Be able to explain the concept of recursive definition Be able to use recursion in

More information

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class. Name: Covers Chapters 1-3 50 mins CSCI 1301 Introduction to Programming Armstrong Atlantic State University Instructor: Dr. Y. Daniel Liang I pledge by honor that I will not discuss this exam with anyone

More information

Recursive Problem Solving

Recursive Problem Solving Recursive Problem Solving Objectives Students should: Be able to explain the concept of recursive definition. Be able to use recursion in Java to solve problems. 2 Recursive Problem Solving How to solve

More information

Functions. Arash Rafiey. September 26, 2017

Functions. Arash Rafiey. September 26, 2017 September 26, 2017 are the basic building blocks of a C program. are the basic building blocks of a C program. A function can be defined as a set of instructions to perform a specific task. are the basic

More information

(c) ((!(a && b)) == (!a!b)) TRUE / FALSE. (f) ((!(a b)) == (!a &&!b)) TRUE / FALSE. (g) (!(!a) && (c-d > 0) && (b!b))

(c) ((!(a && b)) == (!a!b)) TRUE / FALSE. (f) ((!(a b)) == (!a &&!b)) TRUE / FALSE. (g) (!(!a) && (c-d > 0) && (b!b)) ComS 207: Programming I Midterm 2, Tue. Mar 21, 2006 Student Name: Student ID Number: Recitation Section: 1. True/False Questions (10 x 1p each = 10p) Determine the value of each boolean expression given

More information

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal APCS A Midterm Review You will have a copy of the one page Java Quick Reference sheet. This is the same reference that will be available to you when you take the AP Computer Science exam. 1. n bits can

More information

Tasks for fmri-setting (Tasks of first and second pilot study at the end)

Tasks for fmri-setting (Tasks of first and second pilot study at the end) Tasks for fmri-setting (Tasks of first and second pilot study at the end) 1. Faculty int result = 1; int x = 4; while (x > 1) { result = result * x; x--; 7. Find max in list of numbers public static void

More information

Sorting and searching arrays

Sorting and searching arrays Chapter 9 Sorting and searching arrays Lecture slides for: Java Actually: A Comprehensive Primer in Programming Khalid Azim Mughal, Torill Hamre, Rolf W. Rasmussen Cengage Learning, 2008. ISBN: 978-1-844480-933-2

More information

WOSO Source Code (Java)

WOSO Source Code (Java) WOSO 2017 - Source Code (Java) Q 1 - Which of the following is false about String? A. String is immutable. B. String can be created using new operator. C. String is a primary data type. D. None of the

More information

Arrays. CS Feb 2008

Arrays. CS Feb 2008 Arrays CS 180 15 Feb 2008 Announcements Exam 1 Grades on Blackboard Project 2 scores: end of Class Project 4, due date:20 th Feb Snakes & Ladders Game Review of Q5,Q8,Q11 & Programming Questions Arrays:

More information

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1 Name SOLUTION Page Points Score 2 15 3 8 4 18 5 10 6 7 7 7 8 14 9 11 10 10 Total 100 1 P age 1. Program Traces (41 points, 50 minutes)

More information

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2013

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2013 CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2013 Name: This exam consists of 7 problems on the following 6 pages. You may use your single- side hand- written 8 ½ x 11 note sheet during the

More information

Recursion. Tracing Method Calls via a Stack. Beyond this lecture...

Recursion. Tracing Method Calls via a Stack. Beyond this lecture... Recursion EECS2030 B: Advanced Object Oriented Programming Fall 2018 CHEN-WEI WANG Recursion: Principle Recursion is useful in expressing solutions to problems that can be recursively defined: Base Cases:

More information

Arrays and Strings. Arrays - homogeneous indexed collections Selection sort as array example. String class, revisited

Arrays and Strings. Arrays - homogeneous indexed collections Selection sort as array example. String class, revisited Arrays and Strings Arrays - homogeneous indexed collections Selection sort as array example Introduction to complexity analysis String class, revisited 1 Arrays Homogeneous collections of elements of same

More information

University of Palestine. Mid Exam Total Grade: 100

University of Palestine. Mid Exam Total Grade: 100 First Question No. of Branches (5) A) Choose the correct answer: 1. If we type: system.out.println( a ); in the main() method, what will be the result? int a=12; //in the global space... void f() { int

More information

Chapter Objectives. List Processing. Chapter 10: Applications of Arrays and Strings. Java Programming: Program Design Including Data Structures

Chapter Objectives. List Processing. Chapter 10: Applications of Arrays and Strings. Java Programming: Program Design Including Data Structures Chapter 10: Applications of Arrays and Strings Java Programming: Program Design Including Data Structures Chapter Objectives Learn how to implement the sequential search algorithm Explore how to sort an

More information

KENDRIYA VIDYALYA CLRI CHENNAI AUTUMN BREAK HOLIDAY HW MARKS QUESTIONS : DATA STRUCTURE

KENDRIYA VIDYALYA CLRI CHENNAI AUTUMN BREAK HOLIDAY HW MARKS QUESTIONS : DATA STRUCTURE KENDRIYA VIDYALYA CLRI CHENNAI AUTUMN BREAK HOLIDAY HW 8 MARKS QUESTIONS : DATA STRUCTURE. Write a function in C++ which accepts an integer array and its size as arguments and change all the even number

More information

SECTION A (40 Marks) Attempt all questions. (a) What is inheritance? [2] (c) State the number of bytes occupied by char and int data types.

SECTION A (40 Marks) Attempt all questions. (a) What is inheritance? [2] (c) State the number of bytes occupied by char and int data types. QUALITATIVE ANALYSIS SECTION A (40 Marks) Attempt all questions Question 1 (a) What is inheritance? (b) Name the operators listed below: (i) < (ii) ++ (iii) && (iv)? : (c) State the number of bytes occupied

More information

What is an algorithm?

What is an algorithm? /0/ What is an algorithm? Searching and Sorting (Savitch, Chapter 7.) TOPICS Algorithms Complexity Binary Search Bubble Sort Insertion Sort Selection Sort A finite set of precise instrucons for performing

More information

In this chapter, you will:

In this chapter, you will: Java Programming: Guided Learning with Early Objects Chapter 4 Control Structures I: Selection In this chapter, you will: Make decisions with the if and if else structures Use compound statements in an

More information

Sorting and Searching

Sorting and Searching CHAPTER 13 Sorting and Searching The exercises in this chapter are a framework for comparing algorithms empirically. This approach provides students with a means for understanding the finer details of

More information

COMPUTER APPLICATIONS

COMPUTER APPLICATIONS COMPUTER APPLICATIONS (Theory) (Two hours) Answers to this Paper must be written on the paper provided separately. You will not be allowed to write during the first 15 minutes. This time is to be spent

More information

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 10 INTRODUCTION TO ARRAYS

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 10 INTRODUCTION TO ARRAYS UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 10 INTRODUCTION TO ARRAYS EXERCISE 10.1 1. An array can contain many items and still be treated as one thing. Thus, instead of having many variables for

More information

PROGRAMMING IN C AND C++:

PROGRAMMING IN C AND C++: PROGRAMMING IN C AND C++: Week 1 1. Introductions 2. Using Dos commands, make a directory: C:\users\YearOfJoining\Sectionx\USERNAME\CS101 3. Getting started with Visual C++. 4. Write a program to print

More information

DataInputStream dis=new DataInputStream(System.in);

DataInputStream dis=new DataInputStream(System.in); SET 2 Answer Key 1.a. import java.util.*; import java.io.*; class sorting public static void main(string as[])throws Exception DataInputStream dis=new DataInputStream(System.in); System.out.println("Enter

More information

McGill University Faculty of Science School of Computer Science. MIDTERM EXAMINATION - Sample solutions. COMP-250: Introduction to Computer Science

McGill University Faculty of Science School of Computer Science. MIDTERM EXAMINATION - Sample solutions. COMP-250: Introduction to Computer Science STUDENT NAME: STUDENT ID: McGill University Faculty of Science School of Computer Science MIDTERM EXAMINATION - Sample solutions COMP-250: Introduction to Computer Science March 12, 2014 Examiner: Prof.

More information

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail. OOP in Java 1 Outline 1. Getting started, primitive data types and control structures 2. Classes and objects 3. Extending classes 4. Using some standard packages 5. OOP revisited Parts 1 to 3 introduce

More information

Columns A[0] A[0][0] = 20 A[0][1] = 30

Columns A[0] A[0][0] = 20 A[0][1] = 30 UNIT Arrays and Strings Part A (mark questions). What is an array? (or) Define array. An array is a collection of same data type elements All elements are stored in continuous locations Array index always

More information

CS115 Principles of Computer Science

CS115 Principles of Computer Science CS5 Principles of Computer Science Chapter Arrays Prof. Joe X. Zhou Department of Computer Science CS5 Arrays. Re: Objectives in Methods To declare methods, invoke methods, and pass arguments to a method

More information

Java Programming for Selenium

Java Programming for Selenium Video 5 - Java Control Flow, String Handling and Arrays Java Programming for Selenium 1) Conditional / Decision Making 2) Loop 3) Branching 4) String Handling in Java 5) Java Arrays 1) Conditional / Decision

More information

Last Class. While loops Infinite loops Loop counters Iterations

Last Class. While loops Infinite loops Loop counters Iterations Last Class While loops Infinite loops Loop counters Iterations public class January31{ public static void main(string[] args) { while (true) { forloops(); if (checkclassunderstands() ) { break; } teacharrays();

More information

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C Sample Test Paper-I Marks : 25 Time:1 Hrs. Q1. Attempt any THREE 09 Marks a) State four relational operators with meaning. b) State the use of break statement. c) What is constant? Give any two examples.

More information

AP Programming - Chapter 6 Lecture

AP Programming - Chapter 6 Lecture page 1 of 21 The while Statement, Types of Loops, Looping Subtasks, Nested Loops I. The while Statement Note: Loop - a control structure that causes a sequence of statement(s) to be executed repeatedly.

More information