diff --git a/labs/lab3/LuckyNumbers.java b/labs/lab3/LuckyNumbers.java new file mode 100644 index 0000000..ac8ceaa --- /dev/null +++ b/labs/lab3/LuckyNumbers.java @@ -0,0 +1,24 @@ +// ************************************************** +// LuckyNumbers.java +// +// To generate three random "lucky" numbers +// ************************************************** +import java.util.Random; +public class LuckyNumbers +{ +public static void main (String[] args) +{ +Random generator = new Random(); +int lucky1, lucky2, lucky3; +// Generate lucky1 (a random integer between 50 and 79) using the +// nextInt method (with no parameter) +lucky1 = generator.nextInt(30) + 50; +// Generate lucky2 (a random integer between 90 and 100) using the +// nextInt method with an integer parameter +lucky2 = generator.nextInt(11) + 90; +// Generate lucky3 (a random integer between 11 and 30) using nextFloat +lucky3 = (int) (generator.nextFloat() * 20) + 11; +System.out.println ("Your lucky numbers are " + lucky1 + ", " + lucky2 ++ ", and " + lucky3); +} +} \ No newline at end of file diff --git a/labs/lab3/RightTriangle.java b/labs/lab3/RightTriangle.java new file mode 100644 index 0000000..6ea59fa --- /dev/null +++ b/labs/lab3/RightTriangle.java @@ -0,0 +1,25 @@ +// ******************************************************************* +// RightTriangle.java +// +// Compute the length of the hypotenuse of a right triangle +// given the lengths of the sides +// ******************************************************************* +import java.util.Scanner; +public class RightTriangle +{ +public static void main (String[] args) +{ +double side1, side2; // lengths of the sides of a right triangle +double hypotenuse; // length of the hypotenuse +Scanner scan = new Scanner(System.in); +// Get the lengths of the sides as input +System.out.print ("Please enter the lengths of the two sides of " + +"a right triangle (separate by a blank space): "); +side1 = scan.nextDouble(); +side2 = scan.nextDouble(); +// Compute the length of the hypotenuse +hypotenuse = Math.sqrt(Math.pow(side1, 2) + Math.pow(side2, 2)); +// Print the result +System.out.println ("Length of the hypotenuse: " + hypotenuse); +} +} \ No newline at end of file diff --git a/labs/lab3/StringManips.java b/labs/lab3/StringManips.java new file mode 100644 index 0000000..7d13c39 --- /dev/null +++ b/labs/lab3/StringManips.java @@ -0,0 +1,50 @@ +// *************************************************************** +// StringManips.java +// +// Test several methods for manipulating String objects +// *************************************************************** +import java.util.Scanner; +public class StringManips +{ +public static void main (String[] args) +{ +String phrase = new String ("This is a String test."); +int phraseLength; // number of characters in the phrase String +int middleIndex; // index of the middle character in the String +String firstHalf; // first half of the phrase String +String secondHalf; // second half of the phrase String +String switchedPhrase; // a new phrase with original halves switched +String middle3; +String city; +String state; +Scanner scan = new Scanner(System.in); + +System.out.println("Enter your city!"); +city = scan.nextLine(); +System.out.println("Enter your state!"); +state = scan.nextLine(); +String newString = state.toUpperCase() + city.toLowerCase() + state.toUpperCase(); +// compute the length and middle index of the phrase +phraseLength = phrase.length(); +middleIndex = phraseLength / 2; +// get the substring for each half of the phrase +firstHalf = phrase.substring(0,middleIndex); +secondHalf = phrase.substring(middleIndex, phraseLength); +// concatenate the firstHalf at the end of the secondHalf +switchedPhrase = secondHalf.concat(firstHalf); +switchedPhrase = switchedPhrase.replace(" ", "*"); + +middle3 = phrase.substring(middleIndex - 1, middleIndex + 2); +// print information about the phrase +System.out.println(); +System.out.println ("Original phrase: " + phrase); +System.out.println ("Length of the phrase: " + phraseLength + +" characters"); +System.out.println ("Index of the middle: " + middleIndex); +System.out.println ("Character at the middle index: " + +phrase.charAt(middleIndex)); +System.out.println ("Switched phrase: " + switchedPhrase); +System.out.println(); +System.out.println(newString); +} +} \ No newline at end of file diff --git a/labs/lab3/StringPlay.class b/labs/lab3/StringPlay.class new file mode 100644 index 0000000..72f0ce8 Binary files /dev/null and b/labs/lab3/StringPlay.class differ diff --git a/labs/lab3/StringPlay.java b/labs/lab3/StringPlay.java new file mode 100644 index 0000000..ebf1726 --- /dev/null +++ b/labs/lab3/StringPlay.java @@ -0,0 +1,21 @@ +// ************************************************** +// StringPlay.java +// +// Play with String objects +// ************************************************** +public class StringPlay +{ +public static void main (String[] args) +{ +String college = new String ("PoDunk College"); +String town = new String ("Anytown, USA"); // part (a) +int stringLength; +String change1, change2, change3; +stringLength = college.length(); // part (b) +System.out.println (college + " contains " + stringLength + " characters."); +change1 = college.toUpperCase(); // part (c) +change2 = change1.replace("O","*"); // part (d) +change3 = college.concat(", ").concat(town); // part (e) +System.out.println ("The final string is " + change3); +} +} \ No newline at end of file