Java

Table of Contents

1. Programming Foundations

1.1. Hello World

package helloworld;

/**
 *
 * @author ethanlin
 */
public class HelloWorld {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        System.out.println("Hello Java World");
    }

}

2. Java Programming

2.1. Random Number Generator

import java.util.Random;

public class DieSimulator {

    public static void main(String[] args){
        Random generator = new Random();
        System.out.println(generator.nextInt(100)+1);
    }

}

2.2. Employ Class

2.2.1. class

public class Employee {
    private String name;
    private double salary;

    public Employee(String employeeName, double currentSalary)
    {
        this.name = employeeName;
        this.salary = currentSalary;
    }
    public String getName(){
        return name;
    }
    public double getSalary()
    {
        return salary;
    }
    public void raiseSalary(double percent)
    {
        salary += salary*percent/100;
    }

}

2.2.2. testing

package employeePackage;

/**
 *
 * @author ethanlin
 */
public class EmployeeTester {
    public static void main(String[] args)
    {
        Employee harry = new Employee("Harry Hacker",5000);
        harry.raiseSalary(10);
        System.out.println(harry.getName());
        System.out.println("Expected: Harry Hacker");
        System.out.printf("%.2f\n",harry.getSalary());
        System.out.println("Expected: 55000.00");
    }
}



print("Hello World")

Date: 2022-03-09 Wed 00:00

Author: Dr YF Lin

Created: 2022-04-07 Thu 16:52

ThingsEngine