Presentation is loading. Please wait.

Presentation is loading. Please wait.

Math and String Examples

Similar presentations


Presentation on theme: "Math and String Examples"— Presentation transcript:

1 Math and String Examples

2 The distance between 2 points
The distance between the points (x1,y1) and (x2,y2) is given by the formula:

3 Continued on the next slide
Distance.java import java.io.*; public class Distance { public static void main(String[] args) throws IOException { BufferedReader inData = new BufferedReader( new InputStreamReader(System.in)); double x1, y1,x2, y2; Continued on the next slide

4 Continued on the next slide
Distance.java System.out.print("Enter x1: "); x1 = Double.parseDouble(inData.readLine()); System.out.print("Enter y1: "); y1 = Double.parseDouble(inData.readLine()); System.out.print("Enter x2: "); x2 = Double.parseDouble(inData.readLine()); System.out.print("Enter y2: "); y2 = Double.parseDouble(inData.readLine()); Continued on the next slide

5 Distance.java double dist = Math.sqrt(Math.pow(x2-x1,2) Math.pow(y2-y1,2)); System.out.println("The distance is “ + dist); } }

6 Output of Distance

7 String class methods The next example demonstrates the use of the String methods: length substring indexOf charAt

8 Continued on the next slide
StringDemo.java import java.io.*; public class StringDemo { public static void main(String[] args) throws IOException { BufferedReader inData = new BufferedReader( new InputStreamReader(System.in)); String string1, string2, string3; string1 = "abcdefghijklmnopqrstuvwxyz"; Continued on the next slide

9 Continued on the next slide
StringDemo.java System.out.println(string1 + " has " string1.length() + " characters"); int position = string1.indexOf("jkl"); System.out.println("\"jkl\" occurs at " "position " + position " in the string " + string1); position = string1.indexOf("BOB"); System.out.println("\"BOB\" occurs at " "position " + position " in the string " + string1); Continued on the next slide

10 Continued on the next slide
StringDemo.java string2 = string1.substring(9, 14); System.out.println("string1.substring(9,14) = " string2); string3 = string1.substring(9); System.out.println("string1.substring(9) = " string3); Continued on the next slide

11 StringDemo.java System.out.println("The character at " "position 5 in the string “ string1 + " is " string1.charAt(5)); } }

12 Output of StringDemo


Download ppt "Math and String Examples"

Similar presentations


Ads by Google