From 30b3b9a477dcc1d06d3e025e0613e60533548531 Mon Sep 17 00:00:00 2001 From: "John P. Baugh" Date: Wed, 26 Jan 2022 23:42:12 -0500 Subject: [PATCH] Add files via upload --- section5/NameParser.java | 25 +++++++++++++++++ section5/Proj5_1_NamePermutations.java | 39 ++++++++++++++++++++++++++ section5/StringBuilderFun.java | 24 ++++++++++++++++ section5/StringMethods1.java | 38 +++++++++++++++++++++++++ section5/StringMethods2.java | 18 ++++++++++++ 5 files changed, 144 insertions(+) create mode 100644 section5/NameParser.java create mode 100644 section5/Proj5_1_NamePermutations.java create mode 100644 section5/StringBuilderFun.java create mode 100644 section5/StringMethods1.java create mode 100644 section5/StringMethods2.java diff --git a/section5/NameParser.java b/section5/NameParser.java new file mode 100644 index 0000000..c4d5035 --- /dev/null +++ b/section5/NameParser.java @@ -0,0 +1,25 @@ +import java.util.Scanner; + +public class NameParser { + public static void main(String[] args) { + Scanner keyboard = new Scanner(System.in); + + String fullName; + String firstName; + String lastName; + + System.out.println("What is your full name?"); + fullName = keyboard.nextLine(); + + int indexOfSpace = fullName.indexOf(" "); + + firstName = fullName.substring(0, indexOfSpace); + lastName = fullName.substring(indexOfSpace + 1); + + firstName = firstName.toUpperCase(); + lastName = lastName.toLowerCase(); + + System.out.println("First name is " + firstName); + System.out.println("Last name is " + lastName); + }//end main +} diff --git a/section5/Proj5_1_NamePermutations.java b/section5/Proj5_1_NamePermutations.java new file mode 100644 index 0000000..4720e7a --- /dev/null +++ b/section5/Proj5_1_NamePermutations.java @@ -0,0 +1,39 @@ + +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.Scanner; + +public class Proj5_1_NamePermutations { + public static void main(String[] args) { + ArrayList firstNames = new ArrayList<>(); + ArrayList lastNames = new ArrayList<>(); + + Scanner keyboard = new Scanner(System.in); + String fullName; + String firstName; + String lastName; + int indexOfSpace; + + final int NUM_NAMES = 5; + + for(int i = 0; i < NUM_NAMES; i++) { + System.out.print("Please enter name " + i + "\t"); + fullName = keyboard.nextLine(); + indexOfSpace = fullName.indexOf(" "); + firstName = fullName.substring(0, indexOfSpace); + lastName = fullName.substring(indexOfSpace + 1); + + firstNames.add(firstName); + lastNames.add(lastName); + }//end for + + //now for the permutations + + for(int i = 0; i < firstNames.size(); i++) { + for(int j = 0; j < lastNames.size(); j++) { + System.out.print(firstNames.get(i) + " "); + System.out.println(lastNames.get(j)); + }//end for j + }//end for i + }//end main +} diff --git a/section5/StringBuilderFun.java b/section5/StringBuilderFun.java new file mode 100644 index 0000000..4ffa531 --- /dev/null +++ b/section5/StringBuilderFun.java @@ -0,0 +1,24 @@ + + +public class StringBuilderFun { + public static void main(String[] args) { + StringBuilder sb = new StringBuilder("John Baugh"); + + sb.append(" is awesome"); + System.out.println(sb); + + sb.insert(5, "Phillip "); + System.out.println(sb); + + sb.replace(22, 29, "amazing"); + System.out.println(sb); + + sb.delete(5, 13); + System.out.println(sb); + + sb.replace(0,4, "Dr."); + + System.out.println(sb); + + }//end main +} diff --git a/section5/StringMethods1.java b/section5/StringMethods1.java new file mode 100644 index 0000000..a799bcf --- /dev/null +++ b/section5/StringMethods1.java @@ -0,0 +1,38 @@ + + +public class StringMethods1 { + public static void main(String[] args) { + String name = "John Baugh"; + String name2 = "John Baugh"; + String name3 = "Rob Percival"; + String challengeName = "Ed Mortram"; + + for(int i = 0; i < name.length(); i++) { + System.out.print(name.charAt(i) + " "); + }//end for + + System.out.println(); + + if(name.equals(name2)) { + System.out.println("Names are equal."); + } + else { + System.out.println("Names aren't equal."); + } + + if(name.compareTo(name3) > 0) { + System.out.println("name > name3"); + } + else { + System.out.println("name <= name3"); + } + + System.out.println("Comparing for the lecture challenge"); + if (challengeName.compareTo(name) > 0) { + System.out.println(challengeName + " is greater than " + name); + } + else { + System.out.println(challengeName + " is less than or equal to " + name); + } + }//end main +} diff --git a/section5/StringMethods2.java b/section5/StringMethods2.java new file mode 100644 index 0000000..9893ef3 --- /dev/null +++ b/section5/StringMethods2.java @@ -0,0 +1,18 @@ + +public class StringMethods2 { + public static void main(String[] args) { + String myName = "John Baugh"; + + String upper = myName.toUpperCase(); + String lower = myName.toLowerCase(); + + int whereIsB = myName.indexOf("B"); + + String lastName = myName.substring(5); + + System.out.println("upper is " + upper); + System.out.println("lower is " + lower); + System.out.println("B is at index " + whereIsB); + System.out.println("Last name is " + lastName); + }//end main +}