EXAM 2.docx - 1 StringDemo Lecture 2 Code Combination public class StringDemo cfw public static void main(String args cfw String sentence =\"Text

EXAM 2.docx - 1 StringDemo Lecture 2 Code Combination...

This preview shows page 1 - 6 out of 35 pages.

Lecture 2 Code Combination: 1. StringDemo public class StringDemo { public static void main(String[] args) { String sentence = "Text processing is hard!"; int position = sentence.indexOf("hard"); System.out.println(sentence); System.out.println("012345678901234567890123"); System.out.println("The word \"hard\" starts at index " + position); sentence = sentence.substring(0, position) + "easy!"; sentence = sentence.toUpperCase(); System.out.println("The changed string is:"); System.out.println(sentence); } } 2. ScannerDemo import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { Scanner scannerObject = new Scanner(System.in); System.out.println("Enter two whole numbers"); System.out.println("separated by one or more spaces:"); int n1, n2; n1 = scannerObject.nextInt( ); n2 = scannerObject.nextInt( ); System.out.println("You entered " + n1 + " and " + n2); System.out.println("Next enter two numbers."); System.out.println("A decimal point is OK."); double d1, d2; d1 = scannerObject.nextDouble( ); d2 = scannerObject.nextDouble( );
Image of page 1
System.out.println("You entered " + d1 + " and " + d2); System.out.println("Next enter two words:"); String s1, s2; s1 = scannerObject.next( ); s2 = scannerObject.next( ); System.out.println("You entered \"" + s1 + "\" and \"" + s2 + "\""); s1 = scannerObject.nextLine( ); //To get rid of '\n' System.out.println("Next enter a line of text:"); s1 = scannerObject.nextLine( ); System.out.println("You entered: \"" + s1 + "\""); } } Lecture 3 Code Combination: 1. BankBalance import java.util.Scanner; public class BankBalance { public static final double OVERDRAWN_PENALTY = 8.00; public static final double INTEREST_RATE = 0.02;//2% annually public static void main(String[] args) { double balance;
Image of page 2
System.out.print("Enter your checking account balance: $"); Scanner keyboard = new Scanner(System.in); balance = keyboard.nextDouble( ); System.out.println("Original balance $" + balance); if (balance >= 0) balance = balance + (INTEREST_RATE * balance)/12; else balance = balance - OVERDRAWN_PENALTY; System.out.println("After adjusting for one month"); System.out.println("of interest and penalties,"); System.out.println("your new balance is $" + balance); } } 2. Grader import java.util.Scanner; public class Grader { public static void main(String[] args) { int score; char grade; System.out.println("Enter your score: "); Scanner keyboard = new Scanner(System.in); score = keyboard.nextInt( ); if (score >= 90) grade = 'A'; else if (score >= 80) grade = 'B'; else if (score >= 70) grade = 'C'; else if (score >= 60) grade = 'D'; else grade = 'F';
Image of page 3
System.out.println("Score = " + score); System.out.println("Grade = " + grade); } } 3. MultipleBirth import java.util.Scanner; public class MultipleBirths { public static void main(String[] args) { int numberOfBabies; System.out.print("Enter number of babies: "); Scanner keyboard = new Scanner(System.in); numberOfBabies = keyboard.nextInt(); switch (numberOfBabies) { case 1: System.out.println("Congratulations."); break; case 2: System.out.println("Wow. Twins."); break; case 3: System.out.println("Wow. Triplets."); break; case 4: case 5: System.out.print("Unbelievable; "); System.out.println(numberOfBabies + " babies."); break; default: System.out.println("I don't believe you."); break; } } } 4. StringEqualityDemo import java.util.Scanner;
Image of page 4
public class StringEqualityDemo { public static void main(String[] args) { String s1; String s2; System.out.println("Enter two lines of text:"); Scanner keyboard = new Scanner(System.in);
Image of page 5
Image of page 6

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture