0% found this document useful (0 votes)
2 views

SLIP 2

The document contains two Java programs. The first program accepts 'n' integers from the user, stores them in an ArrayList, and displays the elements. The second program defines a class `MyNumber` that includes methods to check if a number is negative, positive, odd, or even, and demonstrates its functionality using command-line arguments.

Uploaded by

gaiwaltushar9
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)
2 views

SLIP 2

The document contains two Java programs. The first program accepts 'n' integers from the user, stores them in an ArrayList, and displays the elements. The second program defines a class `MyNumber` that includes methods to check if a number is negative, positive, odd, or even, and demonstrates its functionality using command-line arguments.

Uploaded by

gaiwaltushar9
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/ 2

### Q1: Write a program to accept ‘n’ integers from the user & store them in an

ArrayList collection. Display the elements of the ArrayList.

Here's a simple Java program for this task:

```java
import java.util.ArrayList;
import java.util.Scanner;

public class IntegerArrayList {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Integer> numbers = new ArrayList<>();

System.out.print("Enter the number of integers: ");


int n = scanner.nextInt();

System.out.println("Enter " + n + " integers:");


for (int i = 0; i < n; i++) {
numbers.add(scanner.nextInt());
}

System.out.println("The elements of the ArrayList are:");


for (int number : numbers) {
System.out.println(number);
}

scanner.close();
}
}

Q2 Option 1: Define a class `MyNumber` with methods to check if the


number is negative, positive, odd, or even.

Here’s a solution:

```java
public class MyNumber {
private int number;

// Default constructor initializing to 0


public MyNumber() {
this.number = 0;
}

// Constructor to initialize with a specific value


public MyNumber(int number) {
this.number = number;
}

// Method to check if the number is negative


public boolean isNegative() {
return number < 0;
}

// Method to check if the number is positive


public boolean isPositive() {
return number > 0;
}
// Method to check if the number is odd
public boolean isOdd() {
return number % 2 != 0;
}

// Method to check if the number is even


public boolean isEven() {
return number % 2 == 0;
}

public static void main(String[] args) {


// Check if command-line argument is provided
if (args.length > 0) {
int num = Integer.parseInt(args[0]);
MyNumber myNumber = new MyNumber(num);

System.out.println("Number: " + num);


System.out.println("Is Negative: " + myNumber.isNegative());
System.out.println("Is Positive: " + myNumber.isPositive());
System.out.println("Is Odd: " + myNumber.isOdd());
System.out.println("Is Even: " + myNumber.isEven());
} else {
System.out.println("Please provide a number as a command-line
argument.");
}
}
}

You might also like