Skip to content

Commit b859180

Browse files
committed
Week 1 Code Samples
0 parents  commit b859180

17 files changed

Lines changed: 393 additions & 0 deletions

Week1/ArrayIntroduction.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package Online_Code_Samples.Week1;
2+
3+
public class ArrayIntroduction {
4+
5+
//Let's write different arrays
6+
static Integer[] intArray = {1, 3, 4, 5, 6};
7+
static int[] numbers = new int[20];
8+
static String[] stringArray = {"Mike", "Pence", "Peace", "John"};
9+
static Object[] objectArray = {new Dog("Rex", 3), 39, "Mike Pence", true};
10+
11+
public static <T> void printArray(T[] array){
12+
for(T obj: array){
13+
System.out.print(obj.toString() + ", ");
14+
}
15+
System.out.println();
16+
}
17+
18+
public static void main(String[] args) {
19+
printArray(intArray);
20+
printArray(stringArray);
21+
printArray(objectArray);
22+
}
23+
}

Week1/Circle.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package Online_Code_Samples.Week1;
2+
3+
public class Circle extends Shape {
4+
private final double radius;
5+
6+
public Circle(double radius) {
7+
this.radius = radius;
8+
}
9+
10+
@Override
11+
public double getRadius() {
12+
return this.radius;
13+
}
14+
15+
@Override
16+
public double area() {
17+
return Math.PI * Math.pow(radius, 2);
18+
}
19+
}

Week1/Debug.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package Online_Code_Samples.Week1;
2+
3+
public class Debug {
4+
5+
public static void main(String[] args) {
6+
int x = 10, y = 11;
7+
8+
for (int j = 1; j < y; j+=1){
9+
System.out.println(j);
10+
}
11+
12+
while( x <= y ){
13+
System.out.println(x++);
14+
}
15+
}
16+
}

Week1/Dog.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package Online_Code_Samples.Week1;
2+
3+
public class Dog {
4+
private String name; //achieve encapsulation by declaring variables as private
5+
private int age;
6+
7+
public Dog(String name, int age) { //Class constructor
8+
this.name = name;
9+
this.age = age;
10+
}
11+
12+
public String getName() { //getter method to access the private variable ‘name’. It’s return type
13+
return name; //is String
14+
}
15+
16+
public void bark() { //void methods do not return any value and do not have a return type
17+
System.out.println("Woof!");
18+
19+
}
20+
21+
public static void main(String[] args) {
22+
Dog dog = new Dog("Alsatian", 2); //A dog object was created with name and age
23+
24+
dog.bark(); //We use the dog object created to call the method 'bark()'
25+
}
26+
}

Week1/Emmanuel.java

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package Online_Code_Samples.Week1;
2+
3+
public class Emmanuel {
4+
private String firstName;
5+
private String lastName;
6+
private String fullName;
7+
private int age;
8+
private String email;
9+
private static int count = 0;
10+
11+
public Emmanuel(String firstName, String lastName, int age, String email) {
12+
this.firstName = firstName;
13+
this.lastName = lastName;
14+
this.age = age;
15+
this.email = email;
16+
this.fullName = firstName + " " + lastName;
17+
count++;
18+
}
19+
20+
public Emmanuel(String firstName, String lastName, String email){
21+
this(firstName, lastName, 35, email);
22+
}
23+
24+
public Emmanuel(String firstName, String lastName){
25+
this(firstName, lastName, "[email protected]");
26+
}
27+
28+
29+
public String getFirstName() {
30+
return firstName;
31+
}
32+
33+
public void setFirstName(String firstName) {
34+
this.firstName = firstName;
35+
}
36+
37+
public String getLastName() {
38+
return lastName;
39+
}
40+
41+
public void setLastName(String lastName) {
42+
this.lastName = lastName;
43+
}
44+
45+
public String getFullName() {
46+
return fullName;
47+
}
48+
49+
public void setFullName(String fullName) {
50+
this.fullName = fullName;
51+
}
52+
53+
public int getAge() {
54+
return age;
55+
}
56+
57+
public void setAge(int age) {
58+
this.age = age;
59+
}
60+
61+
public String getEmail() {
62+
return email;
63+
}
64+
65+
public void setEmail(String email) {
66+
this.email = email;
67+
}
68+
69+
public static int getCount() {
70+
return count;
71+
}
72+
73+
public static void main(String[] args) {
74+
Emmanuel firstEmmanuel = new Emmanuel("First", "Last", "[email protected]");
75+
Emmanuel firstAEmmanuel = new Emmanuel("Firstname", "Lastname");
76+
77+
78+
System.out.println(firstEmmanuel.getFirstName());
79+
System.out.println(firstEmmanuel.getAge());
80+
81+
firstEmmanuel.setAge(65);
82+
System.out.println(firstEmmanuel.getAge());
83+
84+
System.out.println(firstEmmanuel.getFullName());
85+
86+
System.out.println(Emmanuel.count);
87+
88+
System.out.println(getCount());
89+
90+
}
91+
}

Week1/HelloWorld.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package Online_Code_Samples.Week1;
2+
3+
public class HelloWorld {
4+
public static void main(String[] args) {
5+
String greeting = "Hello, World!";
6+
System.out.println(greeting);
7+
}
8+
}

Week1/IfStatement.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package Online_Code_Samples.Week1;
2+
3+
public class IfStatement {
4+
5+
public static void main(String[] args) {
6+
int x = 25;
7+
int y = 30;
8+
if( x > y) //condition is always false; the next line will not be executed
9+
System.out.println("x is greater than y");
10+
}
11+
}

Week1/IncrementalDemo.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package Online_Code_Samples.Week1;
2+
3+
public class IncrementalDemo {
4+
public static void main(String[] args) {
5+
6+
int count = 1;
7+
8+
System.out.println(++count + count++);
9+
System.out.println(count);
10+
11+
System.out.println(++count + (--count + count--));
12+
System.out.println(count);
13+
}
14+
}

Week1/PrimitiveDataTypes.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package Online_Code_Samples.Week1;
2+
3+
public class PrimitiveDataTypes {
4+
public static void main(String[] args) {
5+
6+
byte bite = 127; //1 bytes or 8 bits
7+
System.out.println(Byte.MAX_VALUE);
8+
System.out.println(bite);
9+
10+
boolean bool = true && false; //1 bytes or 8 bits
11+
Boolean boolean1 = false;
12+
System.out.println(bool);
13+
14+
short shot = 32767; //2 bytes or 16 bits
15+
// shot = bite;
16+
bite = (byte) shot;
17+
System.out.println(bite);
18+
char character = 'B'; //2 bytes or 16 bits
19+
20+
int integer = -214748364; //4 bytes or 32 bits
21+
bite = (byte) integer;
22+
System.out.println(bite);
23+
float floater = 234445234F; //4 bytes or 32 bits
24+
25+
double doubleNumber = 56444.60; //8 bytes or 64 bits
26+
integer = (int) (doubleNumber + doubleNumber);
27+
System.out.println(integer);
28+
long longNumber = 8999999999999999L; //8 bytes or 64 bits
29+
30+
31+
32+
}
33+
34+
}

Week1/PrimitiveTypes.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package Online_Code_Samples.Week1;
2+
3+
import java.nio.charset.StandardCharsets;
4+
import java.util.Arrays;
5+
import java.util.Scanner;
6+
7+
public class PrimitiveTypes {
8+
9+
public static void main(String[] args) {
10+
System.out.print("Please type the word \"string\" in the prompt: ");
11+
String aString;
12+
Scanner scanner = new Scanner(System.in);
13+
aString = scanner.next();
14+
int anInt = 1000;
15+
boolean booleanValue = (aString.equals("string"));
16+
char charValue = 'A';
17+
double doubleValue = 1.50;
18+
float floatValue = 2000F;
19+
long longValue = 3445495996L;
20+
short shortValue = 12900;
21+
byte byteValue = 127;
22+
23+
System.out.println(anInt);
24+
System.out.println(Arrays.toString(aString.getBytes(StandardCharsets.UTF_16BE)));
25+
System.out.println(Arrays.toString(aString.getBytes()));
26+
System.out.println(aString.repeat(3));
27+
System.out.println(booleanValue);
28+
System.out.println(charValue);
29+
System.out.println(doubleValue);
30+
System.out.println(floatValue);
31+
System.out.println(longValue);
32+
System.out.println(shortValue);
33+
System.out.println(byteValue );
34+
}
35+
}

0 commit comments

Comments
 (0)