0% found this document useful (0 votes)
36 views3 pages

Code 9 To 16

Uploaded by

nahnbunx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views3 pages

Code 9 To 16

Uploaded by

nahnbunx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

### Statement 9: Reverse an array of integer values

--- CODE START:


import java.util.Arrays;

public class ReverseArray {


public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
reverseArray(arr);
System.out.println("Reversed array: " + Arrays.toString(arr));
}

public static void reverseArray(int[] arr) {


int start = 0;
int end = arr.length - 1;
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
}
--- The END ---

### Statement 10: Find duplicate values in an array of integer values

--- CODE START:


import java.util.Arrays;

public class FindDuplicates {


public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 2, 5, 6, 3};
findDuplicates(arr);
}

public static void findDuplicates(int[] arr) {


Arrays.sort(arr);
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] == arr[i + 1]) {
System.out.println("Duplicate found: " + arr[i]);
}
}
}
}
--- The END ---

### Statement 11: Find common elements between two arrays (string values)

--- CODE START:


public class CommonElements {
public static void main(String[] args) {
String[] arr1 = {"apple", "banana", "orange"};
String[] arr2 = {"banana", "grapes", "orange"};
findCommonElements(arr1, arr2);
}
public static void findCommonElements(String[] arr1, String[] arr2) {
for (String s1 : arr1) {
for (String s2 : arr2) {
if (s1.equals(s2)) {
System.out.println("Common element found: " + s1);
}
}
}
}
}
--- The END ---

### Statement 12: Create a method to find the smallest number among three numbers

--- CODE START:


public class SmallestNumber {
public static void main(String[] args) {
int smallest = findSmallest(5, 3, 7);
System.out.println("Smallest number is: " + smallest);
}

public static int findSmallest(int a, int b, int c) {


return Math.min(Math.min(a, b), c);
}
}
--- The END ---

### Statement 13: Create a method that accepts three integers and returns true if
one is the middle point between the other two integers, otherwise false

--- CODE START:


public class MiddlePoint {
public static void main(String[] args) {
boolean isMid = isMiddlePoint(1, 4, 7);
System.out.println("Is middle point: " + isMid);
}

public static boolean isMiddlePoint(int a, int b, int c) {


return (b > a && b < c) || (b < a && b > c);
}
}
--- The END ---

### Statement 14: Create a method that accepts three integers and checks whether
they are consecutive or not. Returns true or false

--- CODE START:


public class ConsecutiveNumbers {
public static void main(String[] args) {
boolean areCons = areConsecutive(1, 2, 3);
System.out.println("Are consecutive: " + areCons);
}

public static boolean areConsecutive(int a, int b, int c) {


return Math.abs(a - b) == 1 && Math.abs(b - c) == 1;
}
}
--- The END ---
### Statement 15: Create a class called Employee with methods called work() and
getSalary(). Create a subclass called HRManager that overrides the work() method
and adds a new method called addEmployee()

--- CODE START:


public class Employee {
public void work() {
System.out.println("Employee is working.");
}

public double getSalary() {


return 50000.0; // Sample salary
}
}

class HRManager extends Employee {


@Override
public void work() {
System.out.println("HR Manager is managing employees.");
}

public void addEmployee() {


System.out.println("HR Manager is adding a new employee.");
}
}
--- The END ---

### Statement 16: Create a class called Shape with a method called getArea().
Create a subclass called Rectangle that overrides the getArea() method to calculate
the area of a rectangle

--- CODE START:


public class Shape {
public double getArea() {
return 0.0;
}
}

class Rectangle extends Shape {


private double length;
private double width;

public Rectangle(double length, double width) {


this.length = length;
this.width = width;
}

@Override
public double getArea() {
return length * width;
}
}
--- The END ---

Each program focuses on implementing one specific statement.

You might also like