Thinking in a Simple Way


Category Archives: Java

JAVA inheritance(IS a & HAS a relationship)

Closely related classes could be arranged using two types of relationship:

  • is a “, the inheritance relationship, A is a type of B.
  • has a “, the composition relationship, A has a B.

If A and B are of the same type, but A is more specific and B is more general, A shares all the properties/behaviours of B, we can use the “is a” relationship, e.g A extends B.

public class salariedEmployee extends Employee


// contents


we can say salariedEmployee is a Employee

or salariedEmployee is a type of Employee

when a class implements an interface, the same is-a relationship provided by inheritance applies.

for example,

public abstract class Employee implements Payable



Payable is an interface,

we can say Employee is a  Payable. or Employee is a type of Payable.

just as we can

assign the object reference to the superclass type as:

// in
 Account sister = new SAccount("s345","Judy Brown",1000,200);
sister.deposit(500); // superclass method

sister.withdraw(100);// overridden method
we can also assign the reference of a subclass object to an interface Type(variable).
 Payable sister = new SalariedEmployee("dd");

abstract class and interface (java)

An abstract class is to provide an appropriate super-class from which other classes can inherit and thus share a common design.

An abstract can have a set of concrete methods and abstract methods.

Classes can be used to instantiate objects are called concrete classes.

Interface means that Java offers a capability that requires that unrelated classes implement a set of common method.

An interface only contains constants and abstract methods.

unlike classes, all interface elements must be public, and interface may not specify any implementation details.

If a new method is added into an interface, you must perform or add this method to every classes that implement that interface.

A class that does not implement all the methods of the interface is an abstract class and must be declared abstract.

An interface is typically used when unrelated classes need to share common methods and constants. This allows objects of unrelated classes to be processed polymorphically- objects of classes that implement the same interface can respond to the same method calls.

*if an  abstract class implements an interface, we do not need to declare  the interface methods

as abstract in the abstract class,  they are already implicitly declared as such in the interface.

any concrete subclass of the abstract class must implement the interface methods to fulfill the superclass’s contract with the compiler.




java 循环语句for(int a:list)冒号的意思

int[] list = new int [15];  //该数组有15个元素,从0个元素开始一直到第14个结束
for(int index =0; index<15;index++)
list[index]= index*5;  //编辑一个简单的乘法公式,把所有的15个数分别乘进去;
for(int a : list)
System.out.print(a + " ");


0 5 10 15 20 25 30 35 40 45 50 55 60 65 70
【0】= 0 ×5 =0
【1】=1*5 = 5
【2】=2*5 = 10

Substraction single digits integers

import javax.swing.JOptionPane;

public class abc {

public static void main (String[] args){

int correctNumber = 0;
int questionNumber = 0;

int number1 = (int)(Math.random() * 10)+1;
int number2 = (int)(Math.random ()* 10)+1;

if (number1<number2){
int temp = number1;

String questionA = JOptionPane.showInputDialog("what is the result of "+ number1 + "-"+ number2+"?");
int answer = Integer.parseInt(questionA);

String replystring;
if (number1-number2==answer){
replystring="The answer is correct" ;


replystring="the answer is wrong";

JOptionPane.showMessageDialog( null, replystring);


How to use Java Math.random() method

The Math.random() method returns random double numbers in the range >=0.0 to <1.0 . It returns the same result as the Random nextDouble() method (see below).

double x;
x = Math.random(); // assigns random number to x

Usually the range 0.0…0.999999+ isn't what is desired so it's necessary to scale the range by multiplying and translate the values by addition. Finally, an int is commonly desired, so casting is required.

For example, if you need an int int the range 1 to 10, the following code could be used.

int n = (int)(10.0 * Math.random()) + 1;

The multiplication scales the range to 0.0 to 9.9999+, casting it to int to gets it into the integer range 0 to 9 (truncation, not rounding), then addition of 1 translates it into the range 1 to 10.

The java.util.Random class has convenient methods which do this extra work for you, but you have to create a Random object and make sure it's accessible where you need it (sometimes awkward).

Nested Loops

When we want to use nested loops, this can be depicted by the following syntax:

int i=0

int j=0
loops content



public class loop001 {
public static void main(String[] args) {
int i;
int j;
while (i<4)
while( j<3 )