Java Labs
Java Labs
Java Labs
Code Output
class HelloWorld {
public static void main(String[] args) {
System.out.println("Try programiz.pro");
}
}
public class PrimitiveExample1 {
public static void main(String[] args) {
int a = 5;
int b = 10;
int sum = a + b;
System.out.println("Sum: " + sum);
}
}
1
Java Labs Dr Benabderrezak
2
Java Labs Dr Benabderrezak
smallNumber += 5;
System.out.println("Small Number: " + smallNumber);
}
}
3
Java Labs Dr Benabderrezak
Homework
public class BankingSystem {
public static void main(String[] args) {
String accountHolder = "John Doe";
int accountNumber = 123456;
int balance = 0;
int depositAmount = 500;
int withdrawAmount = 200;
int exitChoice = 1;
System.out.println((exitChoice == 1) ? "Exiting the Banking System. Thank you!" : "You chose to continue.");
}
}
Output
4
Java Labs Dr Benabderrezak
Part 2 : Loops
Code Output
5
Java Labs Dr Benabderrezak
attempts++;
guess++;
} while (guess != secretNumber);
System.out.println("Guessed the number in " + attempts + " attempts!");
}
}
7
Java Labs Dr Benabderrezak
// Homework
// Homework
8
Java Labs Dr Benabderrezak
Part 3 : Arrays
Code Output
public class OneDArray {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
for (int i = 0; i < numbers.length; i++) {
System.out.println("Element at index " + i + ": " + numbers[i]);
}
}
}
9
Java Labs Dr Benabderrezak
import java.util.ArrayList;
import java.util.ArrayList;
numbers.add(30);
numbers.remove(1);
System.out.println("After removal: " + numbers);
}
}
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashMap;
};
int[][] transpose = new int[3][3];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
transpose[j][i] = matrix[i][j];
}
}
System.out.println("Transposed Matrix:");
for (int[] row : transpose) {
for (int element : row) {
System.out.print(element + " ");
}
System.out.println();
}
}
}
import java.util.ArrayList;
import java.util.Collections;
Collections.sort(numbers);
System.out.println("Sorted ArrayList: " + numbers);
}
}
13
Java Labs Dr Benabderrezak
}
}
import java.util.HashMap;
import java.util.ArrayList;
numbers.add(2);
numbers.add(8);
import java.util.HashMap;
import java.util.HashMap;
map.put("Alice", 25);
System.out.println("Is the map empty after adding an element? " + map.isEmpty());
}
}
import java.util.HashMap;
map.put("Charlie", 35);
import java.util.HashMap;
import java.util.HashMap;
map1.putAll(map2);
System.out.println("Merged Map: " + map1);
}
}
import java.util.HashMap;
Part 4 : Strings
Code Output
public class ConcatenateStrings {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = " World";
String result = str1 + str2;
System.out.println("Concatenated String: " + result);
}
}
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.LinkedHashMap;
int count = 1;
for (int i = 1; i < str.length(); i++) {
if (str.charAt(i) == str.charAt(i - 1)) {
count++;
} else {
compressed.append(str.charAt(i - 1)).append(count);
count = 1;
}
}
compressed.append(str.charAt(str.length() - 1)).append(count);
20
Java Labs Dr Benabderrezak
}
}
import java.util.Arrays;
import java.util.HashSet;
21
Java Labs Dr Benabderrezak
Part 5 : Functions
code Output
public class SimpleFunction {
public static int add(int a, int b) {
return a + b;
}
22
Java Labs Dr Benabderrezak
23
Java Labs Dr Benabderrezak
import java.util.function.BiFunction;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
}
}
import java.util.function.BiFunction;
import java.util.Arrays;
import java.util.List;
26
Java Labs Dr Benabderrezak
Part 6 : OOP
Code Output
class Dog {
String name;
int age;
void bark() {
System.out.println(name + " says Woof!");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.name = "Buddy";
dog.age = 3;
dog.bark();
}
}
class Rectangle {
int length;
int width;
27
Java Labs Dr Benabderrezak
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Vehicle {
void start() {
System.out.println("Vehicle is starting.");
}
}
class BankAccount {
private double balance;
28
Java Labs Dr Benabderrezak
class Shape {
void draw() {
System.out.println("Drawing a shape.");
}
}
29
Java Labs Dr Benabderrezak
interface Drawable {
void draw();
}
class OuterClass {
private String outerField = "Outer field";
class InnerClass {
void display() {
System.out.println("Accessing: " + outerField);
}
}
}
30
Java Labs Dr Benabderrezak
class Counter {
static int count = 0;
Counter() {
count++;
}
import java.util.ArrayList;
class Employee {
private String name;
private ArrayList<Employee> subordinates;
31
Java Labs Dr Benabderrezak
manager1.addSubordinate(employee1);
manager1.addSubordinate(employee2);
ceo.addSubordinate(manager1);
ceo.addSubordinate(manager2);
System.out.println("Employee Hierarchy:");
ceo.displayHierarchy(0);
}
}
import java.util.ArrayList;
class Student {
private String name;
private int age;
class StudentManagement {
private ArrayList<Student> students;
public StudentManagement() {
students = new ArrayList<>();
}
32
Java Labs Dr Benabderrezak
System.out.println("Student List:");
management.displayStudents();
}
}
import java.util.HashMap;
class PhoneBook {
private HashMap<String, String> contacts;
public PhoneBook() {
contacts = new HashMap<>();
}
33
Java Labs Dr Benabderrezak
System.out.println("\nAll Contacts:");
phoneBook.displayAllContacts();
}
}
- You are tasked with developing a simplified social media management system for a Facebook-like
- The system should allow users to create, edit, delete, and view posts.
- The system should also allow users to manage their profiles and view their posts.
- The system should allow users to send friend requests, accept or reject requests
- Each user should also be able to manage their profile and view information about their friends.
- You are tasked with developing a family relationship management system that represents familial
- Each person in the family will be a node, and relationships (such as parent-child, sibling, and
- The system should allow users to add family members, define relationships, and query
34