Java Basic Activities : Do While Loop
=====================
Code:
=====================
import java.util.Scanner;
public class Test36_Pereda_dowhileloop {
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
int i =1, nums;
do {
i++;
System.out.print("Enter a number: ");
nums = input.nextInt();
System.out.println("The number you entered: " +nums);
}while(i <=5);
input.close();
}
}
Java Basic Activities: While Loop
================
Code:
==================
import java.util.Scanner;
public class Test35_Pereda_whileloop {
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
int i =1, nums;
while(i <=5) {
i++;
System.out.print("Enter a number: ");
nums = input.nextInt();
System.out.println("The number you entered: " +nums);
}
input.close();
}
}
Java Basic Activities: For Loop
======================
Code:
=======================
import java.util.Scanner;
public class Test34_Pereda_forloop {
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
int i, nums;
for(i=1; i<=5; i++){
System.out.print("Enter a number: ");
nums = input.nextInt();
System.out.println("The number you entered: " +nums);
}
input.close();
}
}
Java Basic Activities: Logical Operators w/ nested else if
=========================
Code:
===========================
import java.util.Scanner;
public class Test32_Pereda_Logicalland {
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
int age, num1;
System.out.print("Enter a number: ");
num1 = input.nextInt();
System.out.print("enter your age: ");
age = input.nextInt();
if (num1 <100 && age> 60) {
System.out.println("Congratulation you win a lucky prize!");
}
else {
System.out.println("Sorry , did not win any prize!");
}
input.close();
}
}
=====================================
========================
Code:
========================
import java.util.Scanner;
public class Test33_Pereda_Challege {
public static void main(String [] args) {
System.out.println("=================================================================");
System.out.println("| Name:Sonny Pereda |");
System.out.println("| Subject Code: CC101 |");
System.out.println("| Subject Title: Programming Language |");
System.out.println("| Activity no.33 |");
System.out.println("| Instructor: Clinton Reducto |");
System.out.println("=================================================================");
Scanner input = new Scanner(System.in);
int num1;
System.out.println("\n\nEnter your grade in CC101:");
num1 = input.nextInt();
if (num1<=100 && num1>=98 ) {
System.out.println("Grade Point Equivalent: 1.0 ");
System.out.println("Exellent");
}
else if (num1<=97 && num1>=95 ) {
System.out.println("Grade Point Equivalent: 1.25 ");
System.out.println("Very good");
}
else if (num1<=94 && num1 >=92 ) {
System.out.println("Grade Point Equivalent: 1.75 ");
System.out.println("Execent");
}
else if (num1<=91 && num1 >=89 ) {
System.out.println("Grade Point Equivalent: 2.0 ");
System.out.println("Very good");
}
else if (num1<=88 && num1 >=86 ) {
System.out.println("Grade Point Equivalent: 2.25 ");
System.out.println("Very good");
}
else if (num1<=85 && num1 >=83 ) {
System.out.println("Grade Point Equivalent: 2.5 ");
System.out.println("Fair");
}
else if (num1<=80 && num1 >=82 ) {
System.out.println("Grade Point Equivalent: 2.75 ");
System.out.println("Very good");
}
else if (num1<=79 && num1 >=77 ) {
System.out.println("Grade Point Equivalent: 3.0 ");
System.out.println("Passed");
}
else if (num1<=76 && num1 >=75 ) {
System.out.println("Grade Point Equivalent: 3.0 ");
System.out.println("Failed");
}
else {System.out.println("Your faile");
}
input.close();
}
}
Java Basic Activities: Try Catch w/ nested else if statement | Invalid Character
=============================
Code:
==============================
import java.util.Scanner;
public class Test31_Pereda_Challege {
public static void main(String [] args) {
int pvail;
Scanner input = new Scanner(System.in);
try {
System.out.println("Enter a number:");
pvail = input.nextInt();
if (pvail <= -1) {
System.out.println("The number you entered is negative");
}
else if (pvail >= 1) {
System.out.println("The number you entered is positive");
}
else if (pvail == 0) {
System.out.println("The number you entered is zero");
}
} catch (Exception e) {
System.out.println("Character are not allowed!!!!");
input.close();
}
}
}
========================================================
Java Basic Activities: If else and nested else if statements
==========================
Code:
===========================
public class Test27_Pereda_ifelse {
public static void main(String [] args) {
int m=100, n=100;
if (m==n ) {
System.out.println("m is equal to n");
}
else {
System.out.println("m is not equal to n");
}
}
}
================================
===========================
Code:
==============================
import java.util.Scanner;
public class Test28_Pereda_ifelse2 {
public static void main(String [] args) {
int pvail;
Scanner input = new Scanner(System.in);
System.out.println("Enter a number from 1 to 3: ");
pvail = input.nextInt ();
if (pvail ==2) {
System.out.println("Congratualions!!! You avail the promo ");
}
else {
System.out.println("Thank you for joining");
}
input.close();
}
}
===========================================================
======================
Code:
=======================
import java.util.Scanner;
public class Test29_Pereda_Challege {
public static void main(String [] args) {
int pvail;
Scanner input = new Scanner(System.in);
System.out.println("Enter your age: ");
pvail = input.nextInt ();
if (pvail >=18) {
System.out.println("You are eligible to vote! ");
}
else {
System.out.println("Not eligible to vote!");
input.close();
}
}
}
============================
Code:
==============================
import java.util.Scanner;
public class Test30_Pereda_nestedifelse {
public static void main(String [] args) {
int pvail;
Scanner input = new Scanner (System.in);
System.out.println("Enter a number from 1 to 4: ");
pvail = input.nextInt();
if (pvail == 1) {
System.out.println("Congratulation!!!! Your Discount: 10%");
}
else if (pvail == 2) {
System.out.println("Congratulation!!!! Your Discount: 50%");
}
else if (pvail == 3) {
System.out.println("Sorry you didn't avail the Discount");
}
else if (pvail == 4) {
System.out.println("Congratulation!!!! Your Discount: 20%");
}
else {
System.out.println("The number you entered is invalid. Enter a number from 1 to 4");
}
input.close();
}
}
===================================
Java Basic Activities: Assignment Operators
======================
Code:
======================
public class Test25_Pereda_Challege {
public static void main(String [] args) {
int a = 5, b = 5;
a +=b;
System.out.println("The sum of the first and second:" + a);
a -=b;
System.out.println("The sum of the first and second:" + a);
a *=b;
System.out.println("The sum of the first and second:" + a);
a /=b;
System.out.println("The sum of the first and second:" + a);
}
}
==============================================
Sample no. 2
=======================
Code:
=========================
public class Test26_Pereda_Assignment {
public static void main(String [] args) {
int a = 5, c;
c = a;
System.out.println("Variable C is equal to :" + c);
c += a;
System.out.println("Variable C is equal to :" + c);
c -= a;
System.out.println("Variable C is equal to :" + c);
c *= a;
System.out.println("Variable C is equal to :" + c);
c /= a;
System.out.println("Variable C is equal to :" + c);
}
}
Java Basic Activities: Increment and Decrement Operators
=====================
Code:
=======================
public class Test23_Pereda_increment {
public static void main(String [] args) {
int x= 1, y = 1;
++x;
++y;
System.out.println("Value of x: " +x);
System.out.println("Value of x: " + ++x);
System.out.println("Value of x: " + ++x);
System.out.println("\n\nValue of y: " +y);
System.out.println("Value of x: " + ++y);
System.out.println("Value of x: " + ++y);
}
}
==================================
Sample no. 2
=======================
Code:
======================
public class Test24_Pereda_Challege {
public static void main(String [] args) {
int x= 1, y = 1;
++x;
++y;
System.out.println("Value of x: " +x);
System.out.println("Value of x: " + ++x);
System.out.println("Value of x: " + ++x);
System.out.println("\n\n\nValue of y: " +y);
System.out.println("Value of x: " + ++y);
System.out.println("Value of x: " + ++y);
System.out.println("\n\n\nValue of x: " + --x);
System.out.println("Value of y: " + --y);
}
}
===========================================
Java Basic Activities: Getting the user's input
===============================
Code:
===============================
import java.util.Scanner;
public class Test17_Pereda_Scanner {
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
String name1;
String name2;
System.out.println("Enter Your Full Name:");
name1 = input.nextLine();
System.out.println("What is Your Last Name:");
name2 = input.nextLine();
System.out.println("\n\nYour full name:" +name1);
System.out.println("\n\n Your last name:" +name2);
input.close();
}
}
============================================
Get the user's integer input
import java.util.Scanner;
public class Test18_Pereda_Scannerint {
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
int num1, num2;
System.out.println("Enter two intergers:");
num1 = input.nextInt();
num2 = input.nextInt();
System.out.println("The number you entered: " + num1 + " and " +num2);
input.close();
}
}
==================================================================
Get the user's integer input w/ double and float data type
==========================
Code
==========================
import java.util.Scanner;
public class Test19_Pereda_Scannerfloat {
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
float num1;
double num2;
System.out.println("Enter num1:");
num1 = input.nextFloat();
System.out.println("Enter num2:");
num2 = input.nextDouble();
System.out.println("\n\nThe number you entered: " + num1 + " and " +num2);
input.close();
}
}
=====================================================
Get the user's string input
======================================================
import java.util.Scanner;
public class Test20_Pereda_Challenge {
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
String name1;
String name2;
String name3;
String name4;
String name5;
String name6;
String name7;
String name8;
String name9;
String name10;
System.out.println("Enter your Firstname:");
name1 = input.nextLine();
System.out.println("Enter your Lastname:");
name2 = input.nextLine();
System.out.println("Enter your age:");
name3 = input.nextLine();
System.out.println("Enter your birthdate:");
name4 = input.nextLine();
System.out.println("Enter your address:");
name5 = input.nextLine();
System.out.println("Enter your course:");
name6 = input.nextLine();
System.out.println("Enter your Year and Section:");
name7 = input.nextLine();
System.out.println("Enter the Subject Code:");
name8 = input.nextLine();
System.out.println("Enter the Subject Title:");
name9 = input.nextLine();
System.out.println("Enter the fullname of your Instructor in java:");
name10 = input.nextLine();
System.out.println("=========================");
System.out.println("Firstname:" +name1);
System.out.println("Lastname:" +name2);
System.out.println("Age:" +name3);
System.out.println("Birthdate:" +name4);
System.out.println("Address:" +name5);
System.out.println("Course:" +name6);
System.out.println("Year and Section:" +name7);
System.out.println("Subject Code:" +name8);
System.out.println("Subject Title:" +name9);
System.out.println("Name of Instructor in java:" +name10);
System.out.println("=======================");
input.close();
}
}
=====================================================
Get the user's integer input and display the sum
=======================================================
=============================
Code:
====================
import java.util.Scanner;
public class Test21_Pereda_operator {
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
int num1, num2, sum;
System.out.println("Enter First num: ");
num1 = input.nextInt();
System.out.println("Enter Second num :");
num2 = input.nextInt();
sum = num1 + num2;
System.out.println("\n\n\nThe sum of the first and 2nd numbers:" + sum);
input.close();
}
}
==============================================
Get the user's integer input and display the sum , difference, product and quotient of the two integers
=====================
Code:
=====================
import java.util.Scanner;
public class Test22_Pereda_Challenge {
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
int num1, num2, sum, diff, product, qou;
System.out.println("Enter First num: ");
num1 = input.nextInt();
System.out.println("Enter Second num :");
num2 = input.nextInt();
sum = num1 + num2;
diff = num1 - num2;
product = num1 * num2;
qou = num1 / num2;
System.out.println("\n\nThe sum of the first and 2nd numbers:" + sum);
System.out.println("The difference of the first and 2nd numbers:" + diff);
System.out.println("The product of the first and 2nd numbers:" + product);
System.out.println("The quotient of the first and 2nd numbers:" + qou);
input.close();
}
}
==============================================
Java Basic Activities: Int, float and Double Data type
public class Test13_Pereda_int {
public static void main(String [] args) {
int num1,num2;
num1 = 5;
num2 = 10;
System.out.println("Variable num1 is equal to " +num1);
System.out.println("Variable num1 is equal to " +num2);
}
}
===================================================================
Float and Double Data type
public class Test14_Pereda_doublefloat {
public static void main(String [] args) {
float num1 = 87.45f;
double num2 = 97.67;
System.out.println("Variable Num1: " + num1);
System.out.println("\n\nVariable Num1: " + num2);
}
}
============================================
Long Data Type
===========================================
public class Test15_Pereda_long {
public static void main(String [] args) {
long num1 = 4353454443434233432L;
System.out.println("Value of Num1: " + num1);
}
}
========================================
Boolean Data type
========================================
public class Test16_Pereda_boolean {
public static void main(String [] args) {
boolean x = true;
boolean y = false;
System.out.println("Value of x: " +x);
System.out.println("\n\nValue of y: " +y);
}
}
===========================================
Java Basic Activities: String Data Type
Display the value of a variable w/ String Data Type
=============================
Code:
=================================
public class Test10_Pereda {
public static void main(String [] args) {
String Fname = "Sonny";
String Lname = "Pereda";
System.out.println("The value of the variable Fname: " +Fname );
System.out.println("The value of the variable Lname: " +Lname );
}
}
========================================
Sample no. 2
public class Test11_Pereda {
public static void main(String [] args) {
String Fname, Lname;
Fname = "Sonny";
Lname = "Pereda";
System.out.println (Fname);
System.out.println (Lname);
}
}
======================================
Sample no. 3
public class Test12_Pereda {
public static void main(String [] args) {
String Fname,Lname,Age,Birthdate,Address,Course,Year,Code,Title,Instructor;
Fname = "Sonny";
Lname = "Pereda";
Age = "27";
Birthdate = "July 13, 1991";
Address = "Dayao, Roxas City";
Course = "BSCS";
Year = "1A";
Code = "CC102";
Title = "Programming Language 1 (Java)";
Instructor = "Sir Clinton U. Reducto";
System.out.println("Firstname: " +Fname );
System.out.println("Lastname: " +Lname );
System.out.println("Birthdate: " +Birthdate );
System.out.println("Age: " +Age );
System.out.println("Address: " +Address );
System.out.println("Course: " +Course );
System.out.println("Year & Section: " +Year );
System.out.println("Subject Code: " +Code );
System.out.println("Subject Title: " +Title );
System.out.println("Instructor: " +Instructor );
}
}
==============================================
Mag-subscribe sa:
Mga Post (Atom)