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

JAVA

The document contains code examples demonstrating various Java concepts like variables, data types, user input, arithmetic operators, arrays, conditional statements, loops, methods, classes, objects, and constructors. It shows how to declare and initialize variables, take user input, perform calculations, access array elements, use if/else statements to evaluate conditions, iterate with for, while, and do-while loops, define and call methods, create classes with attributes and behaviors, and initialize objects using constructors.

Uploaded by

valmangalino15
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

JAVA

The document contains code examples demonstrating various Java concepts like variables, data types, user input, arithmetic operators, arrays, conditional statements, loops, methods, classes, objects, and constructors. It shows how to declare and initialize variables, take user input, perform calculations, access array elements, use if/else statements to evaluate conditions, iterate with for, while, and do-while loops, define and call methods, create classes with attributes and behaviors, and initialize objects using constructors.

Uploaded by

valmangalino15
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

Patrick Villanueva

// ADD import java.util.Scanner on each code;

1. Hello World

System.out.println("Hello");
System.out.println("The");
System.out.println("World");

2. Variable and data type

String name="patrick";
String bloodtype="O";
float gpa=1.5f;
int age=21;

System.out.println("I'm " + name + " and my blood type is " + bloodtype + "
and my GPA is " + gpa + ". My age is " + age);

3.User Input & Arithmetic Operators

float num1;
float num2;
float result;

System.out.print("Enter a number: ");


num1 = sc.nextFloat();

System.out.print("Enter a second number: ");


num2 = sc.nextFloat();

System.out.println();

System.out.println("Result " + num1 / num2);


4.Arrays | Elements | Index

String names[] = {"patrick","Mark","James","black","myNig"};

System.out.println(names[2]);

int random_Numbers[] = {34,53,6,778,123,56,89,23};


System.out.println(random_Numbers[4] + random_Numbers[5]);

String emails[]={"[email protected]","[email protected]","[email protected]"};
String userNames[]= {"patrick", "Mark", "myNig"};
String pass[] = {"patrick0000","mark123", "myNig456"};

System.out.println("Email : " + emails[2]);


System.out.println("UserName : " + userNames[2]);
System.out.println("Passwords : " + pass[2]);

5. Conditional Statements | IF-ELSE-ELSE IF | NESTED

System.out.print("English : ");
float english= sc.nextFloat();
System.out.print("Math: ");
float math = sc.nextFloat();
System.out.print("Science: ");
float science = sc.nextFloat();
System.out.print("Computer: ");
float computer = sc.nextFloat();

float average=(english+math+science+computer) / 4;
System.out.println(average);

if(average>100){
System.out.println("Invalid Input");
} else if(average >= 98){
System.out.println("With highest honor");
} else if(average >= 95){
System.out.println("With high honor");
}else if(average>=90){
System.out.println("With honor");
}else if (average>=75){
System.out.println("Passed");
} else if(average<75){
System.out.println("Failed");
}
6.Switch Statements | CASE

String month;

System.out.print("Enter your birth month: ");


month = sc.next();

switch (month) {
case "january" -> System.out.println("January");
case "february" -> System.out.println("February");
case "march" -> System.out.println("march");
case "april" -> System.out.println("April");
case "May" -> System.out.println("May");
case "June" -> System.out.println("June");
case "july" -> System.out.println("July");
case "august" -> System.out.println("August");
case "september" -> System.out.println("September");
case "october" -> System.out.println("October");
case "november" -> System.out.println("November");
case "december" -> System.out.println("December");
}

7. While Do while loop

int tries = 3;
String answer;

while(tries > 0) {
System.out.println("What is a cool car? ");
System.out.print("Answer: ");
answer = sc.next();

if(answer.equalsIgnoreCase("supra")) {
System.out.println("Nice taste");
break;
}
else
tries--;

------------------------------------------------

int tries = 3;
String answer;

do{
System.out.println("What is a cool car? ");
System.out.print("Answer: ");
answer = sc.next();

if(answer.equalsIgnoreCase("supra")) {
System.out.println("You got it! ");
break;
}
else
tries--;

} while (true);

8. For loop

// Scanner for user input


Scanner scanner = new Scanner(System.in);

// Creating a string array


String[] fruits = {"Apple", "Banana", "Orange", "Grapes", "Mango"};
// Using a for loop to print elements from the array
for (int i = 0; i < fruits.length; i++) {
System.out.println("Fruit #" + (i + 1) + ": " + fruits[i]);
}

9.FOR EACH LOOP

// Initializing an array of strings


String[][] userCredentials = {
{"user123", "pass456"},
{"johnDoe", "secret123"},
{"aliceSmith", "p@ssw0rd"}
};

// Scanner for user input


Scanner scanner = new Scanner(System.in);

// Prompting the user for input


System.out.print("Enter your username: ");
String enteredUsername = scanner.nextLine();

System.out.print("Enter your password: ");


String enteredPassword = scanner.nextLine();

// Checking if the entered values match any entry in the array


boolean isAuthenticated = false;
for (String[] credentials : userCredentials) {
String username = credentials[0];
String password = credentials[1];

if (enteredUsername.equals(username) &&
enteredPassword.equals(password)) {
isAuthenticated = true;
break;
}
}

// Displaying authentication result


if (isAuthenticated) {
System.out.println("Authentication successful. Welcome, " +
enteredUsername + "!");
} else {
System.out.println("Authentication failed. Invalid username or
password.");
}

// Close the scanner


scanner.close();
10.2D ARRAY + NESTED FOR LOOP

// Scanner for user input


Scanner scanner = new Scanner(System.in);

// Prompting the user for the dimensions of the 2D array


System.out.print("Enter the number of rows: ");
int rows = scanner.nextInt();

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


int columns = scanner.nextInt();

// Creating a 2D array
String[][] grid = new String[rows][columns];

// Taking user input for each element in the grid


for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print("Enter element at position [" + i + "][" + j + "]:
");
grid[i][j] = scanner.next();
}
}

// Using nested loops to print the elements of the 2D array


System.out.println("Entered elements:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(grid[i][j] + " ");
}
System.out.println(); // Move to the next line after each row
}

// Close the scanner


scanner.close();

11. METHOD

// Create a Scanner object to read input from the user


Scanner scanner = new Scanner(System.in);

// Get user input for the first number


System.out.print("Enter the first number: ");
int num1 = scanner.nextInt();
// Get user input for the second number
System.out.print("Enter the second number: ");
int num2 = scanner.nextInt();

// Call the addNumbers method with user input and print the result
int result = addNumbers(num1, num2);
System.out.println("The sum is: " + result);

// Close the Scanner to prevent resource leaks


scanner.close();
}

// Method to add two numbers


public static int addNumbers(int num1, int num2) {
int sum = num1 + num2;
return sum;

12.

nums a = new nums();

int addition = nums.add(10,5);


System.out.println(addition);

-----class section----
static int add(int num1, int num2){
int addition = num1 + num2;
return addition;

13. Classes & Objects

nums cars1 = new nums("Toyota","Supra",1978);


nums cars2 = new nums("Aston Martin", "Valhalla",2024);

cars1.car1();
cars2.car2();

------class section---------

String company;
String model;
int year;
//constractor

public nums(String company, String model, int year){

this.company=company;
this.model=model;
this.year=year;

public void car1(){


System.out.println("Company : " + company);
System.out.println("Model: " + model);
System.out.println("Year: " + year);
}
public void car2(){
System.out.println("Company : " + company);
System.out.println("Model: " + model);
System.out.println("Year: " + year);
}

14. Constructors

nums cars1 = new nums("Toyota","Supra",1978);


nums cars2 = new nums("Aston Martin", "Valhalla",2024);

cars1.car1();
cars2.car2();

------class section---------

String company;
String model;
int year;

//constractor

public nums(String company, String model, int year){

this.company=company;
this.model=model;
this.year=year;

public void car1(){


System.out.println("Company : " + company);
System.out.println("Model: " + model);
System.out.println("Year: " + year);
}
public void car2(){
System.out.println("Company : " + company);
System.out.println("Model: " + model);
System.out.println("Year: " + year);
}

You might also like