From 4a7cfe73d0f7ad9f0bde6de85c5197c93e7c220e Mon Sep 17 00:00:00 2001 From: Anindita Date: Fri, 29 Apr 2022 23:21:11 -0700 Subject: [PATCH 1/3] exercise-3 --- src/labs_examples/fundamentals/labs/Exercise_03.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/labs_examples/fundamentals/labs/Exercise_03.java b/src/labs_examples/fundamentals/labs/Exercise_03.java index 1285716..0cc5a98 100755 --- a/src/labs_examples/fundamentals/labs/Exercise_03.java +++ b/src/labs_examples/fundamentals/labs/Exercise_03.java @@ -15,9 +15,7 @@ public class Exercise_03 { public static void main(String[] args) { System.out.println("Hello World!"); - - // write code to print out "Check out my first program!" below - + System.out.println("Check out my first program!"); } } From efc56513aafc9a5bca1161fa1b15411e3efd68ce Mon Sep 17 00:00:00 2001 From: Anindita Date: Tue, 10 May 2022 00:13:23 -0700 Subject: [PATCH 2/3] submit example 4 & 5 --- .../fundamentals/labs/Exercise_04.java | 22 ++++++++++++++++++- .../fundamentals/labs/Exercise_05.java | 22 ++++++++++++++----- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/labs_examples/fundamentals/labs/Exercise_04.java b/src/labs_examples/fundamentals/labs/Exercise_04.java index fb9ccfb..ab6499e 100644 --- a/src/labs_examples/fundamentals/labs/Exercise_04.java +++ b/src/labs_examples/fundamentals/labs/Exercise_04.java @@ -15,7 +15,27 @@ public static void main(String[] args) { int i = 1; System.out.println("int i is: " + i); - // write your code below + byte j = 5; + System.out.println("byte j is: " + j); + + long k = 56789; + System.out.println("long k is: " + k); + + float l = 7.8f; + System.out.println("float l is: " + l); + + double m = 234.987; + System.out.println("double m is: " + m); + + char n = 'c'; + System.out.println("char n is: " + n); + + short o = 8; + System.out.println("short o is: " + o); + + String p = "ani"; + System.out.println("String p is: " + p); + } diff --git a/src/labs_examples/fundamentals/labs/Exercise_05.java b/src/labs_examples/fundamentals/labs/Exercise_05.java index 66ca1f7..6de894e 100644 --- a/src/labs_examples/fundamentals/labs/Exercise_05.java +++ b/src/labs_examples/fundamentals/labs/Exercise_05.java @@ -13,15 +13,27 @@ public class Exercise_05 { public static void main(String[] args) { String str = "hello!"; - // please declare an int variable below, and set it to the value of the length of "str" + int length = str.length(); + System.out.println("the length of string is: " + str); String str2 = "hello"; - // please initialize a boolean variable and test whether str is equal to str2 + boolean equal = str.equalsIgnoreCase(str2); + System.out.println("str and str2 is equal: " + equal); - // please concatenate str & str2 and set the result to a new String variable below + String concat = str.concat(str2); + System.out.println("concatenate str & str2: " + concat); + + String replace = str.replace('!','?'); + System.out.println("the new string is: " + replace); + + String sub = str.substring(1,4); + System.out.println("the substring is: " + sub); + + Boolean contain = str.contains("ll"); + System.out.println("Is str contain ll: " + contain); + + System.out.println("the index of str of ello: " + str.indexOf("ello")); - // please demonstrate the use of any other method that is available to us in the String class - // for example, replace(), substring(), contains(), indexOf() etc } From 17162e7ab886f52211ddba4d8e6ed8f6a64037dc Mon Sep 17 00:00:00 2001 From: Anindita Date: Fri, 24 Jun 2022 23:57:08 -0700 Subject: [PATCH 3/3] all labs of datatypes_operators --- src/examples/casting/NarrowingConversion.java | 15 ++++++++++++ src/examples/casting/WideningConversion.java | 15 ++++++++++++ .../datatypes_operators/labs/Exercise_01.java | 10 +++++++- .../datatypes_operators/labs/Exercise_02.java | 7 +++++- .../datatypes_operators/labs/Exercise_03.java | 11 ++++++++- .../datatypes_operators/labs/Exercise_04.java | 8 +++++++ .../datatypes_operators/labs/Exercise_05.java | 10 ++++++++ .../datatypes_operators/labs/Exercise_06.java | 8 +++++-- .../datatypes_operators/labs/Exercise_07.java | 9 ++++---- src/labs_examples/variables/Person.java | 22 ++++++++++++++++++ src/labs_examples/variables/Variables.java | 23 +++++++++++++++++++ 11 files changed, 129 insertions(+), 9 deletions(-) create mode 100644 src/examples/casting/NarrowingConversion.java create mode 100644 src/examples/casting/WideningConversion.java create mode 100644 src/labs_examples/variables/Person.java create mode 100644 src/labs_examples/variables/Variables.java diff --git a/src/examples/casting/NarrowingConversion.java b/src/examples/casting/NarrowingConversion.java new file mode 100644 index 0000000..aca2c16 --- /dev/null +++ b/src/examples/casting/NarrowingConversion.java @@ -0,0 +1,15 @@ +package examples.casting; + +public class NarrowingConversion { + public static void main(String[] args) { + double x = 10.23; + System.out.println("x " + x); + int z = (int) x; + System.out.println("z " + z); + double d = 100.04; + long l = (long)d; + int i = (int)l; + System.out.println("long value " + l); + System.out.println("Int value " + i); + } +} diff --git a/src/examples/casting/WideningConversion.java b/src/examples/casting/WideningConversion.java new file mode 100644 index 0000000..58814c1 --- /dev/null +++ b/src/examples/casting/WideningConversion.java @@ -0,0 +1,15 @@ +package examples.casting; + +public class WideningConversion { + public static void main (String[] args){ + int i = 100; + long l = i; + float f = l; + double x = f; + System.out.println("int value " + i); + System.out.println("long value " + l); + System.out.println("float value " + f); + System.out.println("double value " + x); + } + +} diff --git a/src/labs_examples/datatypes_operators/labs/Exercise_01.java b/src/labs_examples/datatypes_operators/labs/Exercise_01.java index 4ed1dab..4bb4797 100755 --- a/src/labs_examples/datatypes_operators/labs/Exercise_01.java +++ b/src/labs_examples/datatypes_operators/labs/Exercise_01.java @@ -16,7 +16,15 @@ public class Exercise_01 { public static void main(String[] args) { - // write code here + int a = 4; + double b = 6.8; + float c = 3.5f; + char d = 'd'; + + System.out.println("int a = " + a); + System.out.println("double b = " + b); + System.out.println("float c = " + c); + System.out.println("char d = " + d); } } diff --git a/src/labs_examples/datatypes_operators/labs/Exercise_02.java b/src/labs_examples/datatypes_operators/labs/Exercise_02.java index 62ad7d9..68abdf6 100755 --- a/src/labs_examples/datatypes_operators/labs/Exercise_02.java +++ b/src/labs_examples/datatypes_operators/labs/Exercise_02.java @@ -23,7 +23,12 @@ public class Exercise_02 { public static void main(String[] args) { - // write code here + int a = 10; + double b = a; + System.out.println("double b = " + b); + double c = 10.5; + int d = (int) c; + System.out.println("int d = " + d); } } diff --git a/src/labs_examples/datatypes_operators/labs/Exercise_03.java b/src/labs_examples/datatypes_operators/labs/Exercise_03.java index ef6e06c..cac2441 100755 --- a/src/labs_examples/datatypes_operators/labs/Exercise_03.java +++ b/src/labs_examples/datatypes_operators/labs/Exercise_03.java @@ -9,8 +9,17 @@ class ArithmeticOperators { public static void main(String[] args) { + int a = 2 + 3; + double b = 3.5- 2.8; + double c = 2.3 * 3.4; + double d = 8 / 3; + int e = 7 % 3; - // write your code below + System.out.println("after addition " + a); + System.out.println("After subtraction " + b); + System.out.println("After multiplication " + c); + System.out.println("After division " + d); + System.out.println("After modulus " + e); } diff --git a/src/labs_examples/datatypes_operators/labs/Exercise_04.java b/src/labs_examples/datatypes_operators/labs/Exercise_04.java index 029f4cb..c0b7563 100755 --- a/src/labs_examples/datatypes_operators/labs/Exercise_04.java +++ b/src/labs_examples/datatypes_operators/labs/Exercise_04.java @@ -15,6 +15,14 @@ public static void main(String[] args) { int b = 2; if (a < b){ System.out.println("a is less than b"); + } if(b > a){ + System.out.println("b is greater than a"); + }if( a == b){ + System.out.println("a is equals to b"); + }if(a <= b ){ + System.out.println("a is less than b or a is equals to b"); + }if( b >= a){ + System.out.println("b is greater than a or b is equals to a"); } // write your code below diff --git a/src/labs_examples/datatypes_operators/labs/Exercise_05.java b/src/labs_examples/datatypes_operators/labs/Exercise_05.java index 8e26386..8e67a4b 100755 --- a/src/labs_examples/datatypes_operators/labs/Exercise_05.java +++ b/src/labs_examples/datatypes_operators/labs/Exercise_05.java @@ -15,6 +15,16 @@ public static void main(String[] args) { boolean b = false; if (a | b){ System.out.println("a or b is true"); + }if (a || b){ + System.out.println("if a is true then check b"); + }if(a & b){ + System.out.println("a and b both are true"); + }if (a && b){ + System.out.println("both are true"); + }if (a ^ b){ + System.out.println("a and b are different"); + }if (a != b){ + System.out.println("a not equals to b"); } // write your code below diff --git a/src/labs_examples/datatypes_operators/labs/Exercise_06.java b/src/labs_examples/datatypes_operators/labs/Exercise_06.java index ab6c2fc..455cad9 100644 --- a/src/labs_examples/datatypes_operators/labs/Exercise_06.java +++ b/src/labs_examples/datatypes_operators/labs/Exercise_06.java @@ -12,7 +12,11 @@ public class Exercise_06 { public static void main(String[] args) { - // write code here - + double r = 3.14; + double h = 5; + double volume = 3.14 * r * r * h; + System.out.println("the volume of cylinder is " + volume); + double surface = 2 * 3.14 * r * h + 2 * 3.14 * r * r; + System.out.println("the surface of the cylinder " + surface); } } \ No newline at end of file diff --git a/src/labs_examples/datatypes_operators/labs/Exercise_07.java b/src/labs_examples/datatypes_operators/labs/Exercise_07.java index 4b03867..6d5d9d8 100644 --- a/src/labs_examples/datatypes_operators/labs/Exercise_07.java +++ b/src/labs_examples/datatypes_operators/labs/Exercise_07.java @@ -16,14 +16,15 @@ public class Exercise_07 { public static void main(String[] args) { - // create scanner + Scanner scanner = new Scanner(System.in); - // prompt user + System.out.print("Enter a number in days between 1 and 1,000,000: "); - // assign input to variable as int + int days = scanner.nextInt(); - // write completed code here + long second = days * 24 * 60 * 60; + System.out.println("the total seconds of " + days + " are " + second); } } \ No newline at end of file diff --git a/src/labs_examples/variables/Person.java b/src/labs_examples/variables/Person.java new file mode 100644 index 0000000..df6106e --- /dev/null +++ b/src/labs_examples/variables/Person.java @@ -0,0 +1,22 @@ +package labs_examples.variables; + +public class Person { + String name; + int age; + static int numPersonsCreated; + + public Person(String name, int age) { + this.name = name; + this.age = age; + numPersonsCreated ++; + } + + @Override + public String toString() { + return "Person{" + + "name='" + name + '\'' + + ", age=" + age + + ",numPersonsCreated=" + numPersonsCreated + + '}'; + } +} diff --git a/src/labs_examples/variables/Variables.java b/src/labs_examples/variables/Variables.java new file mode 100644 index 0000000..af13ab0 --- /dev/null +++ b/src/labs_examples/variables/Variables.java @@ -0,0 +1,23 @@ +package labs_examples.variables; + +public class Variables { + static double val = 123.45; + public static void main(String[] args){ + Person myPerson = new Person("Rayan", 35); + Person yourPerson = new Person("someCoolName", 39); + Person einstien = new Person("Einstein", 110); + System.out.println(myPerson.toString()); + System.out.println(yourPerson.toString()); + System.out.println(einstien.toString()); + // double x = multiply(val,val * 2); + } + public static double multiply(double a, double b){ + double result = a * b; + printNum(result); + return result; + } + public static void printNum(double numToPrint){ + System.out.println(numToPrint); + System.out.println(val); + } +}