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
CPSC 233 - Introduction to Computer Science for Computer Science Majors II Monir Zaman (Md Moniruzzaman) Tip: Error Messages • Bug: A mistake in a program – The process of eliminating bugs is called debugging • Syntax error: A grammatical mistake in a program – The compiler can detect these errors, and will output an error message saying what it thinks the error is, and where it thinks the error is 1-2 Copyright © 2012 Pearson Addison-Wesley. All rights reserved. • Compute 1+2+...+5 and output the result: int i=0,sum=0; while(++i<5){ sum=sum+i; //System.out.println(i); } System.out.println(sum); • Compute 1+2+...+5 and output the result: int i=0,sum=0; while(i++<5){ sum=sum+i; //System.out.println(i); } System.out.println(sum); Tip: Error Messages • Run-time error: An error that is not detected until a program is run – The compiler cannot detect these errors: an error message is not generated after compilation, but after execution • Logic error: A mistake in the underlying algorithm for a program – The compiler cannot detect these errors, and no error message is generated after compilation or execution, but the program does not do what it is supposed to do 1-5 Copyright © 2012 Pearson Addison-Wesley. All rights reserved. • Print all the elements of an array int[] ar={23,45,67}; int i=0; while(i<4){ System.out.println(ar[i]); i++; } • Print all the elements of an array int[] ar={23,45,67}; int i=0; while(i<ar.length){ System.out.println(ar[i]); i++; } int num=45; if(num>20){ if(num<10){ num*=10; } } Class String • Store text String name=“John”; String name=null; String name; String String name=“John”; name.equals(“smith”) Returns a boolean value: true/false String name=“John”; If( name.equals(“smith”) ) { System.out.printf(“Names %s and %s are equal”, name, “smith”); } String String name=“John”; name.equalsIgnoreCase(“joHn”) Returns a boolean value: true Method definition boolean equals (String) boolean equalsIgnoreCase (String) String toUpperCase() String toLowerCase() String toUpperCase() String toLowerCase() Example: String a_b=“stat101”; System.out.println(a_b.toUpperCase()); STAT101 Some Methods in the Class String (Part 3 of 8) 1-16 Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Some Methods in the Class String (Part 4 of 8) 1-17 Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Some Methods in the Class String (Part 5 of 8) 1-18 Copyright © 2012 Pearson Addison-Wesley. All rights reserved. File Input import java.util.Scanner; import java.io.FileInputStream; import java.io.FileNotFoundException; public class tryfileIO{ public static void main(String[] args){ //this is comment try{ FileInputStream fis=new FileInputStream("/home/grads/mmoniruz/temp.txt"); Scanner fin=new Scanner(fis); System.out.printf("%s%n",fin.nextLine()); }//end of try catch(FileNotFoundException e){ System.out.println(“Error message "+e); System.exit(0); }//end of catch }//end of main }//end of class File Input import java.util.Scanner; import java.io.FileInputStream; import java.io.FileNotFoundException; public class tryfileIO{ public static void main(String[] args){ try{ FileInputStream fis=new FileInputStream("/home/grads/mmoniruz/temp.txt"); Scanner fin=new Scanner(fis); while(fin.hasNextLine()){ System.out.printf("%s%n",fin.nextLine()); } }//end of try catch(FileNotFoundException e){ System.out.println(“Error message "+e); System.exit(0); }//end of catch }//end of main }//end of class Some Methods in the Class String (Part 6 of 8) 1-21 Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Some Methods in the Class String (Part 7 of 8) 1-22 Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Some Methods in the Class String (Part 7 of 8) String name=“JOHN”; name.compareTo(“john”) returns a negative number 1-23 Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Some Methods in the Class String (Part 8 of 8) 1-24 Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Exercise • Write a program that starts with a line of text and then outputs that line with the first occurrence of “hate” changed to “love”. A sample output of the program: The line of text to be changed is: I hate strong wind. I have rephrased that line to read: I love strong wind. Use a defined constant to store the line of text. You can have the word “hate” more than once in the line but change only its first occurrence. Exercise II • Create a text file containing the text “I hate strong wind.” • Write a program that reads the text from the file and changes the first occurrence of “hate” to “love”. A sample output of the program: The line of text to be changed is: I hate strong wind. I have rephrased that line to read: I love strong wind. You can have the word “hate” more than once in the line but change only its first occurrence.