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

Programming Activity 1-6

The document contains 6 coding activities that demonstrate different Java programming concepts: 1. A simple Hello World program that prints a greeting. 2. A program that takes user input of name and age and prints it back. 3. A program that allows a user to choose an arithmetic operation and performs calculations on input numbers. 4. A program that prints shapes like rectangles and triangles based on user-specified rows and columns. 5. A program that demonstrates operations on ArrayList like adding, removing, selecting, and printing minimum, maximum, average and total of elements. 6. A program that takes a name as input and prints it paired with other names from a 2D array.

Uploaded by

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

Programming Activity 1-6

The document contains 6 coding activities that demonstrate different Java programming concepts: 1. A simple Hello World program that prints a greeting. 2. A program that takes user input of name and age and prints it back. 3. A program that allows a user to choose an arithmetic operation and performs calculations on input numbers. 4. A program that prints shapes like rectangles and triangles based on user-specified rows and columns. 5. A program that demonstrates operations on ArrayList like adding, removing, selecting, and printing minimum, maximum, average and total of elements. 6. A program that takes a name as input and prints it paired with other names from a 2D array.

Uploaded by

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

ACTIVITY 1

CODE:

public class MyClass {

public static void main(String args[]) {

System.out.println(Hello ("CLASSMATE"));

public static String Hello (String what){

return "Hello" + what + "!";

}
ACTIVITY 2

CODE:

import java.util.Scanner;

public class Main

public static void main(String[] args)

String name;

int age;

Scanner scanner = new Scanner(System.in);

System.out.println("What is your name? ");

name = scanner.nextLine();

System.out.print("How old are you? ");

age = scanner.nextInt();

System.out.println("My name is " + name);

System.out.println("I am " + age + " years old");

}
ACTIVITY 3

CODE:

import java.util.Scanner;

public class Main

public static void method1() {

Scanner scan = new Scanner(System.in);

float firstNumber, secondNumber;

float sum, subtract, product, remainder;

System.out.print("-----Pick an arithmetic Type:----- \n \n(+)ADDITION\n(-


)SUBTRACTION\n(x)MULTIPLICATION\n(%)MODULUS\n----------------------------------\n");

String name = scan.nextLine();

if (name.equals("+"))

System.out.print("\nEnter First Number: ");

firstNumber = scan.nextFloat ();

System.out.print("\nEnter Second Number: ");

secondNumber = scan.nextFloat ();


sum = firstNumber + secondNumber;

System.out.print("\nSum of the two numbers is: "+sum);

else if (name.equals("-"))

System.out.print("\nEnter First Number: ");

firstNumber = scan.nextFloat ();

System.out.print("\nEnter Second Number: ");

secondNumber = scan.nextFloat ();

subtract = firstNumber - secondNumber;

System.out.print("\nSubtraction of the two numbers is: "+subtract);

else if (name.equals("x"))

System.out.print("\nEnter First Number: ");

firstNumber = scan.nextFloat ();

System.out.print("\nEnter Second Number: ");

secondNumber = scan.nextFloat ();

product = firstNumber * secondNumber;


System.out.print("\nMultiplication of the two numbers is: "+product);

else if (name.equals("%"))

System.out.print("\nEnter First Number: ");

firstNumber = scan.nextFloat ();

System.out.print("\nEnter Second Number: ");

secondNumber = scan.nextFloat ();

remainder = firstNumber % secondNumber;

System.out.print("\nRemainder of the two numbers is: "+remainder);

public static void main(String[] args){

method1();

}
ACTIVITY 4

CODE:

import java.util.Scanner;

public class Main

public static void main(String[] args)

Scanner input = new Scanner(System.in);

int row, column, shp, repeat;

do{

do{

System.out.println("What shape do you want to print?");

System.out.println("Rectangle (1) or Right Triangle (2)?");

shp = input.nextInt();

if(shp>2){

System.out.println("Out of Bound!");

}while(shp>2);

do{

System.out.println("How many rows?");

row = input.nextInt();

if(row>30)

{
System.out.println("Error: Maximum Rows is 30.");

}while(row>30);

do{

System.out.println("How many columns?");

column = input.nextInt();

if(column>30)

System.out.println("Error: Maximum Columns is 30.");

}while(column>30);

if(shp==1)

for(int i = 0; i < row; i++)

for(int j = 0; j < column; j++)

System.out.print("^");

System.out.println("");

else if(shp==2)

for(int k = 0; k < row;k++)

for(int l = -1; l < k; l++)

{
System.out.print("^");

System.out.println("");

else

System.out.println("Out of Bound!");

System.out.println("Do you want to repeat? Yes (1) or No (0)");

repeat = input.nextInt();

}while(repeat==1);

}
ACTIVITY 5

CODE:

import java.util.ArrayList;

import java.util.Scanner;

public class Arrays {

static Scanner input = new Scanner(System.in);

static ArrayList<Integer> numlist = new ArrayList();

static int rep, opt, rem, obt, a, obj, total, ave, mini, maxi, niw;

public static void main(String[] args) {

do {

System.out.println("What do you want to do?");

System.out.println("[1] Show Stored Data?");

System.out.println("[2]Add Data?");

System.out.println("[3]Delete Stored Data?");

System.out.println("[4]Select and Change Data?");

System.out.println("[5]Print Minimum Data?");

System.out.println("[6]Print Maximum Data?");

System.out.println("[7]Print Average of all Data?");

System.out.println("[8]Print Total of all Data?");

System.out.println("[9]Clear Data?");
opt = input.nextInt();

switch (opt) {

case 1:

System.out.println(numlist);

case 2: {

do {

add();

} while (rep == 1 && numlist.size() < 25);

System.out.println(numlist);

case 3: {

do {

del();

} while (rep == 1);

case 4: {

do {

sel();

} while (rep == 1);

case 5: {

min();

case 6: {

max();

case 7: {
ave();

case 8: {

tot();

case 9: {

clr();

default :

System.out.println("Out of Bound!");

System.out.println("Do you want to show menu? Yes[1] or No[2]");

rep = input.nextInt();

} while (rep == 1);

public static void add() {

System.out.println("Enter Value:");

a = input.nextInt();

numlist.add(a);

System.out.println("Updated List:" + numlist);

System.out.println("Do you want to add more? Yes[1] or No[2]");

rep = input.nextInt();

public static void del() {

System.out.println("Select the element you wish to remove starting from 0:");

System.out.println(numlist);
rep = input.nextInt();

numlist.remove(rem);

System.out.println("Updated List:" + numlist);

System.out.println("Do you want to remove more? Yes[1] or No[2]");

rep = input.nextInt();

numlist.remove(rem);

public static void sel() {

System.out.println("Do you want to show menu? Yes[1] or No[2]");

obj = input.nextInt();

while (rep == 1);

System.out.println(obj);

System.out.println("What will be changed?");

niw = input.nextInt();

numlist.set(obt, niw);

System.out.println("Updated List:" + numlist);

System.out.println("Do you want to select again? Yes[1] or No[2]");

rep = input.nextInt();

public static void min() {

for(int i = 0; i < numlist.size(); i++){

if(numlist.get(i) < numlist.get(0))

mini = numlist.get(i);

}System.out.println(mini);

}
public static void max() {

for(int i = 0; i < numlist.size(); i++){

if(numlist.get(i) > numlist.get(0))

maxi = numlist.get(i);

}System.out.println(maxi);

public static void ave() {

for (int i = 0; i < numlist.size(); i++) {

total += numlist.get(i);

ave = total/numlist.size();

}System.out.println(ave);

public static void tot() {

for (int i = 0; i < numlist.size(); i++) {

total += numlist.get(i);

}System.out.println(total);

public static void clr() {

numlist.clear();

}
ACTIVITY 6

CODE:

import java.util.Scanner;

public class Main{

static String firstname [][]= { {"Althea","Samantha","Angel"}, {"Angela", "Princess", "Sophia"},

{"Sopia","Andrea", "Nathalie"} };

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Althea, Samantha, Angel, Angela, Princess, Sophia, Sopia, Andrea, Nathalie");

System.out.println("\n ---Choose a NAME above---");

String name3 = scanner.nextLine();

System.out.println("");

for (int name1 = 0; name1 < 3; name1++) {

for (int name2 = 0; name2 < 3; name2++) {

if (!firstname[name1][name2].equals(name3)) {

System.out.println(name3 + " " + firstname[name1][name2]);

}
}

You might also like