Assignment 2 .

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Assignment2 Prog2 10Marks

WriteaJavaprogramcreateatwo-dimesionalarrayB[6][3]oftypedoubleinsidemainmethod.Assign
the valuesto thearray andapply thefollowing:
a) Printoutallelementsinsidethearray.
b) Printoutthesummationofelementsinthearray.
c) PrintoutthevalueofB[4][2]

public class Main {


public static void main(String[] args) {
double[][] B = new double[6][3];

// Assign values to the array


for (int i = 0; i < 6; i++) {
for (int j = 0; j < 3; j++) {
B[i][j] = (i + 1) * (j + 1);
}
}

// Print out all elements in the array


for (int i = 0; i < 6; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(B[i][j] + " ");
}
System.out.println();
}

// Print out the summation of elements in the array


double sum = 0;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 3; j++) {
sum += B[i][j];
}
}
System.out.println("Sum of all elements in the array: " + sum);

// Print out the value of B[4][2]


System.out.println("Value of B[4][2]: " + B[4][2]);
}
}
Writeajavaprogramtodothefollowing:
a) CreateanArrayListoftypeStringnamedascars;then,addtheelementsBbW,Audoi,ka
dillac, and Devroletinto the ArrayListcars.
b) Writestatementstoinsert"Fari"attheindex1.Thenprinttheindexof"Audoi".
c) Writestatementstodelete"kadillac"fromthelist.
d) Useforlooporforeachtodisplayallelementsfromthecarslist
e) Sortthelistthenprintitagainwithoutloop.

import java.util.ArrayList;
import java.util.Collections;

public class Main {


public static void main(String[] args) {
// Create an ArrayList of type String named as cars
ArrayList<String> cars = new ArrayList<>();

// Add elements to the cars list


cars.add("BbW");
cars.add("Audoi");
cars.add("kadillac");
cars.add("Devrolet");

// Insert "Fari" at index 1


cars.add(1, "Fari");

// Print the index of "Audoi"


System.out.println("Index of Audoi: " + cars.indexOf("Audoi"));

// Delete "kadillac" from the list


cars.remove("kadillac");

// Use for loop to display all elements from the cars list
System.out.println("Elements in the cars list: ");
for (String car : cars) {
System.out.println(car);
}

// Sort the list


Collections.sort(cars);

// Print the list again without loop


System.out.println("Sorted list: " + cars);
}
}
WriteaJavaprogramtocreateaclassnamedTestwiththefollowing:
a) Declareinstancevariables:ID,StuName,Mark
b) Declarethefollowing:
i) constructorwiththreeparameters
ii) show()–todisplaytheobjectvalues
class Test {
int ID;
String StuName;
double Mark;

// constructor with three parameters


public Test(int ID, String StuName, double Mark) {
this.ID = ID;
this.StuName = StuName;
this.Mark = Mark;
}

// show() method to display the object values


public void show() {
System.out.println("ID: " + ID);
System.out.println("Student Name: " + StuName);
System.out.println("Mark: " + Mark);
}
}

public class Main {


public static void main(String[] args) {
Test student = new Test(1, "Rahaf", 95.5);
student.show();
}
}

You might also like