Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/examples/casting/NarrowingConversion.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
15 changes: 15 additions & 0 deletions src/examples/casting/WideningConversion.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
10 changes: 9 additions & 1 deletion src/labs_examples/datatypes_operators/labs/Exercise_01.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
}
Expand Down
7 changes: 6 additions & 1 deletion src/labs_examples/datatypes_operators/labs/Exercise_02.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
}
11 changes: 10 additions & 1 deletion src/labs_examples/datatypes_operators/labs/Exercise_03.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}

Expand Down
8 changes: 8 additions & 0 deletions src/labs_examples/datatypes_operators/labs/Exercise_04.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/labs_examples/datatypes_operators/labs/Exercise_05.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions src/labs_examples/datatypes_operators/labs/Exercise_06.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
9 changes: 5 additions & 4 deletions src/labs_examples/datatypes_operators/labs/Exercise_07.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
}
4 changes: 1 addition & 3 deletions src/labs_examples/fundamentals/labs/Exercise_03.java
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}

}
22 changes: 21 additions & 1 deletion src/labs_examples/fundamentals/labs/Exercise_04.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);


}

Expand Down
22 changes: 17 additions & 5 deletions src/labs_examples/fundamentals/labs/Exercise_05.java
Original file line number Diff line number Diff line change
Expand Up @@ -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

}

Expand Down
22 changes: 22 additions & 0 deletions src/labs_examples/variables/Person.java
Original file line number Diff line number Diff line change
@@ -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 +
'}';
}
}
23 changes: 23 additions & 0 deletions src/labs_examples/variables/Variables.java
Original file line number Diff line number Diff line change
@@ -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);
}
}