You are on page 1of 20

Basic Java Programs

public class CharLesson


{
public static void main(String[] args)
{
char ch = 'a';
System.out.print("The Character is: " + ch);
}
}

Output: The Character is: a


public class UserInputChar
{
public static void main(String[] args) throws Exception
{
char ch;
int i;
System.out.print("Enter a Character: ");
ch = (char)System.in.read();
//i = System.in.read();
i = (int)ch;
System.out.println("The Character you entered is: " + ch);
System.out.println("The Integer Value for char: " + i);
}
}
Output: Enter a Character: a

The Character you entered is: a


The Integer Value for char: 97
public class StringLesson
{
public static void main(String[] args)
{
String str = “Java Standard Edition";
System.out.print("The String is: " + str);
}
}

Output : The String is: Java Standard Edition


import java.util.Scanner;
public class UserInputExample
{
public static void main(String[] args)
{
String str;
int num;
Scanner input = new Scanner(System.in);
System.out.print(“Enter String: “);
str = input.nextLine( );
System.out.print(“Enter Number: “);
num = input.nextInt( );
System.out.print("Your String is: " + str);
System.out.print("Your Number is:"+num);
}
}
import java.util.Scanner;
public class UserInputString
{
public static void main(String[] args)
{
String str;
Scanner input = new Scanner(System.in);
System.out.print("Enter a String: ");
str = input.nextLine();
System.out.print("Your String is: " + str);
}
}
Output: Enter a String: Java

Your String is: Java


import java.util.Scanner;
public class GetUserInfo
{
public static void main(String[] args)
{
String name;
int age = 32;
Scanner input = new Scanner(System.in);
System.out.print("Enter your name: ");
name = input.nextLine();
System.out.print("Enter your age: ");
age = input.nextInt();
System.out.print("Your Name is: " + name + " and you are "
+ age + " years old.");
}
}
Output: Enter your name: Kay Kay
Enter your age: 21
Your Name is: Kay Kay and you are 21 years old.
public class AddTwoNumber
{
public static void main(String[] args)
{
int num1; //declare variable
num1 = 528; //initialize
int num2 = 1500;//declare & initialize
System.out.println("Num1 is: " + num1);
System.out.println("Num2 is: " + num2);
System.out.println("\nThe Sum is: " + (num1 + num2));
}
}
Output: Num1 is: 528
Num2 is: 1500

The Sum is: 2028


import java.util.Scanner;
public class UserInputAdd
{
public static void main(String[] args)
{
int num1, num2;
Scanner input = new Scanner(System.in);
System.out.print("Enter Num1: ");
num1 = input.nextInt();
System.out.print("Enter Num2: ");
num2 = input.nextInt();
System.out.print("The Sum is: " + (num1 + num2));
}
}
Output: Enter Num1: 1000
Enter Num2: 2000
The Sum is: 3000
import java.util.Scanner;
public class WorkHourPayRate
{
public static void main(String[] args)
{
int workDays, workHours, payRate, weeklyPay;
workDays = 5;
Scanner input = new Scanner(System.in);
System.out.print("Enter Hourly Pay Rate: ");
payRate = input.nextInt();
System.out.print("Enter Work Hours: ");
workHours = input.nextInt();
weeklyPay = workDays * workHours * payRate;
System.out.print("Weekly Pay is: " + weeklyPay);
}
}
Output: Enter Hourly Pay Rate: 10000
Enter Work Hours: 8
Weekly Pay is: 400000
import java.util.Scanner;
public class MileToKiloMeter
{
public static void main(String[] args)
{
int mile;
float km;
Scanner input = new Scanner(System.in);
System.out.print("Enter Mile: ");
mile = input.nextInt();
km = (float)(mile * 1.609344);
System.out.print("KiloMeter is: " + km);
}
}
Output: Enter Mile: 3
KiloMeter is: 4.828032
public class Time
{
public static void main(String[] args)
{
int minutes = 376, hours, min ;
hours = minutes/60;
min = minutes%60;
System.out.print("Time = "+hours+" hours "+min+"minutes");
}
}

Output: Time= 6 hours 16 minutes


import java.util.Scanner;
public class HmsToSecond
{
public static void main (String[] args)
{
int hr, min, sec;
long seconds;
Scanner input = new Scanner(System.in);
System.out.print("Enter Hour: ");
hr= input.nextInt();
System.out.print("Enter Minute: ");
min= input.nextInt();
System.out.print("Enter Second: ");
sec= input.nextInt();
seconds = hr * 60 + min * 60+ sec;
System.out.println(“Total number of Seconds = " +
seconds + " Seconds");
}
}
import java.util.Scanner;
public class SecondToHMS
{
public static void main(String[] args)
{
int second, hours, minutes, seconds;
Scanner input = new Scanner(System.in);
System.out.print("Enter Second: ");
second=input.NextInt();
hours = second/3600;
minutes = (second%3600)/60;
seconds = (second%3600)%60;
System.out.println(" Hours, Minutes & Seconds = " + hours +
" Hours " + minutes +" Minutes " + seconds + " Seconds");
}
}
Output: Hours, Minutes & Seconds =
import java.util.Scanner;
public class Digit
{
public static void main(String[] args)
{
int num;
Scanner input = new Scanner(System.in);
System.out.print("Enter Three Digit Number: ");
num = input.nextInt();
System.out.println("First Digit\t: " + num/100);
System.out.println("Second Digit\t: " + (num%100)/10);
System.out.println("Third Digit\t: " + (num%100)%10);
}
}
Output: Enter Three Digit Number: 639
First Digit : 6
Second Digit : 3
Third Digit : 9
public class Payroll
{
public static void main(String[] args)
{
int hourlyPayRate = 500;
int hours = 40;
double taxRate = .15; //15 percent
int grossPay = hourlyPayRate * hours;
double withholdingTax = grossPay * taxRate;
double netPay = grossPay - withholdingTax;
System.out.println("Hourly Pay Rate\t: " + hourlyPayRate +
“ Kyats");
System.out.println("One Week Hours\t: " + hours + " Hours");
System.out.println("Tax Rate\t: " + taxRate + " // 15 Percent");
System.out.println("\nGross Pay\t: " + grossPay + "Kyats");
System.out.println("Withholding Tax\t: " + withholdingTax +
" Kyats");
System.out.println("Net Pay\t\t: " + netPay + " Kyats");
}
}

Output: Hourly pay rate : 500 Kyats


One Week Hours : 40 Hours
Tax Rate : .15 // 15 Percent

One Week Hours : 20000 Kyats


Withholding Tax : 3000.0 Kyats
Net Pay : 17000.0 Kyats
public class UpperToLower
{
public static void main(String[] args) throws Exception
{
char input;
System.out.print("Enter Upper Case Character: ");
input = (char)(System.in.read()+32);
System.out.print("Convert to Lower Case = " + input);
}
}
public class LowerToUpper
{
public static void main(String[] args) throws Exception
{
char input;
System.out.print("Enter Lower Case Character: ");
input = (char)(System.in.read()- 32);
System.out.print("Convert to Upper Case = " + input);
}
}

You might also like