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

Document (8)

Tr

Uploaded by

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

Document (8)

Tr

Uploaded by

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

import java.util.

Scanner;

public class ArrayExample {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Prompt user to enter the size of the arrays

System.out.print("Enter the size of the single-dimensional array: ");

int size1D = scanner.nextInt();

System.out.print("Enter the number of rows for the two-dimensional


array: ");

int rows = scanner.nextInt();

System.out.print("Enter the number of columns for the two-dimensional


array: ");

int columns = scanner.nextInt();

// Create single-dimensional array

int[] array1D = new int[size1D];

// Initialize single-dimensional array with sample values

for (int i = 0; i < size1D; i++) {

array1D[i] = i + 1; // Sample initialization: 1, 2, 3, ..., size1D

// Print single-dimensional array


System.out.println("\nSingle-dimensional array:");

for (int i = 0; i < size1D; i++) {

System.out.print(array1D[i] + " ");

// Create two-dimensional array

int[][] array2D = new int[rows][columns];

// Initialize two-dimensional array with sample values

int value = 1;

for (int i = 0; i < rows; i++) {

for (int j = 0; j < columns; j++) {

array2D[i][j] = value; // Sample initialization: 1, 2, 3, ...

value++;

// Print two-dimensional array

System.out.println("\n\nTwo-dimensional array:");

for (int i = 0; i < rows; i++) {

for (int j = 0; j < columns; j++) {

System.out.print(array2D[i][j] + " ");

System.out.println();

scanner.close();
}

class Lamp {

// stores the value for light

// true if light is on

// false if light is off

boolean isOn;

// method to turn on the light

void turnOn() {

isOn = true;

System.out.println("Light on? " + isOn);

// method to turnoff the light

void turnOff() {

isOn = false;

System.out.println("Light on? " + isOn);

class Main {

public static void main(String[] args) {


// create objects led and halogen

Lamp led = new Lamp();

Lamp halogen = new Lamp();

// turn on the light by

// calling method turnOn()

led.turnOn();

// turn off the light by

// calling method turnOff()

halogen.turnOff();

You might also like