Outprintlnciao name string name 24 converting strings

This preview shows page 4 - 5 out of 5 pages.

System.out.println("Ciao " + name); String name = 24 Converting Strings to numbers String to int: use the static Integer.parseInt() method from the Integer class: String input = "12"; int num1 = input String to double: use the static Double.parseDouble() method from the Double class: String guess = "3.1415"; double num2 = guess 12 num1 3.1415 num2 Integer.parseInt( ) ; Double.parseDouble( ) ;
Image of page 4

Subscribe to view the full document.

CS101 Lecture 5 5 1 2 3 4 5 6 7 8 9 10 Input – obtaining a number from the user We need to convert input from the user (which is in the form of a String) into an int which can be processed. public class TimesNumberByFive { public void start() { System.out.print("Enter a number: "); } } Enter a number: 21 Five times your number is 105 "21" 21 String userNum = Keyboard.readInput(); int numberTimes5 = number * 5; int number = Integer.parseInt(userNum); System.out.print("Five times your number "); System.out.println("is " + numberTimes5); 26 What you need to know Operators - order of precedence Automatic conversion between types e.g. int to double Casting - explicitly converting e.g. double to int Static methods e.g. Math.max(), Math.min() Math.random() 27 What you need to know Introduction to Strings. Concatenation - joining two Strings, joining a number to a String Keyboard.readInput() - Reading input from the user Integer.parseInt() - converting a String to an int Double.parseDouble() - converting a String to an double 1 2 3 4 5 6 7 8 9 10 11 12 Ex01 - What is the output? public class Ex01 { public void start() { int a, b; double c; System.out.println(7 + 1 + "4 * 2" + 3 + 1); System.out.println("5" + (7 + 1) + 3 * 2 + 1); a = 5; b = a / 3 + 1; c = a / b; System.out.println(a + ", " + b + ", " + c); } } 84 * 231 5861 5, 2, 2.0 29 1 2 3 4 5 6 7 8 9 Ex02 – User enters 520, Give output > java Ex02 Enter yearly cost $ 520 Cost per week $10.0 public class CalculateCost { public void start() { System.out.print("Enter yearly cost $"); String userNum = Keyboard.readInput(); int costPerYear = Integer.parseInt(userNum); double costPerWeek = costPerYear / 52.0; System.out.print("Cost per week $" + costPerWeek); } } 30 Ex03 > java TemperatureApp Put following statements in the correct order so that the program works as shown above. 5 9 (T – 32) fahr T = cels 1 userTemp = Keyboard.readInput(); 2 tempF = Integer.parseInt(userTemp); 3 System.out.println("Celsius = " + tempC); 4 System.out.print("Enter temperature (F): "); 5 String userTemp; 6 int tempF, tempC; 7 System.out.println("Temperature Conversion"); 8 tempC = (int) ((tempF - 32.0) * 5 / 9.0); 70 21 Temperature conversion Enter temperature (F): Celsius =
Image of page 5
You've reached the end of this preview.
  • Summer '12
  • AdrianaFerraro
  • Computer Science, Order of precedence, String literal, public void

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern