1 / 15

Algorithm longest = “”, greatest=””; //words with zero letters while(true) { get a word

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

ljohn
Download Presentation

Algorithm longest = “”, greatest=””; //words with zero letters while(true) { get a word

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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);

  4. 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

  5. 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

  6. 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’

  7. 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; }

  8. 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”);

  9. 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; • }

  10. 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;

  11. //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 *

  12. 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)); } • }

  13. 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){…}

More Related