CSC108 Midterm Solution -- Tuesday section ========================================== Solution to question 1 ---------------------- i = 4 + 2 * 3 : 10 i = 5 % 2 + 1 : 2 i = 3 * ( 1 / 2 ) : 0 i = 5.0 / 2.0 : Error: The right side of the assignment statement is a double but the left side is an int. You cannot perform a widening type conversion without an explicit cast. f = 2 * 3 : 6.0 i = (int) 7.8 : 7 s = "Good" + "Luck" : "GoodLuck" i = s.indexOf('d') : 3 b = 2 + 3 > 10 : false b = true || false : true Solution to question 2(a) ------------------------- class Part { String name ; // you could declare these variable to be private long number ; // it's not a good idea to make them public double price; Part ( String name, long number, double price ) { this.name = name ; this.number = number ; this.price = price ; } public void print ( ) { System.out.println ( "\nThe part name is: " + name + "\nThe part number is: " + number + "\nThe part price is: " + price ) ; } } Solution to question 2(b) ------------------------- Part new_part = new Part ( "Widget Wheel", 94523, 9.99 ) ; new_part.print () ; Solution to question 3 ---------------------- boolean is_palindrome ( String word ) { for ( int i = 0; i < word.length() / 2; i++ ) // Note that if you use a <= above, the program does not work // for empty strings. if ( word.charAt(i) != word.charAt(word.length()-1-i) ) return false ; return true ; }