1 / 90

char data type and the String class

char data type and the String class. Learn about the char data type. Learn about string literals Learn about String constructors and commonly used methods Understand immutability of strings Learn how to read string from the keyboard using the Scanner class. Objectives:. ASCII Code.

jcarole
Download Presentation

char data type and the String class

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. char data type and the String class

  2. Learn about the char data type. Learn about string literals Learn about String constructors and commonly used methods Understand immutability of strings Learn how to read string from the keyboard using the Scanner class Objectives:

  3. ASCII Code

  4. ASCII stands for American Standard Code for Information Interchange. Computers can only understand numbers. An ASCII code is the numerical representation of a character such as 'a' or '@' or an action of some sort. ASCII Code

  5. The char Data Type

  6. char Data Type • A char is a variable that holds a single character. Characters are represented by their ASCII values. • char c = ‘A’; orchar c = 65; means: 1. c is a char data type. 2. c is storing the value 65 which is the ASCII value for the character A. 65 c

  7. char Is A Numeric Data Type char ch1 = ‘A’; System.out.println(ch1); A

  8. char Is A Numeric Data Type char ch1 = ‘A’; System.out.println(ch1); System.out.println((int) ch1); A 65

  9. char Is A Numeric Data Type char ch1 = ‘A’; System.out.println(ch1); System.out.println((int) ch1); inti = 65; System.out.println(i); A 65 65

  10. char Is A Numeric Data Type char ch1 = ‘A’; System.out.println(ch1); System.out.println((int) ch1); inti = 65; System.out.println(i); System.out.println((char) i); A 65 65 A

  11. Adding Two chars Yields An int char ch1 = ‘A’; char ch2 = ‘B’; System.out.println(ch1 + ch2); 131

  12. The String Class

  13. The String class • An object of the String class represents a string of characters. (words or sentences) • The String class belongs to the java.lang package, which is built into Java. • Like other classes, String has constructors and methods. • Unlike other classes, String has two operators, + and += (used for concatenation).

  14. The String class (cont’d) There are two ways to declare an object of the String class: String jim = new String(“Jim Bowie”); System.out.println(jim); Jim Bowie

  15. The String class (cont’d) There are two ways to declare an object of the String class: String jim = new String(“Jim Bowie”); System.out.println(jim); String richard = “Richard Widmark”; Jim Bowie System.out.println(richard); Richard Widmark

  16. The String class (cont’d) Failing to assign a value to a String results in a null string. String nada; /* declared as an instance field */ System.out.println(nada); null

  17. The String class (cont’d) A null String is not the same as an empty String. String nada = “”; System.out.println(nada);

  18. The String class (cont’d) The length method: String state = “Texas”; intlen = state.length(); System.out.println(len); 5

  19. The String class (cont’d) The length method: String state = “Texas”; System.out.println(state.length()); 5

  20. The String class (cont’d) The charAt method: char ch = state.charAt(2); System.out.println(ch); x

  21. The String class (cont’d) Character positions in strings are numbered starting from 0 String str = “Hello World”;

  22. The String class (cont’d) The + operator is used to concatenate two or more string together. String city = “San Antonio”; String location = city + “, ”+ state; System.out.println(location); San Antonio, Texas

  23. The String class (cont’d) Primitive data type can also be concatenated into a string using the + operator. intzipCode = 77040; location = location + zipCode; System.out.println(location); San Antonio, Texas 77040

  24. Immutability • Once created, a string cannot be changed: none of its methods changes the string. • Such types of objects are called immutable. • Immutable objects are convenient because two references can point to the same object safely. • There is no danger of changing an object through one reference without the others being aware of the change.

  25. Top Down Design

  26. Java Programming • Start JCreator. • Create a new file called “Lab03.java”. • Save the new file in your Lab03 folder.

  27. Top-Down Programming WAP that reads the name, country of origin, width of the base, and the height of a pyramid. Calculate the surface area and the volume of the pyramid and display the results. There are many kinds of pyramids. The pyramids found in Egypt and South America are square pyramids. NOTE: The Surface Area has two parts: the area of the sides (the Lateral Area) and the area of the base (the Base Area). For this exercise we are going to solve only for the Lateral Area.

  28. Top-Down Programming (cont’d) The first step in programming is UNDERSTAND THE PROBLEM

  29. Top-Down Programming (cont’d) A pyramid is made by connecting a base to an apex.

  30. Top-Down Programming (cont’d) • Volume = 1/3 × [Base Area] × Height • Surface Area = 2 x [Base Width] × [Side Length]

  31. Top-Down Programming (cont’d) • Side Length =

  32. Top-Down Programming (cont’d) The second step programming is PLAN A SOLUTION

  33. Top-Down Programming (cont’d) What fields are required to solve the problem? NOTE: Fields should be the values we are solving for and the values we will read from the keyboard. double volume; doublesurfaceArea; String name; String country; doublebaseWidth; double height; Values to be calculate Values to be read from the keyboard

  34. Top-Down Programming (cont’d) The big picture is read the name of the pyramid, the country of origin, the base width, and the height of the pyramid from the keyboard, calculate the volume and surface area of the pyramid, and display the results on the console screen. How many methods do we need? 3

  35. Top-Down Programming (cont’d) Output will be fairly simple. We need a statement that displays the name of the pyramid, the surface area and the volume of the pyramid. <name> in <country> has a surface area of <Surface Area> square feet and a volume of <Volume> cubic feet.

  36. Top-Down Programming (cont’d) Input will also be very simple. We need prompts to precede each value entered. We need to read a line of text and then two doubles. Enter the base width: 755.9 Enter the height: 480.6 Enter the Name of the pyramid: The Great Pyramid Of Giza Enter the Country of Origin: Egypt

  37. Top-Down Programming (cont’d) Process is a more complex method and will involve several calculations. • We need to calculate the volume. • We need to calculate the surface area. • The formula for surface area requires the side length (which is the hypotenuse of a right triangle).

  38. Top-Down Programming (cont’d) volume= 1/3 * base area * height side length = sqrt( (baseWidth/2)2 + height2 ) surface area = 2 * base width * side length

  39. Top-Down Programming (cont’d) The third step programming is WRITE THE PROGRAM

  40. Top-Down Programming (cont’d) • Start JCreator. • Create a new file called “Lab03.java”. • Save the new file in your Lab03 folder.

  41. Top-Down Programming (cont’d) importjava.util.Scanner; importjava.text.DecimalFormat;

  42. Top-Down Programming (cont’d) importjava.util.Scanner; importjava.text.DecimalFormat; public class Lab03 { }

  43. Top-Down Programming (cont’d) In the planning phase we determined that we need six instance fields to solve the problem.

  44. Top-Down Programming (cont’d) public class Lab03 { private double volume; private double surfaceArea; privateStringname; privateStringcountry; private double baseWidth; private double height; }

  45. Top-Down Programming (cont’d) The “big problem” is defined in the main method.

  46. Top-Down Programming (cont’d) public static void main(String[] args) { }

  47. Top-Down Programming (cont’d) public static void main(String[] args) { Lab03 lab = new Lab03( ); }

More Related