Java

Subtitle

Home


//Back slash (\) skips a line
public class grades
{
public static void main (String [] args)
  {
     int g1=90;
     int g2=89;
     int g3=85;
     int total=g1+g2+g3;
     int average=total/3;
     System.out.println("The grade average is " + average);
  }
}

_____________________________________________________________________
Pay(Input 3 things)
import java.util.Scanner;
public class Pay
{
public static void main (String [] args)
  {
    Scanner input = new Scanner(System.in);
    System.out.println("Name of employee:");
    String name;
    name = input.next();

    System.out.println("Input number of hours they worked:");
    int hours = input.nextInt();

    System.out.println("Input the hourly rate:");
    double rate = input.nextDouble();

    double pay = hours * rate;

    System.out.println("The pay is $" + pay);
  }
}


_________________________________________________________________
If statement to output

import javax.swing.JOptionPane;
import java.util.Scanner;

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

    System.out.println("Enter your grade average:");
    average = input.nextInt();

    if (average >= 90 && average <= 100){
    System.out.println("You got an A!");
    }

    if (average >=80 && average <=89){
    System.out.println("You got a B!");
    }

    if (average >=70 && average <=79){
    System.out.println("You got a C!");
    }

  }
}

______________________________________________________________
import java.util.Scanner;
public class PrintHello
{
  public static void main (String [] args)
  {
    Scanner input = new Scanner (System.in);
    int count = 1, limit;
    limit = input.nextInt();
    while (count <= limit) {

    count = count + 1;
       }
   }
}

Order of operations:
// Write a Java program to declare 2 integers x and y;
// Add x plus y and print the sum;

public class Addition
{
public static void main (String [] args)
  {
    int x=10;
    int y=;
    double x1=10.5;
    int y1=6;
    int sum=x+y;
    int diff=x-y;
    int product=x*y;
    int difference=x/y;
    double difference1= x1 / y1;
    int remainder=x % y;

    System.out.println("The sum is " + sum);
    System.out.println("The difference is " + diff);
    System.out.println("The product is " + product);
    System.out.println("The difference is " + difference);

    System.out.println("x=10.5, y=5.5");
    System.out.println("The difference is " + difference1);
    System.out.println("the remainder is " + remainder);
  }
}

_________________________________________________
Text Box & Response

//Write a java program to display a box where you type your age

import javax.swing.JOptionPane;
public class PrintAge
{
public static void main (String [] args)
  {
    JOptionPane.showMessageDialog(null, "What's your age?");

    String age;
    age = JOptionPane.showInputDialog( "Type your age");
    JOptionPane.showMessageDialog(null, "Your age is " + age + ", Is that correct?");
  }
}

___________________________________________________
Average Calculation
  import javax.swing.JOptionPane;

public class AverageCalculation
{
  public static void main (String [] args)
  {

    double num1,num2,average;
    String temp1;
    String temp2;

    temp1 = JOptionPane.showInputDialog ("Enter a number");
    num1 = Double.parseDouble (temp1) ;
    temp2 = JOptionPane.showInputDialog ("Enter another number");
    num2 = Double.parseDouble(temp2);
    average = (num1 + num2) / 2;
    JOptionPane.showMessageDialog(null, "Your grade average is " + average);

  }
}

__________________________________________________

_____________________________________________________________________
Text Box Pop up

//Write a java program to display Hello Java with a dialog box

import javax.swing.JOptionPane;
public class PrintMessageGUI
{
public static void main (String [] args)
  {
    JOptionPane.showMessageDialog(null, "Hello Java");
    JOptionPane.showMessageDialog(null, "My name is Justin");
  }
}

__________________________________________________________________
    New class converting string to integer

//Write a java program to display a box where you type your age

import javax.swing.JOptionPane;
public class PrintAge
{
public static void main (String [] args)
  {
    String name;
    String ageString;
    int ageInt;
    JOptionPane.showMessageDialog(null, "What's your age?");

    age = JOptionPane.showInputDialog( "Type your age");
    ageInt = Integer.parseInt(ageString);
    JOptionPane.showMessageDialog(null, "Your age is " + ageString + "/nNext year your will be ") + (ageInt+1));
  }

_______________________________________________________________
Take a letter from a word
//do char (name) by strings, and do (name) = ~the variable~
// charAt(#)
import javax.swing.JOptionPane;
import java.util.Scanner;

public class Info
{
public static void main (String [] args)
  {
    Scanner key = new Scanner (System.in);
    String first;
    String middle;
    char mName;
    String last;

    System.out.println("Enter your first name:");
    first = key.nextLine();

    System.out.println("Enter your middle name:");
    middle = key.nextLine();
    mName = middle.charAt(0);

    System.out.println("Enter your last name:");
    last = key.nextLine();

    JOptionPane.showMessageDialog(null,"My name is " + first + " " + mName + ". " + last);

  }
}

If Statements
import javax.swing.JOptionPane;
import java.util.Scanner;

public class IfStatements
{
public static void main (String [] args)

  {
    Scanner input = new Scanner (System.in);
int grade1, grade2,
double average;

    System.out.println("Enter grade 1");
    grade1 = input.nextInt();

    System.out.println("Enter grade 2");
    grade2 = input.nextInt();

    average = (grade1 + grade2)/2;



if (average >= 60){
    System.out.println("You Passed with an " + average + "!");
    JOptionPane.showMessageDialog(null,"You Passed with an " + average + "!");
                                 }
else {
    System.out.println("You Failed");
    JOptionPane.showMessageDialog(null,"You Fail :(");
           }

  }
}

___________________________________________________

Time Calculator

import java.util.Scanner;

public class TimeCalculator
{
public static void main (String [] args)
  {
    Scanner input = new Scanner (System.in);
    int seconds, minutes, hours, days, minute, hour, day;


    System.out.println("Enter a number of seconds");
    seconds = input.nextInt();

    minutes=seconds/60;
    hours=seconds/3600;
    days=seconds/86400;

    if (seconds >= 60){
    System.out.println(" There are " + minutes + " minutes in that many seconds");
    }

    if (seconds >= 3600) {
    System.out.println(" There are " + hours + " hours in that many seconds");
    }

    if (seconds >= 86400){
    System.out.println(" There are " + days + " days in that many seconds");
    }
  }
}


import java.util.Scanner;
public class students
   {
      public static void main (String args [] )
      {
         Scanner input = new Scanner (System.in);
         int amount, students = 1;
          String name;
           System.out.println ("How many students are in the          class?");       
             amount = input.nextInt();
             while (students <= amount)
              {
                System.out.println("What is your name");
                name = input.nextLine();
                  input.nextLine();
                 System.out.println("Hello " + name);
              students = students + 1;
                            }
              }
}

Webs
Better Websites Made SimpleCreate your own free website today