java calasses code example

Example 1: java calculator

package main;

import java.util.Scanner;

public class calculator {

    public static void greetings(){
        System.out.println("Hi welcome to the calculator made by Pepa pig made in java! :D");
    }

    public static void instruction(){
        System.out.println("Hi welcome to the calculator made by Pepa pig made in java! :D");
    }

    public static void calculator(){
        Scanner keyboardInput = new Scanner(System.in);
        String calculator;

        do {
            System.out.print("Enter your first digit: ");
            double num1 = keyboardInput.nextDouble();

            System.out.print("Enter your operator: ");
            String op = keyboardInput.next();

            System.out.print("Enter your second digit: ");
            double num2 = keyboardInput.nextDouble();

            System.out.print("= ");

            if (op.equals("+")){
                System.out.print(num1 + num2);
                System.out.println("\nYou were adding");
                System.out.println("How I know that cause I have big brain and a smart programmer ;)");
            }else if (op.equals("-")){
                System.out.print(num1 - num2);
                System.out.println("\nYou were subtracting");
                System.out.println("How I know that cause I have big brain and a smart programmer ;)");
            }else if (op.equals("*")){
                System.out.print(num1 * num2);
                System.out.println("\nYou were multiplying");
                System.out.println("How I know that cause I have big brain and a smart programmer ;)");
            }else if (op.equals("/")){
                System.out.print(num1 / num2);
                System.out.println("\nYou were dividing");
                System.out.println("How I know that cause I have big brain and a smart programmer ;)");
            }else if (op.equals("%")){
                System.out.print(num1 % num2);
                System.out.println("\nYou were remanding");
                System.out.println("How I know that cause I have big brain and a smart programmer ;)");
            }else{
                System.out.println("Invalid operator");
            }
            System.out.print("Would you like to restart the calculator? (y or n): ");
            calculator = keyboardInput.next();
        }
        while (calculator.equals("y"));

        if (calculator.equals("n")){
            System.out.println("Leave then kiddo ;-;");
        }
    }


    public static void main(String[] args) {
        greetings();
        instruction();
        calculator();
    }
}

Example 2: using class in java

public class HelloWorld {
  public static void main(String[] args) {
   
   
   class Number{
   
   	int number;
   
   	public boolean isPos(){
   		if(number > 0){
   			return true;
   		} else {
   			return false;
   		}
   		
   	}
   }
   
   Number myNumber = new Number();
   
   myNumber.number = 7;
   
   if(myNumber.isPos()){
   	System.out.println(myNumber.number + " is positive!!!");
   } else {
   	System.out.println(myNumber.number + " is not positive!!!");
   }
   
  }
}

Example 3: CalculoIVE java

import java.util.Scanner;

public class CalculoIVE {

	public static void main(String[] args) {
		Scanner teclado = new Scanner(System.in);
		
		//D
		
		double prezo = 0;
		double ive = 0;
		double pfinal = 0;
		
		//E
		
		System.out.println("Introduza prezo sen IVE (€) : ");
			prezo = teclado.nextDouble();
		
		System.out.println("Introduza IVE (%) : ");
			ive = teclado.nextDouble();
		
		//P
		
		pfinal = prezo * (1 + (ive / 100));
		
		//S
		
		System.out.println("Importe con IVE = " + pfinal + "€");

	}

}

Tags:

Java Example