Following are examples of some short Java language codings/programs I created during my short course and in my spare time. All of these were made in 2015.
A program to categorize your age
import java.util.*;
class age
{
public static void main(String[]args)
{
int age;
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter your age: ");
age = keyboard.nextInt();
if ((age <= 100) && (age >= 60))
{
System.out.println("Senior");
}
else if ((age < 60) && (age >= 50))
{
System.out.println("Middle age");
}
else if ((age < 50) && (age >= 30))
{
System.out.println("Adult");
}
else if ((age <30) && (age >= 20))
{
System.out.println("Youth");
}
else if ((age <20) && (age >= 13))
{
System.out.println("Teenage");
}
else if ((age > 0) && (age <= 13))
{
System.out.println("Child");
}
else
{
System.out.println("Error");
}
}
}
public static void main(String[]args)
{
int age;
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter your age: ");
age = keyboard.nextInt();
if ((age <= 100) && (age >= 60))
{
System.out.println("Senior");
}
else if ((age < 60) && (age >= 50))
{
System.out.println("Middle age");
}
else if ((age < 50) && (age >= 30))
{
System.out.println("Adult");
}
else if ((age <30) && (age >= 20))
{
System.out.println("Youth");
}
else if ((age <20) && (age >= 13))
{
System.out.println("Teenage");
}
else if ((age > 0) && (age <= 13))
{
System.out.println("Child");
}
else
{
System.out.println("Error");
}
}
}
A program to check area of a triangle
import java.util.*;
public class area_of_triangle
{
public static void main(String[]args)
{
System.out.println ("Triangle area calculation by Akash Kumar");
int Height,Length,Area;
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter height: ");
Height = keyboard.nextInt();
System.out.print("Please enter length: ");
Length = keyboard.nextInt();
Area = (Length*Height) / 2;
System.out.println("Area of triangle is = " + Area);
}
}
{
public static void main(String[]args)
{
System.out.println ("Triangle area calculation by Akash Kumar");
int Height,Length,Area;
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter height: ");
Height = keyboard.nextInt();
System.out.print("Please enter length: ");
Length = keyboard.nextInt();
Area = (Length*Height) / 2;
System.out.println("Area of triangle is = " + Area);
}
}
A program about class extension
class Human
{
void walk()
{
System.out.println("Human walks");
}
}
public class Boy extends Human
{
public void walk()
{
System.out.println("Boy walks");
}
public static void main( String args[])
{
//Reference is of parent class
Human myobj = new Boy();
myobj.walk();
}
}
{
void walk()
{
System.out.println("Human walks");
}
}
public class Boy extends Human
{
public void walk()
{
System.out.println("Boy walks");
}
public static void main( String args[])
{
//Reference is of parent class
Human myobj = new Boy();
myobj.walk();
}
}
A simple calculator program
import java.util.*;
class menu
{
public static void main(String[]args)
{
char choice;
int no1,no2,result;
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter your first number: ");
no1 = keyboard.nextInt();
System.out.print("Please enter your second number: ");
no2 = keyboard.nextInt();
System.out.print("\n\n\t*****************************\n");
System.out.print("\t* SIMPLE CACULATOR *");
System.out.print("\n\t*****************************");
System.out.print("\n\t* A - Addition * ");
System.out.print("\n\t* S - Subtrction * ");
System.out.print("\n\t* M - Multification * ");
System.out.print("\n\t* D - Divition * ");
System.out.print("\n\t*****************************");
System.out.print("\n\t* Please enter your choice: ");
choice = keyboard.next().charAt(0);
System.out.print("\t*****************************\n");
if (choice == 'A')
{
result = no1 + no2;
System.out.println("\t*RESULT = " + result);
System.out.print("\t*****************************\n");
}
else if (choice == 'S')
{
result = no1 - no2;
System.out.println("RESULT = " + result);
}
else if (choice == 'M')
{
result = no1 * no2;
System.out.println("RESULT = " + result);
}
else if (choice == 'D')
{
result = no1 / no2;
System.out.println("RESULT = " + result);
}
else
{
System.out.println("Error");
}
}
}
public static void main(String[]args)
{
char choice;
int no1,no2,result;
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter your first number: ");
no1 = keyboard.nextInt();
System.out.print("Please enter your second number: ");
no2 = keyboard.nextInt();
System.out.print("\n\n\t*****************************\n");
System.out.print("\t* SIMPLE CACULATOR *");
System.out.print("\n\t*****************************");
System.out.print("\n\t* A - Addition * ");
System.out.print("\n\t* S - Subtrction * ");
System.out.print("\n\t* M - Multification * ");
System.out.print("\n\t* D - Divition * ");
System.out.print("\n\t*****************************");
System.out.print("\n\t* Please enter your choice: ");
choice = keyboard.next().charAt(0);
System.out.print("\t*****************************\n");
if (choice == 'A')
{
result = no1 + no2;
System.out.println("\t*RESULT = " + result);
System.out.print("\t*****************************\n");
}
else if (choice == 'S')
{
result = no1 - no2;
System.out.println("RESULT = " + result);
}
else if (choice == 'M')
{
result = no1 * no2;
System.out.println("RESULT = " + result);
}
else if (choice == 'D')
{
result = no1 / no2;
System.out.println("RESULT = " + result);
}
else
{
System.out.println("Error");
}
}
}
A program on storing and outputting data from arrays
import java.util.*;
class clean
{
public static void main(String[] args)
{
int index[] = { 1 , 2 , 3 , 4 , 5 };
String cleaner[] = {"John ", "Shawn ", "Melalin ","jane ", "Bruce "};
int Room[] = { 201 , 202 , 203 , 204 , 205 };
double time1[] = { 9.30, 12.00, 1.00, 8.30, 4.20};
int i;
for(i=0; i<5; i++)
{
System.out.println(index [i] +"\t" +cleaner [i] + "\t"+ Room [i] + "\t"+ time1 [i] );
}
}
}
public static void main(String[] args)
{
int index[] = { 1 , 2 , 3 , 4 , 5 };
String cleaner[] = {"John ", "Shawn ", "Melalin ","jane ", "Bruce "};
int Room[] = { 201 , 202 , 203 , 204 , 205 };
double time1[] = { 9.30, 12.00, 1.00, 8.30, 4.20};
int i;
for(i=0; i<5; i++)
{
System.out.println(index [i] +"\t" +cleaner [i] + "\t"+ Room [i] + "\t"+ time1 [i] );
}
}
}
A program whcih outputs the smallest integer from 3 integers taken from the user
import java.util.*;
class for_choice
{
public static void main(String[]args)
{
int a;int choice1;int choice2;int choice3;
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter choice 1: ");
choice1 = keyboard.nextInt();
System.out.print("Please enter choice 2: ");
choice2 = keyboard.nextInt();
System.out.print("Please enter choice 3: ");
choice3 = keyboard.nextInt();
for(a=choice1;a<=choice2;a=a+choice3)
{
System.out.println(a);
}
}
}
{
public static void main(String[]args)
{
int a;int choice1;int choice2;int choice3;
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter choice 1: ");
choice1 = keyboard.nextInt();
System.out.print("Please enter choice 2: ");
choice2 = keyboard.nextInt();
System.out.print("Please enter choice 3: ");
choice3 = keyboard.nextInt();
for(a=choice1;a<=choice2;a=a+choice3)
{
System.out.println(a);
}
}
}
A program which tells the grade according to the entered marks by the user
import java.util.*;
public class marks
{
public static void main(String[]args)
{
int marks;
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter your marks: ");
marks = keyboard.nextInt();
if ((marks >= 85) && (marks <= 100))
{
System.out.println("Congratulations! You scored Distinction");
}
else if ((marks >=70) && (marks <85))
{
System.out.println("Congratulations! You scored Merit");
}
else if ((marks >= 50) && (marks <70))
{
System.out.println("Well done! You passed the exam");
}
else if ((marks >= 0) && (marks <50))
{
System.out.println("Unfortunately you failed the exam. Better luck next time!");
}
else
{
System.out.println("Error");
}
}
}
{
public static void main(String[]args)
{
int marks;
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter your marks: ");
marks = keyboard.nextInt();
if ((marks >= 85) && (marks <= 100))
{
System.out.println("Congratulations! You scored Distinction");
}
else if ((marks >=70) && (marks <85))
{
System.out.println("Congratulations! You scored Merit");
}
else if ((marks >= 50) && (marks <70))
{
System.out.println("Well done! You passed the exam");
}
else if ((marks >= 0) && (marks <50))
{
System.out.println("Unfortunately you failed the exam. Better luck next time!");
}
else
{
System.out.println("Error");
}
}
}
A program which tells whether the entered integer vakue is positive or negative
import java.util.*;
class positive_or_negative
{
public static void main(String[]args)
{
System.out.println ("Number test by Akash Kumar");
int Number;
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter an integer value: ");
Number = keyboard.nextInt();
if (Number >= 0)
System.out.println("Integer value is positive!");
else if (Number < 0)
System.out.println("Integer value is negative!");
else
System.out.println("Error! Please try again.");
}
}
public static void main(String[]args)
{
System.out.println ("Number test by Akash Kumar");
int Number;
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter an integer value: ");
Number = keyboard.nextInt();
if (Number >= 0)
System.out.println("Integer value is positive!");
else if (Number < 0)
System.out.println("Integer value is negative!");
else
System.out.println("Error! Please try again.");
}
}
A program which stores student records and outputs them in an organised format
import java.util.*;
public class student_records
{
public static void main(String[]args)
{
System.out.println ("Student records by Akash Kumar");
int Age;
String Name,StudentID;
char Sex;
double Height;
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter your student ID: ");
StudentID = keyboard.next();
System.out.print("Please enter your Name: ");
Name = keyboard.next();
System.out.print("Please enter your gender: ");
Sex = keyboard.next().charAt(0);
System.out.print("Please enter your age: ");
Age = keyboard.nextInt();
System.out.print("Please enter your height in metres: ");
Height = keyboard.nextDouble();
System.out.println("Student ID: " + StudentID);
System.out.println("Name : " + Name);
System.out.println("Sex : " + Sex);
System.out.println("Age : " + Age);
System.out.println("Height : " + Height);
}
}
{
public static void main(String[]args)
{
System.out.println ("Student records by Akash Kumar");
int Age;
String Name,StudentID;
char Sex;
double Height;
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter your student ID: ");
StudentID = keyboard.next();
System.out.print("Please enter your Name: ");
Name = keyboard.next();
System.out.print("Please enter your gender: ");
Sex = keyboard.next().charAt(0);
System.out.print("Please enter your age: ");
Age = keyboard.nextInt();
System.out.print("Please enter your height in metres: ");
Height = keyboard.nextDouble();
System.out.println("Student ID: " + StudentID);
System.out.println("Name : " + Name);
System.out.println("Sex : " + Sex);
System.out.println("Age : " + Age);
System.out.println("Height : " + Height);
}
}
A program to check whether the entered alphabet is a vowel or not
import java.util.*;
class vowel
{
public static void main(String[]args)
{
char Vowel;
Scanner keyboard = new Scanner(System.in);
System.out.println("Vowel checker by Akash Kumar");
Vowel = keyboard.next().charAt(0);
switch (Vowel)
{
case 'a':case 'A':
System.out.println("Alphabet is a vowel!");
break;
case 'e' :case 'E':
System.out.println("Alphabet is a vowel!");
break;
case 'i' :case 'I':
System.out.println("Alphabet is a vowel!");
break;
case 'o' :case 'O':
System.out.println("Alphabet is a vowel!");
break;
case 'u' :case 'U':
System.out.println("Alphabet is a vowel!");
break;
default:
System.out.println("Alphabet is not a vowel!");
}
}
}
public static void main(String[]args)
{
char Vowel;
Scanner keyboard = new Scanner(System.in);
System.out.println("Vowel checker by Akash Kumar");
Vowel = keyboard.next().charAt(0);
switch (Vowel)
{
case 'a':case 'A':
System.out.println("Alphabet is a vowel!");
break;
case 'e' :case 'E':
System.out.println("Alphabet is a vowel!");
break;
case 'i' :case 'I':
System.out.println("Alphabet is a vowel!");
break;
case 'o' :case 'O':
System.out.println("Alphabet is a vowel!");
break;
case 'u' :case 'U':
System.out.println("Alphabet is a vowel!");
break;
default:
System.out.println("Alphabet is not a vowel!");
}
}
}