Thinking in a Simple Way

搭好梯子再上来 https://simpleface.wordpress.com

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 Test.java
 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接口所规定的方法的类都可以具有这个接口的类型,而一个类可以实现任意多个Java接口,从而这个类就有了多种类型

 

 

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 + " ");

冒号指迭代,即不断从数组的List里取其中的元素,
赋值给临时变量a,从JDK1.5开始,冒号就可以实现很方便的读取List里的内容了。
输出结果如下:

0 5 10 15 20 25 30 35 40 45 50 55 60 65 70
这15个数组的变化规律
【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;

while(questionNumber<10){
int number1 = (int)(Math.random() * 10)+1;
int number2 = (int)(Math.random ()* 10)+1;

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

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" ;

correctNumber++;
}
else

replystring="the answer is wrong";

JOptionPane.showMessageDialog( null, replystring);

questionNumber++;

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
while(i<times)
{

int j=0
while(j<3)
{
loops content
j=j+1;
}

i=i+1;

}

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

{

System.out.print("j");
System.out.print("*");
j=j+1;

}
System.out.print("");
i=i+1;

}

}

}