150 likes | 174 Views
Problem. Write a program that reads from the keyboard a list of words until the user types “end” and writes the longest and the greatest (alphabetically): Example: Word(or end)? gabriela Word(or end)? jose Word(or end)? rosa Word(or end)? matias Word(or end)? end Longest = gabriela
E N D
Problem. Write a program that reads from the keyboard a list of words until the user types “end” and writes the longest and the greatest (alphabetically): Example: Word(or end)? gabriela Word(or end)? jose Word(or end)? rosa Word(or end)? matias Word(or end)? end Longest = gabriela Greatest = rosa
Algorithm longest = “”, greatest=””; //words with zero letters while(true) { get a word if( word == “end” ) break; if( word’s size > size of longest so fat ) longest=word; if( word > greatest ) greatest = word; } write longest and greatest
Program String longest="", greatest=""; Scanner U = new Scanner(System.in); while(true){ System.out.print("word (or end) "); String word=U.nextLine(); if(word.equals("end")) break; if(word.length() > longest.length()) longest=word; if( word.compareTo(greatest) > 0) greatest=word; } System.out.println("longest=" + longest); System.out.println("greatest=" + greatest);
Explanations • String greatest="", longest=""; • Empty (but valid) strings • String: a pre-defined class (not primitive) • Strings are objects (not variables) • Operations with methods : object.method(arguments) • 2. U.readLine("…") • Scanner’s method for Reading a line (including spaces like “la casa”) • 3. word.equals("end") • Returns true if the string in the argument is equal to the string “word” • 4. word.length() • Returns the number of characters in the string
word.compareTo(greatest) • Compares Strings word and greatest, it returns • 0 if word=greatest, • int number <0 if word<greatest, • int number >0 if word > greatest • Lexicographic comparison • Extension of alphabetical (“dictionary”) to include all characters • order: space<digits<uppercase<lowercase
Char type • for individual characters (letter, digit, special signs) • 16 bits (2 bytes) UNICODE codification • variables: ex: char c; • constants: ‘x’ , ’’, ’a’, ‘A’, ’8’, ’=’ • assignment : ex: c=’a’; • comparison: • ‘ ‘ < ’0’ < ’1’ < ... < ’9’ < • ’A’ < ... < ’Z’ < ’a’ < ... < ’z’
Prob : count how many times a character appears in a string Ex: count(‘a’,“abracadabra”) returns 5 Solution static public intcount(char x,String y) { int n=0; inti=0; while(i<y.length(){ if(y.charAt(i) == x) n=n+1; i=i+1; } return n; }
Problem. Force the user to type yes or no Example: delete the file ? (yes or no): mmm… delete the file ? (yes or no): I dont know delete the file ? (yes or no): Yes File deleted ! Program using a function that gets a yes or no answer if(yesOrNo(“delete the file”).equals(“yes”)) System.out.println(“File deleted!”); else System.out.println(“file kept”);
Solution • static public String yesOrNo(String x) { • String r; // answer • Scanner U = new Scanner(System.in); • while (true) { • System.out.print(x + " (yes or no): "); • r = U.nextLine();// read answer • r = r.trim(); // delete spaces • r = r.toLowerCase();// convert to lowercase • if (r.equals("yes") || r.equals("no")) • break; • } • return r; • }
problem //repeat string x, y times //Ex:repeat(“ho”,3)=“hohoho”, repeat(“ho”,0)=”” static public String repeat(String x,int y){ String s = ""; for(inti=1; i<=y; ++i) s = s.concat(x); //alternatively s = s+x return s;
//draw a square static public void main(String[]x){ … } Dialog: Length of the side ?4 * * * * * * * * * * * * Note. For obtaining a good square add a space to each *
import java.util.*; • class Square{ • static public void main(String[]x) { • Scanner U = new Scanner(System.in); • System.out.print("Square Side ?"); • int n = U.nextInt(); • // draw first line • System.out.println(repeat("* ", n)); • // draw n-2 lines • for (int i = 1; i <= n - 2; ++i) • System.out.println("* " + repeat(" ", n - 2) + "*"); • // draw last line • System.out.println(repeat("* ", n)); } • }
Number to String Conversion String s = “”+n; //if i n=123, s=“123” String s = “”+x; //if x=4.5, s=“4.5” String to Integer Conversion int n=Integer.parseInt(s);//s=”123”,n=123 class Integer{//predefined class … static public int parseInteger(String x){…} } String to real Conversion double x=Double.parseDouble(s);//s=”4.5”,x=4.5 class Double{//predefined class … static public double parseDouble(String x){…}