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
CS 180 Recitation 8 / {30, 31} / 2007 Announcements • Project 1 is out – Due 9/5 @ 10:00pm – Start early! – Use the newsgroup for your questions – LWSN B146 Lab help hours 7-10pm Monday through Wednesday Naming Conventions • Methods and variables begin with lower case int age, aMultipleWordVariable, another_multiple_word_variable; public void method() { } public void aMultipleWordMethod() { } • Classes should begin with upper case public class MyClass { } • Names should be descriptive for clarity and understanding • No identifier can start with number • Java is case sensitive aGe, Age, agE, AgE are all different in Java Coding Conventions • Code should be organized in a clean and clear fashion so that it is easy to read • Tabs and whitespace do not change your code – Use them to cleanly space out your code – Don’t leave too much white space around – find a balance (ex: don’t double space lines of code) Bad Code (My eyes! My eyes!) public class veRybAdpRogramMINgSTYLe { String NONdESCrIPTVariaBL_Ename; public String GEtS____tuFF ( ){ return NONdESCrIPTVariaBL_Ename; } } Good Code (Ah… much better!) public class Name { String name; public String getName() { return name; } } Comments • Used to document your code • Help remind you what variables or methods do/represent (a good name helps as well) • Comment classes, class variables, and methods to describe what they are and what they do // a single line comment can use double slashes /* or you can do the multi-line style as well */ /* multi-line comments look like this */ /** * javascript comments look like this * @param paramName our parameter */ String • A Java class (we will discuss classes and objects in more detail later) • A sequence of 0 or more characters including the 26 letters, digits 0-9, punctuation marks, and whitespaces • Declaration – can use constructor String str = new String(“I love my TA!!!”); String str = “I love my TA!!!”; • Use length() method to determine number of characters in the string String str = “I love my TA!!!”; // str.length() returns 15 • Indexing characters starts at 0 – Starting indexing at 0 will be seen again when we study arrays) – Therefore, the characters are at indices 0…str.length()-1 String • Substring – method used to grab a portion of the string – First index – the beginning character – Last index – the character after the last taken one (if none specified, go to end of string) String str = “myString!!!”; String str2 = str.substring(0,str.length()); // “myString!!!” String str3 = str.substring(3,5); // “tr” String str4 = str.substring(3); // “tring!!!” Other Common Methods • int indexOf(char) – Returns the first index of the character argument in the String, -1 if the character does not exist in the String • char charAt(int) – Returns the character at the integer argument index in the String; an exception is thrown if the index is invalid • Concatenation (+) – Combine two Strings together • Many more methods – consult API Dates • Date class – From java.util package – Use toString method to print out the date • toString is implicitly called for you • SimpleDateFormat class – From java.text package – Used to format dates SimpleDateFormat sdf = new SimpleDateFormat(“MM/dd/yy”); User Input • Scanner – A java provided class in the java.util library – Many uses • Input and parsing – Keyboard – Files (later in the semester) – Strings • JOptionPane – A java provided class in the javax.swing library – Provides many methods to generate windows • Messages • Input Two Sample Programs import java.util.Scanner; public class SampleProgram { public static void main(String[] args) { Scanner input = new Scanner (System.in); System.out.print(“Enter a word: “); String word = input.next(); System.out.println(“Your word is “ + word.length() + “ letters long.”); } } import javax.swing.JOptionPane; public class SampleProgram { public static void main(String[] args) { String word = JOptionPane.showInputDialog(null, “Enter a word:”); JOptionPane.showMessageDialog(null, “Your word is “ + word.length() + “ letters long.”); } } Pig Latin! import java.util.Scanner; public class PigLatinTranslator { public static void main(String[] args) { PigLatinTranslator plt = new PigLatinTranslator(); Scanner input = new Scanner (System.in); System.out.print(“Enter a word to translate: “); String word = input.next(); System.out.println(word + “in pig latin is “ + plt.translate() + “.”); } // a very naïve translator public String translate(String s) { return s.substring(1) + s.charAt(0) + “ay”; } }