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
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class ExerciseServer {
public static void main(String[] args) {
try (ServerSocket serverSocket = new ServerSocket(4040)) {
System.out.println("Server listening on port 4040");
try (Socket socket = serverSocket.accept()) {
System.out.println("client connect");
try (BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
String line;
while ((line = in.readLine()) !=null) {
System.out.println("Received: " + line);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.cbfacademy;

import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class ExerciseClient {
public static void main(String[] args) {
try (Socket socket = new Socket("localhost", 4040)) {
System.out.println("Connected to Server");

try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()))) {
String message = "Hello, Server!";
out.write(message);
out.newLine();
out.flush();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.cbfacademy;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class URLConnectionExample {
public static void main(String[] args) {
try {
URL myURL = new URL("https://codingblackfemales.com/");
URLConnection myURLConnection = myURL.openConnection();
myURLConnection.connect();
String contentType = myURLConnection.getContentType();
System.out.println("Content Type: " + contentType);

if(myURLConnection instanceof HttpURLConnection) {
try(BufferedReader in = new BufferedReader(new InputStreamReader(myURLConnection.getInputStream()))) {
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
}
}

} catch (MalformedURLException e) {
System.out.println("Invalid URL: " + e.getMessage());
} catch (IOException e) {
System.out.println("Error in connection: " + e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.codingblackfemales.academy;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");

}//end of main

}


17 changes: 15 additions & 2 deletions exercises/java-collections/src/main/java/com/cbfacademy/App.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
package com.cbfacademy;

import java.util.*;
import com.cbfacademy.CollectionsExercises;


public class App {
public static void main( String[] args ) {
System.out.println( "Hello World!" );
public static void main(String[] args) {
CollectionsExercises collectionsExercises = new CollectionsExercises();
collectionsExercises.useLinkedList();
collectionsExercises.useStack();
collectionsExercises.useHashMap();
collectionsExercises.useArrayDeque();
}
}





Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,102 @@ public class CollectionsExercises {

public LinkedList<Integer> useLinkedList() {
// TODO: create an empty linked list
// - add 4 as the first element of the list
// - then add 5, 6, 8, 2, 9 to the the list
// - add another 2 as the last element of the list
// - add 4 as the 3rd element of the list
// - invoke the method element() on the list and print the result on the screen
// - return the list
throw new RuntimeException("Not implemented");
LinkedList<Integer> linkedList = new LinkedList<>();
// - add 4 as the first element of the list
linkedList.addFirst(4);
// - then add 5, 6, 8, 2, 9 to the the list
linkedList.add(5);
linkedList.add(6);
linkedList.add(8);
linkedList.add(2);
linkedList.add(9);
// - add another 2 as the last element of the list
linkedList.addLast(2);
// - add 4 as the 3rd element of the list
linkedList.add(2, 4);
// - invoke the method element() on the list and print the result on the screen

System.out.println(linkedList.element());
// - return the list
return linkedList;
// throw new RuntimeException("Not implemented");
}

public Stack<Integer> useStack() {
// TODO: create an empty stack
// - add 5, 6, 8, 9 to the the stack
// - print the first element of the stack on the screen
// - print the last element of the stack on the screen
// - invoke the method pop() on the stack and print the result on the screen
// - invoke the push(4) method on the stack
// - return the stack
throw new RuntimeException("Not implemented");
Stack<Integer> stack = new Stack<>();
// - add 5, 6, 8, 9 to the the stack
stack.push(5);
stack.push(6);
stack.push(8);
stack.push(9);
// - print the first element of the stack on the screen
System.out.println(stack.firstElement());
// - print the last element of the stack on the screen
System.out.println(stack.peek());
// - invoke the method pop() on the stack and print the result on the screen
int poppedElement = stack.pop();
System.out.println(poppedElement);
// - invoke the push(4) method on the stack
stack.push(4);
// - return the stack
return stack;
// throw new RuntimeException("Not implemented");

}

public ArrayDeque<Integer> useArrayDeque() {
// TODO: create an empty arrayDeque
// - add 5, 6, 8, 9 to the the stack
// - print the first element of the queue on the screen
// - print the last element of the queue on the screen
// - invoke the method poll() on the queue and print the result on the screen
// - invoke the element() method on the queue and print the result on the screen
// - return the queue
throw new RuntimeException("Not implemented");
ArrayDeque<Integer> arrayDeque = new ArrayDeque<>();
// - add 5, 6, 8, 9 to the the stack
arrayDeque.add(5);
arrayDeque.add(6);
arrayDeque.add(8);
arrayDeque.add(9);
// - print the first element of the queue on the screen
System.out.println(arrayDeque.getFirst());
// - print the last element of the queue on the screen
System.out.println(arrayDeque.getLast());
// - invoke the method poll() on the queue and print the result on the screen
int polledElement = arrayDeque.poll();
System.out.println(polledElement);
// - invoke the element() method on the queue and print the result on the screen
int peekedElement = arrayDeque.element();
System.out.println(peekedElement);
// - return the queue
return arrayDeque;
// throw new RuntimeException("Not implemented");
}

public HashMap<Integer, String> useHashMap() {
// TODO: create an empty hash map
// - add {1, TypeScript} entry to the map
// - add {2, Kotlin} entry to the map
// - add {3, Python} entry to the map
// - add {4, Java} entry to the map
// - add {5, JavaScript} entry to the map
// - add {6, Rust} entry to the map
// - determine the set of keys from the map and print it on the screen
// - determine the set of keys from the map and print it on the screen
// - determine whether the map contains "English" as a language and print the result on the screen
// - return the map
throw new RuntimeException("Not implemented");
HashMap<Integer, String> hashMap = new HashMap<>();
// - add {1, TypeScript} entry to the map
hashMap.put(1, "TypeScript");
// - add {2, Kotlin} entry to the map
hashMap.put(2, "Kotlin");
// - add {3, Python} entry to the map
hashMap.put(3, "Python");
// - add {4, Java} entry to the map
hashMap.put(4, "Java");
// - add {5, JavaScript} entry to the map
hashMap.put(5, "JavaScript");
// - add {6, Rust} entry to the map
hashMap.put(6, "Rust");
// - determine the set of keys from the map and print it on the screen
Set<Integer> keys = hashMap.keySet();
Collection<String> values = hashMap.values();
System.out.println("" + keys + values + hashMap.containsKey(7));
// - determine the set of keys from the map and print it on the screen

// boolean containsKey7 = hashMap.containsKey(7);
//System.out.println("Contains key 7: " + containsKey7);
// - determine whether the map contains "English" as a language and print the

// result on the screen
// - return the map
return hashMap;
// throw new RuntimeException("Not implemented");
}

public String getName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void workingWithStacks() {
assertThat(result.get(1), is(6));
assertThat(result.get(2), is(8));
assertThat(result.get(3), is(4));
assertThat(outputStreamCaptor.toString().trim(), is("599"));
assertThat(outputStreamCaptor.toString().trim().replaceAll("\n","").replaceAll("\r", ""), is("599"));
}

@Test
Expand All @@ -91,7 +91,7 @@ public void workingWithQueues() {
assertThat(array[0], is(6));
assertThat(array[1], is(8));
assertThat(array[2], is(9));
assertThat(outputStreamCaptor.toString().trim(), is("5956"));
assertThat(outputStreamCaptor.toString().trim().replaceAll("\n","").replaceAll("\r", ""), is("5956"));
}

@Test
Expand Down
11 changes: 10 additions & 1 deletion exercises/java-primitives/src/main/java/com/cbfacademy/App.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
package com.cbfacademy;
import com.cbfacademy.Arithmetic;

public class App {
public static void main( String[] args ) {
public static void main ( String[] args ) {
System.out.println( "Hello World!" );
float operand1 = 9.6f;
float operand2 = 17.3f;
Arithmetic exercises = new Arithmetic();

System.out.println(exercises.AddFloat(operand2, operand2));
System.out.println(exercises.SubtractFloat(operand2, operand2));
System.out.println(exercises.MultiplyFloat(operand2, operand2));
System.out.println(exercises.DivideFloat(operand2, operand2));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.cbfacademy;

public class Arithmetic {
float AddFloat(float operand1, float operand2) {
return operand1 + operand2;
}
float SubtractFloat(float operand1, float operand2) {
return operand1 - operand2;
}
float MultiplyFloat(float operand1, float operand2) {
return operand1 * operand2;
}
float DivideFloat(float operand1, float operand2) {
return operand1 / operand2;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.cbfacademy;

public class MatrixArray {

public static void main(String[] args){
// different arrays
Integer[] intArray = new Integer[8];
Float[] floatArray = new Float[12];
Double[] doubleArray = new Double[5];
Boolean[] booleanArray = new Boolean[6];

for (int i = 0; i < intArray.length; i++) {
intArray[i] = i * 3;
}

for (int i = 0; i < floatArray.length; i++) {
floatArray[i] = (float) (i * 1.3);
}

for (int i = 0; i < doubleArray.length; i++) {
doubleArray[i] = i * 1.7;
}

for (int i = 0; i < booleanArray.length; i++) {
booleanArray[i] = i % 2 == 0;
}

// To print the 5th element
System.out.println("5th element of intArray: " + intArray[4]);
System.out.println("5th element of floatArray: " + floatArray[4]);
System.out.println("5th element of doubleArray: " + doubleArray[4]);
System.out.println("5th element of booleanArray: " + booleanArray[4]);

// initialize matrix with the arrays using Object data type
Object[][] matrix = new Object[4][];
matrix[0] = intArray;
matrix[1] = floatArray;
matrix[2] = doubleArray;
matrix[3] = booleanArray;

// print the matrix
System.out.println("\nMatrix:");
for (Object[] row : matrix) {
for (Object element : row) {
System.out.print(element + " ");
}
System.out.println();
}



}// end main
}


Loading