Docsity

Basic Java Commands Examples, Study notes of Java Programming

Toggle sidebar
bg1
Basic Java Commands Examples
SphereProperties is a program that will take as input the radius of a sphere and output the volume and surface area of the
sphere.
import java.util.Scanner;
public class SphereProperties {
/**
* SphereProperties prints out the volume and surface area of
* a sphere given its radius.
* Author: Don Spickler
* Date: 1/31/2011
*/
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Input the radius of the sphere: r = ");
double radius = keyboard.nextDouble();
double area = 4*Math.PI*Math.pow(radius, 2);
double volume = 4/3*Math.PI*Math.pow(radius, 3);
System.out.printf("In a sphere of radius %.4f, the volume is %.4f and the
surface area is %.4f.", radius, volume, area);
}
}
CylinderVolume is a program that will take as input the radius and height of a cylinder and output its volume.
import java.util.Scanner;
public class CylinderVolume {
/**
* CylinderVolume prints out the volume of
* a cylinder given its height and radius.
* Author: Don Spickler
* Date: 1/31/2011
*/
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Input the radius of the cylinder: r = ");
double radius = keyboard.nextDouble();
System.out.print("Input the height of the cylinder: h = ");
double height = keyboard.nextDouble();
double volume = Math.PI*Math.pow(radius, 2)*height;
System.out.printf("A cylinder of radius %.4f, and height %.4f has volume
%.4f.", radius, height, volume);
}
}
Introductory Java Examples
1 of 165.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download Basic Java Commands Examples and more Study notes Java Programming in PDF only on Docsity!

Basic Java Commands Examples

SphereProperties is a program that will take as input the radius of a sphere and output the volume and surface area of the

sphere.

import java.util.Scanner;

public class SphereProperties {

* SphereProperties prints out the volume and surface area of

* a sphere given its radius.

* Author: Don Spickler

* Date: 1/31/

public static void main(String[] args) {

Scanner keyboard = new Scanner(System. in );

System. out .print("Input the radius of the sphere: r = ");

double radius = keyboard.nextDouble();

double area = 4*Math. PI *Math. pow (radius, 2);

double volume = 4/3*Math. PI *Math. pow (radius, 3);

System. out .printf("In a sphere of radius %.4f, the volume is %.4f and the

surface area is %.4f.", radius, volume, area);

CylinderVolume is a program that will take as input the radius and height of a cylinder and output its volume.

import java.util.Scanner;

public class CylinderVolume {

* CylinderVolume prints out the volume of

* a cylinder given its height and radius.

* Author: Don Spickler

* Date: 1/31/

public static void main(String[] args) {

Scanner keyboard = new Scanner(System. in );

System. out .print("Input the radius of the cylinder: r = ");

double radius = keyboard.nextDouble();

System. out .print("Input the height of the cylinder: h = ");

double height = keyboard.nextDouble();

double volume = Math. PI Math. pow (radius, 2)height;

System. out .printf("A cylinder of radius %.4f, and height %.4f has volume

%.4f.", radius, height, volume);

Basic Java Statements

import java.util.Scanner;

public class InputExamples {

* InputExamples --- Example of different input types and data types.

* Author: Don Spickler

* Date: 2/2/

public static void main(String[] args) {

Scanner keyboard = new Scanner(System. in );

System. out .print("Input an integer: ");

int num1 = keyboard.nextInt();

System. out .println(num1);

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

double num2 = keyboard.nextDouble();

System. out .println(num2);

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

byte num3 = keyboard.nextByte();

System. out .println(num3);

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

long num4 = keyboard.nextLong();

System. out .println(num4);

num1 = num4;

num3 = num4;

num2 = num4;

num4 = num1;

num4 = num3;

num4 = num2;

Run:

Input an integer: 25

Input a double: 236. 35

Input a byte: 12

Input a long: 1234 56

public class MathExamples {

* MathExamples --- just some calculations.

* Author: Don Spickler

* Date: 2/2/

public static void main(String[] args) {

double num1 = Math. random ();

System. out .println("Random Number: " + num1);

System. out .println("e = " + Math. E );

System. out .println("pi = " + Math. PI );

System. out .println("floor(27.8) = " + Math. floor (27.8));

System. out .println("floor(17.0) = " + Math. floor (17.0));

System. out .println("floor(-7.6) = " + Math. floor (-7.6));

System. out .println("sqrt(2) = " + Math. sqrt (2));

System. out .println("sqrt(16) = " + Math. sqrt (16));

System. out .println("1/3 = " + (1/3));

System. out .println("1.0/3.0 = " + (1.0/3.0));

System. out .println("1.0/3.03.0 = " + (1.0/3.0)3.0);

System. out .println("max(23, 102) = " + Math. max (23, 102));

System. out .println("min(23, 102) = " + Math. min (23, 102));

Run:

Random Number: 0.

e = 2.

pi = 3.

floor(27.8) = 27.

floor(17.0) = 17.

floor(-7.6) = -8.

sqrt(2) = 1.

sqrt(16) = 4.

max(23, 102) = 102

min(23, 102) = 23

public class SpecialIntegerFunctions {

* StringExample --- familiar to formal name.

* Author: Don Spickler

* Date: 2/2/

public static void main(String[] args) {

int int1 = 7;

long long1 = 700;

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

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

int1++;

long1--;

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

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

int1-=5;

long1+=10;

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

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

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

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

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

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

System. out .println("int1 = " + --int1);

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

System. out .println("4int1++ = " + 4int1++);

System. out .println("4int1 = " + 4int1);

long1*=20;

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

long1/=5;

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

Run:

int1 = 7

long1 = 700

int1 = 8

long1 = 699

int1 = 3

long1 = 709

int1 = 3

int1 = 4

int1 = 5

int1 = 5

int1 = 4

int1 = 4

4*int1++ = 16

4*int1 = 20

long1 = 14180

long1 = 2836

public class DataTypes002 { public static void main(String[] args) { // Overloading and underloading int c = 299792458; System. out .println("c = " + c); System. out .println("cc = " + cc); long lc = 299792458; System. out .println("lc = " + lc); System. out .println("lclc = " + lclc); System. out .println(); byte b = 120; for ( int i = 0; i < 10; i++) { b++; System. out .println(b); } System. out .println(); float fl = 1000000000000000000000000000000f; for ( int i = 0; i < 10; i++) { fl=10; System. out .println(fl); } System. out .println(); double db = Math. pow (10, 300); for ( int i = 0; i < 10; i++) { db=10; System. out .println(db); } System. out .println(); db = Math. pow (10, -320); for ( int i = 0; i < 5; i++) { db/=10; System. out .println(db); } } } c = 299792458 cc = - lc = 299792458 lclc = 89875517873681764 121 122 123 124 125 126 127

1.0E 1.0E 1.0000001E 1.00000004E 1.0E 1.00000004E 1.00000006E 1.0000001E Infinity Infinity 1.0E 1.0E 1.0E 1.0E 1.0E 9.999999999999999E 9.999999999999999E 9.999999999999998E Infinity Infinity 1.0E- 1.0E- 1.0E-

public class DataTypes003 { public static void main(String[] args) { // Characters char c = 't'; System. out .println(c); for ( int i = 0; i < 10; i++) { c++; System. out .println(c); } System. out .println(); System. out .println("ASCII Table"); c = 0; for ( int i = 1; i < 256; i++) { c++; System. out .print(c + " "); if (i % 10 == 0) System. out .println(); } System. out .println(); System. out .println(); c = '"'; System. out .println(c); c = '"'; System. out .println(c); c = '''; System. out .println(c); System. out .println("ASCII Table Again"); c = 0; for ( int i = 1; i < 256; i++) { c++; System. out .print(c + "\t"); if (i % 10 == 0) System. out .print("\n"); } } }

import java.util.Scanner; public class DataTypes004 { public static void main(String[] args) { // Reference Types Scanner keyboard = new Scanner(System. in ); System. out .print("Input an integer: "); int t = keyboard.nextInt(); /* Scanner keyboard2 = null; System.out.print("Input an second integer: "); int t2 = keyboard2.nextInt(); */ } } public class DataTypes005 { public static void main(String[] args) { // String Examples String str1 = "This is a test."; char ch1 = str1.charAt(0); System. out .println(ch1); System. out .println(str1.charAt(0)); System. out .println(str1.length()); System. out .println(); for ( int i = 0; i < str1.length(); i++) { System. out .println(str1.charAt(i)); } System. out .println(); String str2 = "this is a test."; System. out .println(str1.equals(str2)); System. out .println(str1.equalsIgnoreCase(str2)); System. out .println(); String str3 = String. valueOf (3.14159); System. out .println(str3); System. out .println(); System. out .println(str1.indexOf("i")); System. out .println(str1.indexOf("i", 3)); System. out .println(str1.indexOf("i", 7)); System. out .println(); System. out .println(str1.lastIndexOf("i")); System. out .println(str1.lastIndexOf("i", 3)); System. out .println(str1.lastIndexOf("i", 7)); System. out .println(str1.lastIndexOf("i", 1)); System. out .println(); System. out .println(str1.indexOf("is")); System. out .println(str1.indexOf("is", 3)); System. out .println(str1.indexOf("that")); System. out .println(); str1 = "cat"; str2 = "dog"; System. out .println(str1.compareTo(str2)); System. out .println(str1.compareTo(str1)); System. out .println(str2.compareTo(str1)); System. out .println(); str1 = "cat"; str2 = "Dog";

T
T

T h i s i s a t e s t. false true

2 5

5 2 5

2 5

0 1

System. out .println(str1.compareTo(str2)); System. out .println(str1.compareTo(str1)); System. out .println(str2.compareTo(str1)); System. out .println(); System. out .println(( int )('c') + " " + ( int )('D')); System. out .println(); System. out .println(str1.compareToIgnoreCase(str2)); System. out .println(str1.compareToIgnoreCase(str1)); System. out .println(str2.compareToIgnoreCase(str1)); System. out .println(); str1 = str1.concat(str2); System. out .println(str1); str1 = "cat"; str2 = "Dog"; str1 = str2 + str1; System. out .println(str1); System. out .println(); System. out .println(str1.endsWith("at")); System. out .println(str1.endsWith("ta")); System. out .println(); System. out .println(str1.startsWith("Dog")); System. out .println(str1.startsWith("dog")); System. out .println(); System. out .println(str1.isEmpty()); System. out .println(); System. out .println(str1); System. out .println(str1.toLowerCase()); System. out .println(str1.toUpperCase()); System. out .println(str1); str1 = str1.toUpperCase(); System. out .println(str1); System. out .println(); str1 = "This is a test string for testing the replace command."; System. out .println(str1); str1.replaceAll("test", "short"); System. out .println(str1.replaceAll("test", "short")); System. out .println(str1); System. out .println(str1.replaceFirst("test", "short")); System. out .println(str1); System. out .println(); System. out .println(str1.substring(5, 7)); System. out .println(str1.substring(15, 20)); System. out .println(str1.substring(15, 21)); System. out .println(str1.substring(15)); System. out .println(str1); System. out .println(); str1 = " This is a trim test "; System. out .println(str1.trim() + "*****"); } }

catDog Dogcat true false true false false Dogcat dogcat DOGCAT Dogcat DOGCAT This is a test string for testing the replace command. This is a short string for shorting the replace command. This is a test string for testing the replace command. This is a short string for testing the replace command. This is a test string for testing the replace command. is strin string string for testing the replace command. This is a test string for testing the replace command. This is a trim test*****

public class DataTypes008 { public static void main(String[] args) { // More random numbers for ( int i = 0; i < 10; i++) System. out .println(Math. random ()); for ( int i = 0; i < 10; i++) System. out .println(( int )(Math. random ()*7) + 5); } }

6 9 10 11 6 9 8 7 6 6

Boolean Expression Examples

public class BoolanExpressions {

public static void main(String[] args) {

boolean b1 = true ;

boolean b2 = false ;

boolean b3 = true ;

int x = 2;

int y = 3;

int z = 4;

System. out .println(b1);

System. out .println(b2);

System. out .println();

System. out .println(2 == 4);

System. out .println(x == 4);

System. out .println(z == 4);

System. out .println(x == z);

System. out .println(x != z);

System. out .println();

System. out .println(z < y);

System. out .println(z > z);

System. out .println(z >= z);

System. out .println();

System. out .println(!b1);

System. out .println(!b2);

System. out .println();

System. out .println(b1 && b2);

System. out .println(b1 && b3);

System. out .println(b1 && !b2);

System. out .println();

System. out .println(b1 || b2);

System. out .println(b1 || b3);

System. out .println(!b1 || b2);

System. out .println();

System. out .println((x > 1) && (z < 7));

System. out .println((x > 1) && (z < 4));

System. out .println((x > 1) || (z < 4));

System. out .println();

System. out .println((x % 2 == 0) && (y % 2 == 0));

System. out .println((x % 2 == 0) && (z % 2 == 0));

System. out .println();

System. out .println(((x % 2 == 0) && (z % 2 == 0)) || b2);

System. out .println(((x % 2 == 0) && (z % 2 == 0)) && b2);

System. out .println(((x % 2 == 0) && (z % 2 == 0)) && b3);

System. out .println();

System. out .println(b1 ^ b3);

System. out .println(b1 ^ b2);

System. out .println();

System. out .println( true ^ true );

System. out .println( true ^ false );

System. out .println( false ^ true );

System. out .println( false ^ false );

System. out .println();

System. out .println(2 ^ 5);

System. out .println(23 ^ 15);

System. out .println(42 ^ 49);

true

false

false

false

true

false

true

false

false

true

false

true

false

true

true

true

true

false

true

false

true

false

true

true

false

true

false

true

false

true

true

false

import java.util.Scanner;

* Conditional Example

* Author: Don Spickler

* Date: 2/6/

public class ConditionalExample {

public static void main(String[] args) {

Scanner keyboard = new Scanner(System. in );

System. out .print("How many eggs do you have? ");

int numEggs = keyboard.nextInt();

if (numEggs == 12){

System. out .println("You have a dozen eggs.");

else if (numEggs < 12){

System. out .println("You have fewer than a dozen eggs.");

else {

System. out .println("You have more than a dozen eggs.");

import java.util.Scanner;

* Conditional Example

* Author: Don Spickler

* Date: 2/6/

public class ConditionalExample {

public static void main(String[] args) {

Scanner keyboard = new Scanner(System. in );

System. out .print("How many eggs do you have? ");

int numEggs = keyboard.nextInt();

if (numEggs == 12){

System. out .println("You have a dozen eggs.");

else if (numEggs == 2){

System. out .println("You have a couple eggs.");

else if (numEggs <= 7){

System. out .println("You have a few eggs.");

else if (numEggs < 12){

System. out .println("You have several eggs.");

else {

System. out .println("You have more than a dozen eggs.");

import java.util.Scanner;

* Conditional Example

* Author: Don Spickler

* Date: 2/6/

public class ConditionalExample {

public static void main(String[] args) {

Scanner keyboard = new Scanner(System. in );

System. out .println("Please select from the following menu:");

System. out .println("1. Rectangle Properties");

System. out .println("2. Circle Properties");

System. out .println();

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

int menuOption = keyboard.nextInt();

if (menuOption == 1){

System. out .print("Input the width of the rectangle: ");

double width = keyboard.nextDouble();

System. out .print("Input the height of the rectangle: ");

double height = keyboard.nextDouble();

double area = height * width;

double perimeter = 2height + 2width;

System. out .println("The area of the rectangle is " + area);

System. out .println("The perimeter of the rectangle is " + perimeter);

else if (menuOption == 2){

System. out .print("Input the radius of the circle: ");

double radius = keyboard.nextDouble();

double area = Math. PI * Math. pow (radius, 2);

double circumference = 2 * Math. PI * radius;

System. out .println("The area of the circle is " + area);

System. out .println("The circumference of the circle is " + circumference);

else {

System. out .println("Invalid Menu Selection!");

While Loop Examples

import java.util.Scanner;

* While Loop Example

* Author: Don Spickler

* Date: 2/6/

public class WhileLoopExample {

public static void main(String[] args) {

Scanner keyboard = new Scanner(System. in );

System. out .print("Input the maximum number to square: ");

int maxNum = keyboard.nextInt();

int currentNum = 1;

while (currentNum <= maxNum)

int square = currentNum * currentNum;

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

currentNum = currentNum + 1;

System. out .println();

* While Loop Example

* Author: Don Spickler

* Date: 2/6/

public class WhileLoopExample {

public static void main(String[] args) {

int currentNum = 1;

int sum = 0;

while (currentNum <= 100)

sum = sum + currentNum;

currentNum = currentNum + 1;

System. out .println("The sum of the numbers from 1 to 100 is " + sum + ".");

import java.util.Scanner;

* While Loop Example

* Author: Don Spickler

* Date: 2/6/

public class WhileLoopExample {

public static void main(String[] args) {

Scanner keyboard = new Scanner(System. in );

System. out .print("Input n: ");

int n = keyboard.nextInt();

int currentNum = 1;

int sum = 0;

int sumSquare = 0;

int sumCube = 0;

while (currentNum <= n)

sum = sum + currentNum;

sumSquare = sumSquare + currentNum*currentNum;

sumCube = sumCube + currentNumcurrentNumcurrentNum;

currentNum = currentNum + 1;

System. out .println("The sum of the numbers from 1 to " + n + " is " + sum + ".");

System. out .println("The sum of the squares of the numbers from 1 to " + n + " is " +

sumSquare + ".");

System. out .println("The sum of the cubes of the numbers from 1 to " + n + " is " +

sumCube + ".");

import java.util.Scanner;

* While Loop Example

* Author: Don Spickler

* Date: 2/6/

public class WhileLoopExample {

public static void main(String[] args) {

Scanner keyboard = new Scanner(System. in );

int currentNum = 0;

int sum = 0;

while (currentNum != -1)

sum = sum + currentNum;

System. out .print("Input the next positive number to add (-1 to quit): ");

currentNum = keyboard.nextInt();

System. out .println("The sum of the input numbers is " + sum + ".");