From 0d3b7bf545652ec0822e653fd5d2c7ab599e0a07 Mon Sep 17 00:00:00 2001 From: k0rrluna Date: Wed, 20 Nov 2024 01:42:50 +0300 Subject: [PATCH] labs --- labs/lab3/LuckyNumbers.java | 24 +++++++++++++++++ labs/lab3/RightTriangle.java | 25 ++++++++++++++++++ labs/lab3/StringManips.java | 50 +++++++++++++++++++++++++++++++++++ labs/lab3/StringPlay.class | Bin 0 -> 1337 bytes labs/lab3/StringPlay.java | 21 +++++++++++++++ 5 files changed, 120 insertions(+) create mode 100644 labs/lab3/LuckyNumbers.java create mode 100644 labs/lab3/RightTriangle.java create mode 100644 labs/lab3/StringManips.java create mode 100644 labs/lab3/StringPlay.class create mode 100644 labs/lab3/StringPlay.java 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 0000000000000000000000000000000000000000..72f0ce836bcb39cf658f0bd07cc0f046a5aae74f GIT binary patch literal 1337 zcmaJ>>rN9v6#fQEx0IDaxmE=i<UhqZ{g(89$sNrIazlJi>h3#zJonm+d-@w<< zACO?;1NczJGhGbZKy1^Q)3ay3?>m>--+#`20eFs594%-y5HZn)C_~o)KjJxu%l+KO z?tv&PhUlCvZ8gu(n$GMPh&2UmD$kbtF~k`{j?+e8vQMC%B-uPP1N)pqou-@t&0L8O`m9Q#UCDax&r z*1mwpc5@}trHD%KYCcJ+-Y{_!Lkt5|ekcmAEOYhVRu!^*#ijBYCaw^;cf9t0zb+d4RL0XeP9Or2kJUF9o}*VcrpxO;vg3L2Y56bvkBEw30xFJ{e_NA97>1u#L^ zmw8#a9!dNb&hk7k0A1m7&k_D;(RB~&wQyMjps=Uu;MQ@isSD@f;Z%cLl@GV%9HPJ- zXVX?9AH!>gL1s}q6cx+2$fP!tZsGaUF|5*@Z&if#$(G!)s2FquEt|sVHmyoY1Lknw z7ruca!`P)_T$Q7Nb%x>phthxq1;GfO&|5=)T4)65y+P|U`Zq*7qrGkN6zn@0470TA z$9io?9&=>t(64Bv$phZ~gz+KSeu|D_!|Eh!ZR!MF^FPo#HFSpSzzK%Qb7wY^iXm`X?ZmcVK)4*VOi#2Q@^%sjKQC9!} literal 0 HcmV?d00001 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