variable vs constant in java

0

variable: its value can be changed in any time in the code.

constant: is value can not be changed in the code.

 

example of define variables: java code to sum 2 string variables and print the result

String firstName="ahmed";
String lastName="mahdi";
String fullName=firstName+" "+lastName;
System.out.println(fullName);

output : 

ahmed mahdi

---------------------

int number1=15;
int number2=5;
int result=number1+number2;
System.out.println(result);

output:

20

----------------------

read firstname and last name from keyboard then print the resulting full name:

import java.util.Scanner;

public class Main
{
	public static void main(String[] args) {
	Scanner input=new Scanner(System.in);
	System.out.println("enter first name: ");
	String firstname=input.next();
	System.out.println("enter last name: ");
	String lastname=input.next();
	String fullname=firstname+" "+lastname;
	System.out.println(fullname);
}
}

-------------------

read 2 integers from keyboard and calculates summation of them then prints the summation:

import java.util.Scanner;

public class Main
{
	public static void main(String[] args) {
	    Scanner input=new Scanner(System.in);
	    System.out.println("enter first number:");
	    int number1=input.nextInt();
	    System.out.println("enter second number:");
	    int number2=input.nextInt();
	    int result=number1+number2;
	    System.out.println(result);
}
}

 

define boolean variable :

boolean is_smoker=true;
System.out.println(is_smoker);

 

 

 

 

All Contributions

(Geometry: area of a hexagon) Write a program that prompts the user to enter the

side of a hexagon and displays its area.

import java.util.Scanner;

public class Exercise_02_16 {
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);

  // Prompt the user to enter the side of a hexagon
  System.out.print("Enter the side: ");
  double side = input.nextDouble();

  // Compute the area of the hexagon
  double area = ((3 * Math.pow(3, 0.5)) / 2) * Math.pow(side, 2);

  // Display result
  System.out.println("The area of the hexagon is " + area); 
 }
}

create simple calculater using java 

Scanner input = new Scanner(System.in);
System.out.println("please enter first number:");
int num1=input.nextInt();
System.out.println("please enter second number:");
int num2=input.nextInt();
int summation_result=num1+num2;
int subtraction_result=num1-num2;
int multiplication_result=num1*num2;
int division_result=num1/num2;
int remainder_result=num1%num2;
System.out.println("the summation result is :" + summation_result);
System.out.println("the subtraction result is :"+subtraction_result);
System.out.println("the multiplication result is :"+multiplication_result);
System.out.println("the division result is :"+division_result);
System.out.println("the remainder is :"+remainder_result);

write java program to switch values between 2 variables, using a temporary third variable

int x=5;
int y=10;
System.out.println("x before is : "+x+"   y before is : "+y);
int temp=x;
x=y;
y=temp;
System.out.println("x after is : "+x+"   y after is : "+y);

write java program to calculate rectangle perimeter, program should prompts user to enter length and width, then calculate the area and perimeter based on it

import java.util.Scanner;

public class Main
{
 public static void main(String[] args) {
	Scanner input=new Scanner(System.in);
	System.out.println("please enter the length of rectangle:");
	int length=input.nextInt();
	System.out.println("please enter the width of rectangle:");
	int width=input.nextInt();
	int area=length*width;
	System.out.println("the rectangle area is "+area);
	int perimeter=(length+width)*2;
	System.out.println("the rectangle perimeter is "+perimeter);
}	

write java program to calculate square area and perimeter, program should prompts user to enter side length, then calculate the area and perimeter based on it:

import java.util.Scanner;

public class Main
{
 public static void main(String[] args) {
	Scanner input=new Scanner(System.in);
	System.out.println("please enter the side length");
	int sideLength=input.nextInt();
	int area=sideLength*sideLength;
	System.out.println("the square area is "+area);
	int perimeter=sideLength*2;
	System.out.println("the square perimeter is "+perimeter);
}
}

total contributions (6)

New to examplegeek.com?

Join us