Examples Java 2 كورس تعلم جافا للمبتدا والمحترف

Examples Java 2 كورس تعلم جافا للمبتدا والمحترف ، كورس تعلم لغة جافا من البداية ، تعلم لغة جافا من الصفر . 

Program to Create a Simple Calculator.

Program to Create a Simple Calculator.

Example

Main.java
			  
 import java.util.Scanner;
import java.io.*;
class Calculator
{
public static void main(String[] args)
{
int choice;
int x = 0;
int y = 0;
int sum;
PrintStream out;
Scanner input;
Calculator calc = new Calculator();
try
{
out = new PrintStream("calclog.txt");
do
{
System.out.println("Calculator Program"); System.out.println("--------------------\n");
System.out.println("1. Add");
System.out.println("2. Subtract");
System.out.println("3. Multiply");
System.out.println("4. Divide");
System.out.println("5. Mod");
System.out.println("6. Power");
System.out.println("99. End Program\n");
System.out.println("Enter Choice: ");
input = new Scanner(System.in);
choice = input.nextInt();
while ((choice < 1 || choice > 6) && choice != 99)
{
System.out.println("Please enter 1, 2, 3, 4, 5, or 6: ");
choice = input.nextInt();
}
if (choice != 99)
{
System.out.println("Please enter 2 numbers only: ");
x = input.nextInt();
y = input.nextInt();
}
switch (choice)
{
case 1:
sum = calc.add(x, y);
System.out.printf("The sum is %d\n\n", sum);
out.println(x + "+" + y + "=" + sum);
break;
case 2:
sum = calc.sub(x, y);
System.out.printf("The answer is %d\n\n", sum);
out.println(x + "-" + y + "=" + sum);
break;
case 3:
sum = calc.multi(x, y);
System.out.printf("The answer is %d\n\n", sum);
out.println(x + "*" + y + "=" + sum);
break;
case 4:
try
{
sum = calc.div(x, y);
System.out.printf("The answer is %d\n\n", sum);
out.println(x + "/" + y + "=" + sum);
}
catch (Exception e)
{
System.out.println("\nError: Cannot Divide by zero\n\n");
}
break;
case 5:
sum = calc.mod(x, y);
System.out.printf("The mod is %d\n\n", sum);
out.println(x + "%" + y + "=" + sum);
break;
case 6:
sum = calc.pow(x, y)
System.out.printf("The answer is %d\n\n", sum);
out.println(x + "^" + y + "=" + sum);
break;
}
}
while (choice != 99);
input.close();
System.out.println("Ending program...");
}
catch (Exception e){
System.out.println("ERROR: Some error occured");
e.printStackTrace();
}
}
public int add(int num1, int num2)
{int sum;
sum = num1 + num2;
return sum;
}
public int sub(int num1, int num2)
{ int sum;
sum = num1 - num2;
return sum;
}
public int multi(int num1, int num2)
{ int sum;
sum = num1 * num2;
return sum;
}
public int div(int num1, int num2)
{int sum;
sum = num1 / num2;
return sum;
}
public int mod(int num1, int num2)
{int sum;
sum = num1 % num2;
return sum;
}
public int pow(int base, int exp)
{int sum = 1;
if (exp == 0)
{sum = 1;
}
while (exp > 0)
{sum = sum * base;
exp--;
}
return sum;
}
}

			

Program to Calculate Mean.

Program to Calculate Mean.

Example

Main.java
			  

import java.util.Scanner;

class CalculateMean
{

public static void main(String[] args)
{

int sum = 0, inputNum;
int counter;
float mean;
Scanner NumScanner = new Scanner(System.in);

System.out.println("Enter the total number of terms whose mean you want to calculate");

counter = NumScanner.nextInt();

System.out.println("Please enter " + counter + " numbers:");

for (int x = 1; x <= counter; x++)
{
inputNum = NumScanner.nextInt();
sum = sum + inputNum;
System.out.println();
}

mean = sum / counter;
System.out.println("The mean of the " + counter + " numbers you entered is " + mean);
}
}


			

Program to Convert Binary to Decimal.

Program to Convert Binary to Decimal.

Example

Main.java
			  

import java.io.*;

class BinaryToDecimal
{
public static void main(String[] args) throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Binary no. to convert in Decimal : ");
String number = br.readLine();

/*
to convert Binary number to decimal number use,
int parseInt method of Integer wrapper class.

Pass 2 as redix second argument.
*/

int decimalNumber = Integer.parseInt(number, 2);
System.out.println("Binary number converted to decimal number");
System.out.println("Decimal number is : " + decimalNumber);

}
}


			

Output

Program to Convert Binary to Octal.

Program to Convert Binary to Octal.

Example

Main.java
			  
 
import java.io.*;

class BinaryToOctal
{
public static void main(String[] args) throws Exception
{
String num = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter binary number : ");
num = br.readLine();
int dec = Integer.parseInt(num, 2);

String oct = Integer.toOctalString(dec);

System.out.println("Binary " + num + " in Octal is " + oct);

}
}


			


Program to Convert Decimal to Binary.

Program to Convert Decimal to Binary.

Example

Main.java
			  

import java.util.Scanner;

class DecimalToBinary
{

public String toBinary(int n)
{
if (n == 0)
{
return "0";
}

String binary = "";
while (n > 0)
{
int rem = n % 2;
binary = rem + binary;
n = n / 2;
}

return binary;
}

public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int decimal = scanner.nextInt();

DecimalToBinary decimalToBinary = new DecimalToBinary();
String binary = decimalToBinary.toBinary(decimal);

System.out.println("The binary representation is " + binary);
}
}


			

Program to Find Fraction Addition.

Program to Find Fraction Addition.

Example

Main.java
			  

import java.util.*;

class FractionAdding
{
public static void main(String args[])
{
float a, b, c, d;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a : ");
a = scanner.nextFloat();
System.out.print("Enter b : ");
b = scanner.nextFloat();
System.out.print("Enter c : ");
c = scanner.nextFloat();
System.out.print("Enter d : ");
d = scanner.nextFloat();

// fraction addition formula ((a*d)+(b*c))/(b*d)
System.out.print("Fraction Addition : [( " + a + " * " + d + " )+( " + b + " * " + c + " ) / ( " + b + " * " + d + " )] = " + (((a * d) + (b * c)) / (b * d)));
}
}


			

Program to Find Fraction Subtraction.

Program to Find Fraction Subtraction.

Example

Main.java
			  

import java.util.*;

class FractionSubtraction
{
public static void main(String args[])
{
float a,b,c,d;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a : ");
a = scanner.nextFloat();
System.out.print("Enter b : ");
b = scanner.nextFloat();
System.out.print("Enter c : ");
c = scanner.nextFloat();
System.out.print("Enter d : ");
d = scanner.nextFloat();

// fraction addition formula ((a*d)-(b*c))/(b*d)
System.out.print("Fraction subtraction : [( "+a+" * "+d+" )-( "+b+" * "+c+" ) / ( "+b+" * "+d+" )] = "+(((a*d)-(b*c))/(b*d)));
}
}


			

Program to Find GCDLCM.

Program to Find GCDLCM.

Example

Main.java
			  

import java.util.Scanner;

class GCDLCM
{
public static void main(String args[])
{
int x, y;
Scanner sc = new Scanner(System.in);

System.out.println("Enter the two numbers: ");
x = sc.nextInt();
y = sc.nextInt();

System.out.println("GCD of two numbers is : " + gcd(x, y));
System.out.println("LCM of two numbers is : " + lcm(x, y));
}

static int gcd(int x, int y)
{
int r = 0, a, b;
a = (x > y) ? x : y; // a is greater number
b = (x < y) ? x : y; // b is smaller number

r = b;
while (a % b != 0)
{
r = a % b;
a = b;
b = r;
}
return r;
}

static int lcm(int x, int y)
{
int a;
a = (x > y) ? x : y; // a is greater number
while (true)
{
if (a % x == 0 && a % y == 0)
{
return a;
}
++a;
}
}
}


			

Program to Find Harmonic Series.

Program to Find Harmonic Series.

Example

Main.java
			  
import java.util.*;

class HarmonicSeries
{

public static void main(String args[])
{

int num, i = 1;
double rst = 0.0;

Scanner in = new Scanner(System.in);
System.out.println("Enter the number for length of series");
num = in.nextInt();

while (i <= num)
{

System.out.print("1/" + i + " +");
rst = rst + (double) 1 / i;

i++;
}

System.out.println("\n\nSum of Harmonic Series is " + rst);
}
}


			

Program to Create Multiplication Table.

Program to Create Multiplication Table.

Example

Main.java
			  

import java.util.Scanner;

class MultiplicationTable
{

public static void main(String args[])
{

int n, c;
System.out
.println("Enter an integer to print it's multiplication table");

Scanner in = new Scanner(System.in);
n = in.nextInt();
System.out.println("Multiplication table of " + n + " is :-");

for (c = 1; c <= 10; c++)
{
System.out.println(n + "*" + c + " = " + (n * c));
}

}
}


			
تعليقات