Introduction to Java

Java Programming is concisely explained.

Introduction to Java: Table of Contents

 

  1. Java: String Catenation
  2. Java: Array
  3. Java: Understanding Classes
  4. Java: Access from a Different Class within a Package
  5. Java: Static and Non-static Members
  6. Java: Constructor
  7. Java: Passing Arguments to Method
  8. Java: Inheritance
  9. Java: toString() method
  10. Java:Abstract Class
  11. NHK攻略英語スニングの復習
  12. 英語で行う授業
  13. 本能寺の変431年目の真実
  14. 東京オリンピックロゴ:市松模様などの研究 in English and Japanese

Go to Table of Contents

10. Abstract Class

In designing a number of classes, you may find there are some common elements among them. To extract the common and make it a class is to create a super class. The individual parts of the original classes become subclasses. In cases we do not need instances of the super class, we should make it abstract.
An abstract class is a class which has at least one abstract method. An abstract class is expected to become a super class and the abstract method is expected to be overridden in subclasses. An abstract class must be declared with “abstract” modifier. It cannot create an instance, however, may have a constructor. An abstract method is a method declared with “abstract” with method signature only.

Here is a sample program:
In this program, the common part for ProBook and TextBook comprises the abstract class Book, which is composed of title, page, constructor Book, and the abstract method display(). Each individual part of the subclasses composes ProBook and TextBook. ProBook is consist of the field category, the constructor ProBook, and the method display(); and TextBook is made of the filed school, the constructor TextBook, and the method of display().

AbstractClass.java

abstract class Book{
	String title;
	int page;
   public Book(String title,int page) {
     this.title = title;this.page=page;
   }
	abstract void display();
}
class ProBook extends Book{
   private int category;
   public ProBook(String title, int page, int category){
		super(title,page);this.category=category;
   }
   public void display(){
		System.out.println(" [ProBook: "+title+","+page+","+category+"]");
	}
}
class TextBook extends Book{
   private int school;
   public TextBook(String title, int page, int school){
		super(title,page);this.school=school;
   }
   public void display(){
		System.out.println(" [TextBook: "+title+","+page+","+school+"]");
   }
}
class AbstractClass{
   public static void main(String args[]){
		Book a,b;
		a=new ProBook("Introduction to C++",300,5);
		b=new TextBook("Beginner's Java",400,3);
		a.display();b.display();
   }
}
// [ProBook: Introduction to C++,300,5]
// [TextBook: Beginner's Java,400,3]

Go to Table of Contents

9. Java: toString() method

toString() is a special instance method that returns a string particular to the instance. Whenever the string is demanded, toString() is called and provides the string. The situation the particular string is demanded is like being argument of println() method. Since toString() is a public method of Object class, the overriding method must be public. Here is a sample program.

ToString.java

class A {
	private String name;
	A(String name){this.name=name; }
	public String toString(){
		return "["+name + "]";
	}
}
class B extends A{
	private int x,y;
	B(String s, int x, int y){super(s);this.x=x; this.y=y;}
	public String toString(){
		return super.toString()+"("+x+","+y+")";
	}
}
class ToString {
	public static void main(String[] arts){
		A a =new A("AAA");
		System.out.println(a);
		B b = new B("BBB",5,10);
		System.out.println(b);
	}
}
/*
[AAA]
[BBB](5,10)
*/

Go to Table of Contents

8. Java: Inheritance

8. Java: Inheritance
Inheritance is to derive a class from another class. It is to create a class by making use of properties of another class. The derived class is called a subclass and the parent class is called a superclass. Constructors and private members are not inherited. The following is a sample coding of inheritance:
class A extends B {
………….
}
Overriding instance methods
As told above, methods can be inherited. If a method has the same signature (name, arguments, and return modifier) in both of super and sub classes, it is said that the method is overridden. With the same signature, which method to execute is seemingly confusing? It is, clear, however. The one of the instance is executed. Here is a sample program.

Overriding class methods:
Overridden class methods are differentiated by the class name. Therefore, usually no problem exists.

Inheritance of constructor
It is not possible or not defined.
Since a constructor has the same name of the class, the subclass and the superclass have to have the same name. Therefore it is not allowed.

Here is a sample program for Inheritance:
Inheritance.java

class A {
	int x;
	A(int x){this.x=x;}
	void display(){System.out.println("display x="+x);}
	void show_me() {
		System.out.println("This is show_me() of class A x="+x);
	}
}
class Inheritance extends A {
	String name;
	Inheritance(String n){
		super(20); this.name=n;
	}
	void show_me() {
		System.out.println("This is show_me() of class Inheritance name="+name + " x="+ x);
	}
	public static void main(String[] arguments) {
		A a = new A(10);		 
		Inheritance b = new Inheritance("AIT");
		a.display(); 	b.display();
		a.show_me();b.show_me();
	}
}
/* result
display x=10
display x=20
This is show_me() of class A x=10
This is show_me() of class Inheritance name=AIT x=20
*/

Class A is inherited to Inheritance.
Line 12: super(29) set x in A to 20. The constructor is not inherited.
Line 14: This show_me() is the overridden one. “name” is the newly added field but “x” here is the inherited one.
Line 18: new A(10) is the call to the super class constructor.
Line 19: new Inheritance(“AIT”) is the call for the constructor of the class Inheritance.
Line 20: a.display() is the call to the method of A.
b.display() is the cal to the inherited method of Inheritance.
Line 21 a.show_me() is the call to the method of A.
B.show_me() is the call to the overridden method.

Important Points:
There are three kinds of method execution concerning inheritance as:

1)Call to a method of super class (a.display, a.show_me())
2)Call to a method inherited but same as the one of the super class (b.display)
3)Call to a method overridden (b.show_me())

Go to Table of Contents

7. Passing Arguments to Method

There are two ways for parameter passing between the method calling side and the called. One is called “by value” and the other is “by reference”. The former is applied to primitive data types, the latter to objects.
Look at the sample program of PassArg.java. When the method toAAA is called, the data of the variable x, a primitive type, is first copied to the argument xx of the method; and the reference to the array y of main method is also copied to yy of the method. Then xx is changed to 0 in the method; and all the string data referenced by yy is changed to “AAA”. And the control is returned to the calling side. Now in the main method, x is still 8. Generally, modification to primitive type argument variable in called side has no effect to calling side. However, the contents of yy are changed to all “AAA”. This means modifications to referenced objects are persistent outside of the method.

Here is the sample program: PassArgs.java

class PassArgs {
	static void toAAA(int xx,String[] yy) {
		System.out.println("At the beginning of method:");
		System.out.println("xx="+xx);
		for (int i = 0; i < yy.length; i++) {System.out.print(yy[i] + " ");}
		xx=0;
		for (int i = 0; i < yy.length; i++) { yy[i]="AAA";}
	}
	public static void main(String[] arguments) {
		int x=8;
		String[] y = {"XX", "YY", "ZZ"};
		toAAA(x,y);
		System.out.println("\nAfter call to method:");
		System.out.println("x="+x);
		for (int i = 0; i < y.length; i++) {System.out.print(y[i] + "  ");}
	}
}
/*
At the beginning of method:
xx=8
XX YY ZZ
After call to method:
x=8
AAA  AAA  AAA
*/

Go to Table of Contents

6. Constructor

Constructor is a special method which is more like an instance method than a class method, for it is related with instance. Constructor can access all of instance fields and methods and static fields and methods. However, it can run without instance, a feature of static method. Constructor’s major features are:
1)Its name is always same as the class name.
2)It is used to create an instance of the class.
3)It has no return value.
4)If a constructor is executed in a constructor, it must be done at the top. This is done by this(), or this(,,,), whatever is defined. Even if a constructor is executed in a constructed, no new instance is created in a instance.
5)One no-parameter constructor is automatically and implicitly defined if no constructor is explicitly defined.
6)A constructor initializes instance and class variables. If there is no explicit initialization of fields, those are set as follows: 0 for numbers; null for objects; false for boolean and ‘\0’ for character.

Here are examples:
Constructor.java: to show how consturctor is used.

 class ClassA{//has two explicit constructors---one with parameters and the other without them 
	private int m;
	private double n;
	ClassA(int m, double n){//Constructor's name is the same as that of the class
		this.m=m; this.n=n;
		//NO return statement
	}
	ClassA(){
		this(1,1.0);//Calling a constructor must be at the top of constructor.
		System.out.println("Calling constructor must be at the top of constructor.");
	}
}
class Constructor {
	public static void main(String args[]){
		ClassA c1 = new ClassA(1,1.0);//Constructor is called to make an instance. 
		System.out.println();
		ClassA c2 =new ClassA();
	}
}

Constructor2.java: to show how primitives are initialized in a constructor.

class ClassB{//has no explicit constructor
   int a;
	short b;
	double c;
	boolean d;
	String e;
	char f;
	void show(){
		System.out.println("int a="+a);
		System.out.println("short b="+b);
		System.out.println("double c="+c);
		System.out.println("boolean d="+d);
		System.out.println("String d="+e);
		if(f=='\0') System.out.println("char f=\'\\0\'");
	}
}
class Constructor2 {
   public static void main(String args[]){
      ClassB c = new ClassB();//implicitly defined constructor
		c.show();
   }
}
//int a=0
//short b=0
//double c=0.0
//boolean d=false
//String d=null
//char f='\0'

Go to Table of Contents

5. Static and Non-static Members

There are two kinds of member in java class. One is called static member, the other is non-static member.
Static member includes static field and static method:
Static members are with "static" modifier. Those are said to belong to the class where they are declared. Unlike non-static members, static members exist once the class is declared. So, static method can be executed without instance. Accordingly, static fields can be accessed without instance. They are not related to instance but related to the class. From static methods, non-static fields nor non-static methods cannot be seen. Hence, static methods cannot access to non-static (instance) fields.
Non-static members are instance field and instance method:
Those members are without "static" modifier. They are said to belong to the instance created. Unlike static members, non-static members can exist only after the instance is created. So instance methods can be executed only after instance creation. And instance fields can be accessed only after the instance is created. They are related to the instance. From non-static methods, both of static and non-static members can be seen. Concept of “this” exists for instance members.

Summary of Static and Non-Static Members
  Static Member=Class Member Non-static Member=Instance Member
Modifier static without static
Members are class fields and methods instance fields and methods
Where to belong to Class Instance
When to run
the method It can run
without instance No instance needed to run the method
What can be accessed
by the method static members
and local variables static and non-static members
and local variables
Concept of this not exist exist

Here is a sample program for Static and Non-Static Members
StaticNonStatic.java:

class Book{
   private String title;
	private int n;
	private static int number_sold=0;
	Book(int n){
		this.n=n;number_sold++;
      System.out.println("Const: This Book is set to " + n + ".");
	}
   static void showNSold(){
      System.out.println("showNSold() number_sold="+number_sold);
   }
   void show(){
      System.out.println("show() Book n="+n+" number_sold="+number_sold);
		showNSold();
   }
}
class StaticNonStatic {
   public static void main(String args[]){
      Book.showNSold();
      Book c = new Book(200);
      Book.showNSold();
		c.show();
   }
}
/*
showNSold() number_sold=0
Const Book is set to 200.
showNSold() number_sold=1
show() Book n=200 number_sold=1
showNSold() number_sold=1
*/

Explanation:
The constructor Book is not static so can access both of static and non-static fields, n and title. The static method showNSold cannot access the non-static field n but can access the static field number_sold. The non-static method show can access both of the non-static field n and the static field number_sold.

Go to Table of Contents

4. Access from a Different Class within a package

A java program may consist of a number of packages.
A package may have a number of classes. For the time being, our program is made of only one package, which is nameless package.

Access Control is to control access to a member in a class. The control is given on two stages. One is on a class. The other is on a member.

This access control is provided by giving access modifiers to member and to class. For a class there are two kinds of modifier, public or no-modifier. For a member, there are four modifiers, public, no-modifier, protected and private. For the time being, we do not consider protected.

To access to a member in a class from other packages, both of class and member must be public. Access to a public member in a public class is always possible. It is possible from anywhere.

To access to a member in a class from a different class in the same package, class and member must be either with public or no-modifier. Access to a member in a class from within the same class is always possible. This means that to prohibit access to a member in a class from a different class, the member must be private.

Here is a sample program to demonstrate the access control.
B.java

class A{
	public int X;
	private int Y;
	void setY(int yy) {
		Y=yy;
	}
	void show(){
		System.out.println("X=" + X + "Y="+Y);
	}
}
class B{
	public static void main(String args[]) {
		A a1=new A();
		a1.X=1;
		//a1.Y=2;
		a1.setY(2);
		a1.show();
	}
}
/*
X=1Y=2
Since X in A is public, a1.X=1 is possible.
But Y in A is private, so a1.Y=2 is not allowed.
Therefore void setY(int yy) is made to set Y in A. 
*/

Go to Table of Contents

3. Java: Understanding classes

In Java, a program is made of one main class and some other classes. In java application program, which we are learning now, program execution starts with the main method of the main class.
As we have already studied, there are some basic data types such as int, double, char, float, and the like. Such types are templates to create data of java primitive data type. For examples, int creates integer data, double creates double data, and char creates char data, etc.
A class is a template which is made of some fields or some methods, or both. Fields are some data and methods are some programs or functions. Like int creates integer data, class creates a program element which is called object.

Here is a sample source program where the source file name is Start.java. In the program, there are two classes, Man and Start. The program starts with main of Start (main class). The source program name must be the same as the main class name. z1,and z2 are class variables made by the class Man. Objects are created by new Man(). Man() is called a constructor, which we will learn later. In the program, two objects are created and the variables z1 and z2 are assigned to each of the objects, respectively. The class Man has fields of name and height and the method show(). So both z1 and z2 are the references to each of those objects, respectively. Consequently, z1 and z2 are said to be the objects characterized by name and height and show().

Sample program: Class.java

class Man{
	String name;
	int height=0;
	void show(){
		System.out.println("name:"+ name + " height:" +height);
	}
}
class Class{
	public static void main(String args[]) {
		Man m1, m2;
		m1=new Man(); m1.name="香川"; m1.height=170; 
		m2=new Man(); m2.name="大島" ;m2.height=180;
		m1.show();
		m2.show();
	}
}
/* 
name:香川 height:170
name:大島 height:180
*/

Go to Table of Contents

Array

What is an array?
An array is a group of data of a same data type, or a same object.It can be one-dimensional, two dimensional, three-dimensional, and more. The data type can be int, double, char, float, an object, and the like. There are three steps to use an array. They are 1) declaration of array variable, making array object, and use arrays (move data into an array and get data from array).

Samples
int a; // a one dimensional array variable declaration.
a= new int[5]; //an array object of int with size 5 is created.
a[3] = 5; //int of 5 is stored in a[3].
Int b = a[3];
int a
; // another way to declare array variable

An array can be multi-dimensional. Here are some samples.
int c; c=new int[5][6]; c[2][3]=4; // tow-dimensional
char d[]; d=new char[5][4][5]; d[0][3][3]=‘a’;//three dimensional
As shown, access to an array is by indexing as:
For(int i=0;i<5;i++) a[i]=i*i;

Here is a sample program: Array.java

class Array{
   public static void main(String args[]){
		int a[][] = new int[9][9];
		for(int i=0;i<9;i++)
			for(int j=0;j<9;j++)
					a[i][j]=(i+1)*(j+1);
		System.out.println("   Multiplication Table");
		for(int i=0;i<9;i++){
			for(int j=0;j<9;j++)
				System.out.printf("%3d",a[i][j]);
			System.out.println();
		}
	}
}
/*
   Multiplication Table
  1  2  3  4  5  6  7  8  9
  2  4  6  8 10 12 14 16 18
  3  6  9 12 15 18 21 24 27
  4  8 12 16 20 24 28 32 36
  5 10 15 20 25 30 35 40 45
  6 12 18 24 30 36 42 48 54
  7 14 21 28 35 42 49 56 63
  8 16 24 32 40 48 56 64 72
  9 18 27 36 45 54 63 72 81
*/

Go to Table of Contents

Java language and String Catenation

 

Java is a relatively new type of computer programming language having features at least as
1.It is an object oriented language.
“Object oriented language” means that the program written by the language is based on modules called objects. The word “Object” means a thing, a collection of things, or any thing which is expressed in a program to means a unit of things on which the program is built. It is relatively easy to express things of the world by the program.
2.It is an interpreter type of language.
This type of language requires compiling a source program before it is executed. In case of java, the program produced by compiling is called a byte code. This code can be executed in an environment called Java Run time Environment (JRE). The byte code can be run anywhere JRE is prepared. So the Java program can be run on virtually any kind of computers. However, the execution time is usually longer than that of native code program.
3.The source program appearance is similar to that of C or C++.

The binary operator symbol “+” has two meanings. One is the arithmetic addition and the other is the string catenation. String catenation means attach one string to the other in a binary string operation.
Example1:
Stirng a = “abcde”;
String b = “123”;
String c = a+b;
System.out.printlin(c);
OUT PUT:
abcde123
Arithmetic addition is applied only when both operands are numeric. If one of the operands is numeric, the numeric number is converted to a string before catenation is applied.
Example2:
Stirng a = “abcde”;
int b = 24;
String c = a+b;
System.out.printlin(c);
OUT PUT:
abcde24

Here is a sample code: Catenation

class Catenation{
   public static void main(String args[]) {
		String a="xyz"; 
		String b="12"; 
		String ab=a+b;
		String aa=a+4; 
		String bb=5+a;
		String cc=a+1+2;
		String ac=a+(1+2);
 		System.out.println("ab=" + ab);
		System.out.println("aa=" + aa);
		System.out.println("bb=" + bb);
     	System.out.println("cc=" + cc +" (catenation)");
     	System.out.println("ac=" + ac + " (addition)");
   }
}
/*  result
ab=xyz12
aa=xyz4
bb=5xyz
cc=xyz12 (catenation)
ac=xyz3 (addition)
*/

Go to Table of Contents

英語学習ー本能寺の変

 

本能寺の変431年目の真実

私は2,3か月前から幾つかの英会話クラブまたは英会話Cafe(名古屋, 豊田)に参加しています。定年退職後の人生を探す試みの一つです。その中の一つではメンバーがトピックを決めて英語でしゃべります。以下はその時の私のスピーキングです。多少修正しています。余り上手ではない英語ですので、お気づきの方はコメントをください。訂正、修正などのコメントは大変ありがたいです。ブログを始めて一週間ぐらいで、使い方があまりよくわかっていません。お気づきのことがありましたら、ご指摘ください。メンバーの中でこのブログを見ている人がいたら、コメントは役に立つと思います。よろしくお願いします。私も、面白いトピックがあったらまた書きたいと思います。

About ten years ago, I read a book called “Honnouji no Hen - the truth after 427 years.” It was written by Akechi Kenzaburou, who is supposed to be a descendant of Akechi Mitsuhide. I read it and the revised about five times. It is a very surprising and astonishing book based on the great survey by the author. The following is according to it:
Before Honnouji no Hen, Oda Nobunaga had conquered the central Japan - most parts of Kinki and major parts of Chubu and was expected to conquer the entire Japan very soon.

Oda had a plan to kill Tokugawa because he was an obstacle for Oda to conquer and to govern Japan. So he invited Tokugawa to Azuchi, Kyoto, Osaka for sight seeing and then to Honnouji. He also directed Akechi, Oda’s most favorite subordinate, to attack Honnouji with the war troop and kill Tokugawa.
However, before Tokugawa arrived in Kyoto, Honnouji was attacked by Akechi and Oda was killed. It occurred very early in the morning June 2, 1582.  At that time Tokugawa was in Sakai and was on the way to Honnouji and noticed that the coup d‘etat broke out. Then he decided directly to go back home to Okazaki through Iga. The trip through Iga was said to be very difficult for Tokugawa. But, it was easy because Iga people were very kind and supportive to Tokugawa. The reason was that when Oda attacked Iga, many Iga people were killed but some escaped and were saved by Tokugawa.

この英会話クラブはワンコイン英会話と呼ばれ、豊田市ホープチャペルという教会の一室で毎週開かれています。メンバーは予め自分が喋りたいことを用意しておいて、しゃべります。その準備に時間がかかるかもしれませんが、それが英語学習には非常に役に立っているように思います。先生(Sashaさん)のこの考え方は非常に良いと思います。私は2,3か月だけ通っているだけですが、メンバーの多くは目に見えるように上手になっているように思います。

英語で行う授業

 lecture

英語で行う授業

日本の英語教育は依然として役に立っていないと言われている。英語で授業をやったらよいなどと言われているが、日本の大学は英語の授業をおこなう準備などできていないと言われている。教員も学生も授業が成り立つほど英語の能力が付いていない。これは数年前に北海道のある大学の先生が調査した結果です。数年前の旧型式TOEICの点数が700点以上(アメリカ留学に必要と言われた点数)に達しているものは1ないし2パーセントしかないいそうです。教員の方は900点以上が必要と言われている。大学で授業をやるには専門知識も必要である。いずれにしても、学生、教師いずれか一方ができなければ、有効な授業はできない。教員だけの問題ではないと思われる。これは、大学に限らず、中学、高校についても言えることかも知れない。中学、高校では英語の授業を英語で行うことを勧めているが。さてどしたらいいのでしょうか。

NHK「攻略英語リスニング」の復習

 

NHK「攻略英語リスニング」の復習

現在、1年ぐらい前のNHK「攻略英語リスニング」を復習しています。Microbiome, Cleopatraなどです。トピックス一つひとつが1ページの短編にまとめられていて大変面白いと思います。これらをシャドウイングを中心に復習しています。英語だけでなく、かなり質の高い知識の獲得にもなるので大変良いレッスンであったと思います。  

 

Microbiomeであれば、人間の体は人間自身の細胞はほんの少しで、ほとんとがバクテリアに占められているということでした。バクテリアのバランスが崩れると体の調子が悪くなるというものです。最近の乳酸菌関係の食品がもてはやされていることに通じると思いました。Cleopatraは美貌やファッションセンスはもちろんですが、大変な教養を身に着けていた。哲学、数学、さらには十カ国語を話したということでした。それがゆえにファラオとしての力を発揮できたということでした