Addition (Using variables)
class Addition {
static final int p = 1000;
static final int m = 9;
public static void main (String [] args) {
System.out.println (“Simple addition. This is my first experiment in java”);
int Addition = p + m ;
System.out.println (“The answer of of “+p+ ” and” +m+
” equals ” +Addition);
}
}
Pythagoras
public class Pythagoras
{
public static void main (String [] args){
int a = 3;
int b = 4;
double power = Math.pow (a,2) + Math.pow (b,2);
double h = Math.sqrt(power);
System.out.println (“The Hypotenuse of a right angled triangle of sides 3cm and 4cm =”+h+” cm”);
}
}
Adding Integers (using Scanner class)
import java.util.*;
public class addSixInt
{
public static void main(String[] args)
{
System.out.println( “This simple program adds six numbers using the nextInt” );
System.out.println( “Please, write down five numbers:” );
int n1, n2, n3, n4, n5, n6;
Scanner readkeyboard= new Scanner(System.in);
n1 = readkeyboard.nextInt();
n2 = readkeyboard.nextInt();
n3 = readkeyboard.nextInt();
n4 = readkeyboard.nextInt();
n5 = readkeyboard.nextInt();
n6 = readkeyboard.nextInt();
System.out.println( “The sum of those three numbers is: ” + (n1 + n2 + n3+ n4 + n5 +n6) );
}
}
Pythagoras (using Scanner Class)
import java.util.*;
import java.io.*;
public class PythagorasInput
{
public static void main (String [] args){
Scanner readkeyboard;
readkeyboard = new Scanner(System.in);
System.out.println( “This simple program will calculate the Hypotenuse of a triangle: “);
System.out.println( “Please enter side a of the triangle: “);
int a = readkeyboard.nextInt();
System.out.println( “Please enter side b of the triangle: “);
int b = readkeyboard.nextInt();
double power = Math.pow (a,2) + Math.pow (b,2);
double h = Math.sqrt (power);
System.out.println (“The Hypotenuse of a right angled triangle of sides “+a+” and “+b+ ” cm is ” +h+ ” cm”);
}
}
Case Switch
import java.util.*;
class Seasons
{
public static void main (String[] args){
Scanner sc= new Scanner(System.in);
int month = 12;
System.out.println( “This program allows the user to enter the season : “);
System.out.println( “Enter number of month: “);
month = sc.nextInt();
switch (month) {
case 12:
case 1:
case 2: System.out.println(“Winter”);break;
case 3:
case 4:
case 5: System.out.println(“Spring”);break;
case 6:
case 7:
case 8: System.out.println(“Summer”);break;
case 9:
case 10:
case 11: System.out.println(“Autumn”);break;
default: System.out.println(“Invalid month.”);break;
}
}
}
Populating 200 random numbers; finding average and numbers above the average and below the average
{
static int randomInt (int min, int max)
{
return min + (int) (Math.random()*(max-min+1));
}
public static void main (String[] args)
{ int arr[] = new int[200];
int sum=0; int above=0; int below =0;
float aver;
for (int k=0 ; k<=199; k++)
{ arr[k] = randomInt(0,100);
System.out.print(arr[k]+”\t”);
sum = sum + arr[k];
}
aver = sum/200;
System.out.println(“The average value is: “+aver+”\n”);
for (int k=0 ; k<=199; k++)
{ if (arr[k] < aver)
below++;
if (arr[k] > aver)
above++;
}
System.out.println(“Number of numbers above: “+above);
System.out.println(“Number of numbers below: “+below);
System.out.println();
System.out.println();
}
}
Generate random numbers without repetition
class RandNoRepetition
{
public static void main (String[] args)
{ int myArr[] = new int[20];
int temp;
boolean found;
System.out.println (“Task: Generate 20 random numbers within 100-119 WITHOUT repetition”);
System.out.println (“Random number generation”);
for (int i=0; i<20;i++) // 20 numbers required
{ do
{ temp = (int)(Math.round(100 + 20*Math.random()));
found = false;
for (int j=0;j<i; j++)
if (temp == myArr[j])
found = true;
}
while (found);
myArr[i] = temp;
System.out.println (“Random Number always >=100 generated: ” + myArr[i]);
}
}
}
Money conversion (Using JOptionPane)
import javax.swing.*;
import java.io.*;
public class Euroconverter {
static float factor = (float)0.429;
public static void main(String args[]) {
Integer num;
Character c;
Float Inputamount;
String str = JOptionPane.showInputDialog
(“Please Enter 1 for Euro-to-LM conversion or 2 for Lm-to-Euro Conversion “);
num= new Integer(Integer.parseInt(str));
if (num==2) {
String ammont = JOptionPane.showInputDialog (“Enter amount in Lm to convert “);
Inputamount= new Float (Float.parseFloat (ammont));
Float AmountinEuros = Inputamount/factor;
JOptionPane.showMessageDialog (null, “This amount is equal to ” +AmountinEuros + ” Euros”);
}
else{
String tieniammont = JOptionPane.showInputDialog
(“Enter amount in Euros to convert “);
Inputamount= new Float (Float.parseFloat (tieniammont));
float AmountinLM = Inputamount*factor;
JOptionPane.showMessageDialog (null, “This amount is equal to ” +AmountinLM + ” LM”);
}
JOptionPane.showMessageDialog (null, “Thank you for using our Euro converter ” );
System.exit(0);
}
}
Number Guessing game
// Number guessing game created by Omar Seguna
import java.util.*;
public class Numberguessinggame // The user is asked to guess the number and has 6 tries
{
static int randomnumber(int min, int max)
{
return (int)(Math.random() * max + 1);
}
public static void main (String[] args) // main
{
Scanner keyboard;
keyboard = new Scanner(System.in);
int a;
int i; //list of all variables
String b;
do {
int x= randomnumber (1,100);
System.out.println( “I thought of a number from 1 to 100. Please try to guess the number : “);
for (i=1;i<60;i++) //print 60 wonderful stars
System.out.print(“*”);
System.out.println(” “);
//count must be initialised each time that the user starts a new game
int count = 1;
boolean correct = false;
do{ // will ask you the number until correct answer
//count++; count should be incremented with each try not with each game
do { // will ask you the number unless smaller than 100
System.out.println( “Please enter the number from 1 to 100: “);
a = keyboard.nextInt();
} while (a >100);
if (a==x){
correct = true;
System.out.println (“You are a genius”); //if correct answer
} else if (a < x) {
//only check whether its less
System.out.print(“Try a bigger number. “);
count++;
} else if (a > x) {
//only check whether its high
System.out.print(“Try a smaller number. “);
count++;
}
} while (count<=6 && correct == false);
System.out.println (“The number was ” +x ); //will print the random number
System.out.println (“Do you want to play again? [y/n]”);
b = keyboard.next();
} while (b.equals (“y”));
} // end of main
} // end of class