Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

public class TwoStringVariables {

public static void main(String[] args) {


String name = "Jose", school="Jose Rizal University";

System.out.println(" Hello " + name + ",");


System.out.println(" Your School is " + school + ",");
System.out.println(" Bonifacio attended " + school + " too! ");
}
}

Hello World
public class Mico {

public static void main(String[] args) {

System.out.println("Hello World!");
}
}
MESSAGE
import javax.swing.JOptionPane;

public class VariableDeclaration {

public static void main(String args[])


{
JOptionPane.showMessageDialog(null,
"Hello,\nNicolle Plagere,\nthe one who create\nthis program is\nloving you
so much." );

System.exit(0);
}

}
public class VariableDeclaration{
public static void main(String args[]) {
char c='M';

byte numberByte = 1;
int numberInteger =1434414344;
short numberShort = 14344;
long numberLong = 1234567890;

float numberFloat = 1414;


double numberDouble = 3.1416;

boolean result = true;

String message = "Welcome to JRU!";

System.out.println("Character = " + c );

System.out.println("Byte = " + numberByte );


System.out.println("Integer = " + numberInteger );
System.out.println("Short = " + numberShort );
System.out.println("Long = " + numberLong );

System.out.println("Float = " + numberFloat );


System.out.println("Double = " + numberDouble );

System.out.println("Result = " + result );

System.out.println("Message = " + message );


}
}
public class SampleOne {

public static void main(String[] args) {

String status = "" ;

double examGrade = 80;

if(examGrade >= 60) {

status = "Passed";

}else {

status = "Failed";

System.out.println("status= "+ status);

status=(examGrade>=60) ? "Passed": "Failed";

System.out.println("status= "+ status);

}
1. Create a program that will input the following data: Employee Number, Employee LastName,
Employee FirstName, Rate per hour, Number of Hours Worked. Compute and output the Gross Pay, SSS
Deduction, Tax Withhold, Total Deduction, Net Pay. ( Use JOptionPane)

import javax.swing.JOptionPane;

public class InputJOptionPane {

public static void main(String args[]) {

/* GP = Gross Pay

SD = SSS Deduction

T = Tax Withhold

TD = Total Deduction

EN = Employee Number

ELN = Employee Last Name

EFN = Employee First Name

Rph/R = Rate per hour

NHW = Number of Hours Worked

NT = Net Pay

*/

String EN = JOptionPane.showInputDialog("Enter Employee Number: ");

String EFN = JOptionPane.showInputDialog("Enter Employee First Name: ");

String ELN = JOptionPane.showInputDialog("Enter Employee Last Name: ");

String Rph = JOptionPane.showInputDialog("Enter Employee Rate per hour: ");

float R= float.valueOf(Rph);

String NHW = JOptionPane.showInputDialog("Enter Number of Hours Worked: ");

float Nw= float.valueOf(NHW);

float GP = Nw * R;

double SD = GP * .05;

double T = GP * .10;

double TD = SD + T;

double NP = GP - TD;

JOptionPane.ShowMessageDialog(null,

"Employee Number: " + EN + "\nEmployee First Name: " + EFN + "/nEmployee Last Name: " + ELN +

"\nEmployee Rate per hour: " + R + "\nNumber of hours worked: " + Nw + "\nGross Pay: " + GP + "\
nSSS Deduction : " + SD +

"\nTax Withhold: " + T + "\nTotal Deduction: "+ TD);


}

}
2. This program computes the interest earned on 17,000 pesos with an interest
rate of .70 a year. The interest and the value of the investment after one year are
printed to standard output.

public class PrelimPT6 {

public static void main(String[] args) {

/* VI = Value of Investment
IR = Interest Rate
I = Interest earned in one year
VIOY = Value of Investment after One Year
*/

int VI = 17000;
double IR = .70;
double I;

I = VI * IR * 1;
double VIOY = VI*(1 + IR);

System.out.println("The Interest is: " + I);


System.out.println("The Value of Investment after one year is: " + VIOY);

3. Create a program that will input for the following data: (Using Scanner)
Student Name, Course
Grade in Quiz, Seatwork, Laboratory Exercises and Prelim
Compute and output the Prelim Grade.
Prelim Grade = Quiz * 25% + Seatwork * 20% + Lab. Exercise * 25% + Prelim Exam
* 30%

import java.util.Scanner;

public class Main


{

public static void main(String[] args)


{
Scanner keyboard = new Scanner( System.in );

System.out.println("Enter Student Name: ");


String SN = keyboard.nextLine();

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


String C = keyboard.nextLine();

System.out.println("Enter Quiz grade: ");


int Q = keyboard.nextInt();

System.out.println("Enter Seatwork grade: ");


int S = keyboard.nextInt();

System.out.println("Enter Laboratory Exercise grade: ");


int LE = keyboard.nextInt();

System.out.println("Enter Prelim Exam grade: ");


int PE = keyboard.nextInt();
double PG = (Q * 0.25) + (S * 0.20) + (LE * 0.25) + (PE * 0.30);

System.out.println("Your Prelim Grade is: " + PG);

if(PG >= 75){


System.out.println("Congratulations " + SN + " you passed!");

}else{
System.out.println("Sorry " + SN + " you failed.");
}
}
}

1. using BufferedReader Class


import java.io.*;
public class InputBufferedReader{
public static void main(String[] args) throws IOException
{
String mystr;

System.out.println("Getting Input Using BufferredReader Class");

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


BufferedReader dataIn = new BufferedReader (new
InputStreamReader(System.in));
mystr = dataIn.readLine();

System.out.println("Hello, " + mystr + ",");

System.out.println("What is your Favorite Food");


mystr = dataIn.readLine();

System.out.println("I Like " + mystr + " too ");

}
}

2. Use JOptionPane

import javax.swing.JOptionPane;
public class InputJOptionPane
{
public static void main (String[] args)
{

double pi = 3.151519;
String radius =JOptionPane.showInputDialog ("Enter the Circle's Radius:");
double r = Double.valueOf(radius); //Convets String to Double
double area = pi * r * r;
double circumference = 2 * pi * r;

JOptionPane.showMessageDialog(null, "The Circle's Radius is: " + radius + "\n


THE CIRCLE'S AREA IS: " + area
+ "\n THE CIRCLE'S CIRCUMFERENCE IS: " + circumference);
}
}

3. Use Scanner

import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner( System.in );

System.out.println("Getting Input Using Scanner");

System.out.println("What is Your Name?");


String mystr = keyboard.nextLine();
System.out.println("Hello, " + mystr + ".");

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


mystr = keyboard.nextLine();
System.out.println("I Like " + mystr + "too!");

}
}

BufferedReader

package com.runner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Eclipse {

public static void main(String[] args) throws NumberFormatException,


IOException {
// TODO Auto-generated method stub
BufferedReader dataIn = new BufferedReader(new
InputStreamReader(System.in));
int firstNum = 0;
int secondNum = 0;
int sum = 0;

System.out.println("Enter the first number: ");


firstNum = Integer.parseInt(dataIn.readLine());

System.out.println("Enter the second number: ");


secondNum = Integer.parseInt(dataIn.readLine());

sum = firstNum + secondNum;

System.out.println("The sum is: "+ sum);

}
Scanner
package com.runner;

import java.util.Scanner;
public class Eclipse {

public static void main(String[] args) {


// TODO Auto-generated method stub
Scanner dataIn = new Scanner(System.in);
int firstNum = 0;
int secondNum = 0;
int sum = 0;

System.out.println("Enter the first number: ");


firstNum = dataIn.nextInt();

System.out.println("Enter the second number: ");


secondNum = dataIn.nextInt();

sum = firstNum + secondNum;

System.out.println("The sum is: "+ sum);

Scanner
with decimal

package com.runner;
import java.util.Scanner;

public class Eclipse {

public static void main(String[] args) {


// TODO Auto-generated method stub
Scanner dataIn = new Scanner(System.in);
double firstNum = 0;
double secondNum = 0;
double sum = 0;

System.out.println("Enter the first number: ");


firstNum = dataIn.nextDouble();

System.out.println("Enter the second number: ");


secondNum = dataIn.nextDouble();

sum = firstNum + secondNum;

System.out.println("The sum is: "+ sum);

JOptionPane
Message

package com.runner;
import javax.swing.JOptionPane;

public class DialogBoxDemo {

public static void main(String[] args) {


// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null,
"Welcome\nto\nJava\nProgram");

JOptionPane
Message (double)

package com.runner;
import javax.swing.JOptionPane;

public class DialogBoxDemo {

public static void main(String[] args) {


// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null,
"Welcome\nto\nJava\nProgram");
double firstNum = 0;
double secondNum = 0;
double sum = 0;

firstNum =
Double.parseDouble(JOptionPane.showInputDialog("Enter a number"));
secondNum =
Double.parseDouble(JOptionPane.showInputDialog("Enter a number"));
sum = firstNum + secondNum;

JOptionPane.showMessageDialog(null,
"The sum is " + sum);

You might also like