0% found this document useful (0 votes)
81 views

Java Homework: Name: Abdullah Raoufi Shift: Morning Semester: 3

The document contains 14 Java programs written by Abdullah Raoufi for his homework. Program 1 prints his name and occupation. Program 2 gets user input of personal details using Scanner. Program 3 performs basic math operations on user-input numbers. The remaining programs demonstrate various Java concepts like loops, arrays, methods, classes through examples like temperature conversion, currency conversion, shape printing, multiplication tables etc.

Uploaded by

Abdullah Raoufi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views

Java Homework: Name: Abdullah Raoufi Shift: Morning Semester: 3

The document contains 14 Java programs written by Abdullah Raoufi for his homework. Program 1 prints his name and occupation. Program 2 gets user input of personal details using Scanner. Program 3 performs basic math operations on user-input numbers. The remaining programs demonstrate various Java concepts like loops, arrays, methods, classes through examples like temperature conversion, currency conversion, shape printing, multiplication tables etc.

Uploaded by

Abdullah Raoufi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Java Homework

Name: Abdullah Raoufi


Shift: Morning
Semester: 3
Program 1
public class Print{

public static void main(String[] args){

System.out.println("My name is Abdullah Raouf !\nI am fullstock web developer\n ");

Program 2
import java.util.Scanner;

public class Input {

public static void main(String[] args){

Scanner input= new Scanner(System.in);

String firstName,lastName ,address,comment,cellPhone;

int age;

System.out.print(" Enter First name: ");

firstName=input.nextLine();

System.out.print("Enter Last name: ");

lastName=input.next();

System.out.print("Age: ");

age=input.nextByte();
input.nextLine();

System.out.print("Enter your phone Number: ");

cellPhone=input.nextLine();

System.out.print("Enter your Address: ");

address=input.nextLine();

System.out.print("What is your comment: ");

comment=input.nextLine();

System.out.println("First name: "+firstName);

System.out.println("Last name: "+lastName);

System.out.println("your Age is : "+age);

System.out.println("your number is phone: "+cellPhone);

System.out.println("your Address: "+address);

System.out.println("your Comment: "+comment);

Program 3
import java.util.Scanner;

public class Operations {

public static void main(String[] args){

Scanner input=new Scanner(System.in);

int num1,num2,sum,sub,multi,divi;

System.out.print("Number 1: ");

num1=input.nextInt();

System.out.print("Number 2: ");

num2=input.nextInt();

System.out.println("Sum = "+(num1+num2));

System.out.println("Multiplication = "+(num1*num2));

if (num1>num2){
System.out.println("Subtraction = "+(num1-num2));

System.out.println("Divition = "+(num1/num2));

else{

System.out.println("Subtraction = "+(num2-num1));

System.out.println("Divition = "+(num2/num1));

Program 4
import java.util.Scanner;

public class fahrenheit_celsius {

public static void main(String[] args){

Scanner input=new Scanner(System.in);

int choice;

double c,f;

System.out.println("1.Celsius to Fahrenheit\n2.Fahrenheit to celsius");

System.out.print("Your choice: ");

choice=input.nextInt();

switch (choice){

case 1:

System.out.print("Celsius: ");

c=input.nextDouble();

double fahrenheit=c*9.0/5.0+32;
System.out.println(c+" Celcius = "+fahrenheit +" Fahrenheit");

break;

case 2:

System.out.print("Fahrenheit: ");

f=input.nextDouble();

double celcius=(5.0/9.0)*(f-32);

System.out.println(f+" Fahrenheit = "+celcius+" Celcius");

break;

default :

System.out.print("Wrong choice!");

break;

Program 5

import java.util.Scanner;

public class currency_converter {

public static void main(String[] args) {

Scanner input=new Scanner(System.in);

int choice;

float one_usd;

double usd,af;

System.out.println("1.USD-to-AF\n2.AF-to-USD");

System.out.print("Your choice: ");

choice=input.nextInt();
switch(choice){

case 1:

System.out.print("One USD to AF? ");

one_usd=input.nextFloat();

System.out.print("How much USD: ");

usd=input.nextDouble();

System.out.println(usd+" Dollars = "+one_usd*usd+" Afghani");

break;

case 2:

System.out.print("One USD to AF? ");

one_usd=input.nextFloat();

System.out.print("How much AF: ");

af=input.nextDouble();

System.out.println(af+" Afghani = "+af/one_usd+" US dollar");

break;

default:

System.out.println("You can only choose 1 or 2");

break;

Program 6

import java.util.Scanner;

import static java.lang.Math.*;

public class rectangle_square_triangle {


public static void main(String[] args) {

Scanner input=new Scanner(System.in);

int choice;

float length, width,side,a,b,c,s;

System.out.println("1.Rectangle\n2.Square\n3.Triangle");

System.out.print("Your choice: ");

choice=input.nextInt();

switch(choice){

case 1:

System.out.print("Length: ");

length=input.nextFloat();

System.out.print("Width: ");

width=input.nextFloat();

System.out.println("Perimeter = "+2*(length+width));

System.out.println("Area = "+width*length);

break;

case 2:

System.out.print("Side: ");

side=input.nextFloat();

System.out.println("Perimeter = "+4*side);

System.out.println("Area = "+pow(side,2));

break;

case 3:
System.out.print("Three sides:\na: ");

a=input.nextFloat();

System.out.print("b: ");

b=input.nextFloat();

System.out.print("c: ");

c=input.nextFloat();

s=(a+b+c)/2;

System.out.println("Perimeter = "+(a+b+c));

System.out.println("Area = "+(sqrt(s*(s-a)*(s-b)*(s-c))));

break;

default:

System.out.println("Can only operate Rectangle, Square and Triangle");

break;

Program 7

import java.util.Scanner;

import static java.lang.Math.*;

public class quadratic_equations {

public static void main(String[] args){

Scanner input=new Scanner(System.in);

double a,b,c,delta,x1,x2;

System.out.println("Quadratic equation coefficients");

System.out.print("a: ");

a=input.nextDouble();
System.out.print("b: ");

b=input.nextDouble();

System.out.print("c: ");

c=input.nextDouble();

if (a==0||b==0||c==0){

System.out.println("The coefficients must not be zero.");

return;

delta=pow(b,2)-(4*a*c);

if (delta<0)

System.out.println("The equation does not have a real root")

else

if (delta==0){

x1=-b/(2*a);

System.out.println("Root is = "+x1);

else

x1=-(b+sqrt(delta))/(2*a);

x2=(-b-sqrt(delta))/(2*a);

System.out.println("X1 = "+x1+"\nX2 = "+x2);

Program 8
public class Loop1 {

public static void main(String [] args){

for (int i=500; i<=5000;i++)

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

Program 9

public class loop_total {

public static void main(String[] args) {

int total=0;

for (int i=100;i>=0;i--){

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

total+=i;

System.out.println("Total = "+total);

Program 10

package pkg9_loop_odd_even;
public class Main {

public static void main(String[] args) {

System.out.println("Evens: ");

for(int i=0;i<=100;i++){

if(i%2==0)

System.out.print(i+" ");

System.out.println();

System.out.println("Odds: ");

for (int ii=0;ii<100;ii++){

if(ii%2!=0)

System.out.print(ii+" ");

System.out.println();

Program 11
package pkg11_loop_5_jump;

public class Main {

public static void main(String[] args) {

int counter=5;

while (counter<=50){

System.out.println(counter);

counter+=5;

}
}

Program 12

public class multipulication_table {

public static void main(String[] args) {

for (int i=1;i<=10;i++){

for (int j=1;j<=10;j++)

System.out.print(i*j+" ");

System.out.println(" ");

Program 13
public class shape {
public static void main(String[] args){
char arr[][]=new char[10][10];
int traverse=1;

for (int i=0;i<10;i++){


for (int j=0;j<traverse;j++)
arr[i][j]='*';
traverse++;
}

int col_repeat=1;
for (int row=0;row<10;row++){
for (int column=0;column<col_repeat;column++)
System.out.print(arr[row][column]+" ");

col_repeat++;
System.out.println();
}

}
}

Program 14
public class multipulicatio_table {
public static void main(String[] args){
int multi_table[][]=new int[10][10];

int multiplier=1;

for (int multiplicand=0;multiplicand<10;multiplicand++){


if (multiplicand==0)
multi_table[multiplicand][0]=1*multiplier;
else{
multiplier++;
multi_table[multiplicand][multiplier-1]=multiplicand*multiplier;
}
}

for (int row=0;row<10;row++){


for (int column=0;column<10;column++){
System.out.print(multi_table[row][column]+" ");
}
System.out.println();
}

}
}

You might also like