900 likes | 917 Views
Understand the char data type and String class in Java. Learn about constructing strings, string literals, and methods. Explore string immutability and reading input with Scanner. Dive into ASCII codes and String object properties.
E N D
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 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
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
char Is A Numeric Data Type char ch1 = ‘A’; System.out.println(ch1); A
char Is A Numeric Data Type char ch1 = ‘A’; System.out.println(ch1); System.out.println((int) ch1); A 65
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
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
Adding Two chars Yields An int char ch1 = ‘A’; char ch2 = ‘B’; System.out.println(ch1 + ch2); 131
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).
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
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
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
The String class (cont’d) A null String is not the same as an empty String. String nada = “”; System.out.println(nada);
The String class (cont’d) The length method: String state = “Texas”; intlen = state.length(); System.out.println(len); 5
The String class (cont’d) The length method: String state = “Texas”; System.out.println(state.length()); 5
The String class (cont’d) The charAt method: char ch = state.charAt(2); System.out.println(ch); x
The String class (cont’d) Character positions in strings are numbered starting from 0 String str = “Hello World”;
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
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
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.
Java Programming • Start JCreator. • Create a new file called “Lab03.java”. • Save the new file in your Lab03 folder.
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.
Top-Down Programming (cont’d) The first step in programming is UNDERSTAND THE PROBLEM
Top-Down Programming (cont’d) A pyramid is made by connecting a base to an apex.
Top-Down Programming (cont’d) • Volume = 1/3 × [Base Area] × Height • Surface Area = 2 x [Base Width] × [Side Length]
Top-Down Programming (cont’d) • Side Length =
Top-Down Programming (cont’d) The second step programming is PLAN A SOLUTION
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
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
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.
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
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).
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
Top-Down Programming (cont’d) The third step programming is WRITE THE PROGRAM
Top-Down Programming (cont’d) • Start JCreator. • Create a new file called “Lab03.java”. • Save the new file in your Lab03 folder.
Top-Down Programming (cont’d) importjava.util.Scanner; importjava.text.DecimalFormat;
Top-Down Programming (cont’d) importjava.util.Scanner; importjava.text.DecimalFormat; public class Lab03 { }
Top-Down Programming (cont’d) In the planning phase we determined that we need six instance fields to solve the problem.
Top-Down Programming (cont’d) public class Lab03 { private double volume; private double surfaceArea; privateStringname; privateStringcountry; private double baseWidth; private double height; }
Top-Down Programming (cont’d) The “big problem” is defined in the main method.
Top-Down Programming (cont’d) public static void main(String[] args) { }
Top-Down Programming (cont’d) public static void main(String[] args) { Lab03 lab = new Lab03( ); }