Java Programming Tutorial for Beginner – 1 ~ 87

.


 Java Programming Tutorial for Beginner – 1 – Installing the JDK

.

Go to Google “Java SE(Standard Edition)”, or go to the link below.

http://www.oracle.com/technetwork/java/javase/downloads/index.html

Click on “Java Download”, or go to the link below.

http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

Click on “Accept License Agreement” and on “jdk-8u25-windows-x64.exe” to start downloading it.

C:\User\Adrian\Downloads\jdk-8u25-windows-x64.exe

Move “jdk-8u25-windows-x64.exe” into “C:\Program Files\”.

C:\Program Files\jdk-8u25-windows-x64.exe

Click on “jdk-8u25-windows-x64.exe”.

User Account Control (Popup)

Click on “Yes” to allow the installation of it.

Java SE Development Kit 8 Update 25 (64-bit) – Setup (Popup)

Click on “Next” and confirm the current location for the installation of it, or click on “Change”.

C:\Program Files\Java\jdk1.8.0_25\  

Click on “Next” and check the “Status” until it is completed.

Java SE Development Kit 8 Update 25 (64-bit) – Progress (Popup)

Java Setup Destination Folder  (Popup)

Confirm the current location for the installation of it, or click on “Change”.

C:\Program Files\Java\jre1.8.0_25\ 

Click onNext” and check the “Status” until it is completed.

Java SE Development Kit 8 Update 25 (64-bit) – Progress (Popup)

Java Setup – Progress (Popup)  

Java SE Development Kit 8 Update 25 (64-bit) – Complete (Popup)

Go to the Windows, “Advanced system settings” to add a new “System variable” as below.

Start > Control Panel > System  > Advanced > Environment Variables > (User variables for Adrian) New >

New User Variable (Popup)

Variable name: Path

Variable value: C:\Program Files\Java\jdk1.8.0_25\bin

Open cmd.exe and run javac to confirm that Java is installed properly.

C:\Windoews\system32\cmd.exe (Popup)

C:\Users\Adrian>javac

.


 Java Programming Tutorial for Beginner – 2 – Running a Java Program

.

Go to C:\ drive, and create a new Test folder, then create a new youtube.java file.

Right click on youtube.java file to Edit it as below and Save it.

youtube.java – Notepad (popup)

class apples{

public static void main(String args[]){

System.out.println(“Hello youtube!”);

}

}

.

Open cmd.exe and run as below.

C:\Windoews\system32\cmd.exe (Popup)

C:\Users\Adrian>cd\

C:\>dir

C:\>cd Test

C:\Test>dir 

youtube.java 

C:\Test>javac youtube.java

C:\Test>dir

apples.class

youtube.java 

.


 Java Programming Tutorial for Beginner – 3 – Downloading Eclipse

Go to Google Eclipse Downloads or go to the link below.

http://www.eclipse.org/downloads/?

Choose Eclipse IDE for Java Developers for Windows 64Bit.

http://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/luna/SR1/

eclipse-java-luna-SR1-win32-x86_64.zip

Click on Download eclipse-java-luna-SR1-win32-x86_64.zip.

C:\Users\Adrian\Downloads\eclipse-java-luna-SR1-win32-x86_64.zip

Click on eclipse-java-luna-SR1-win32-x86_64.zip folder to extract eclipse folder.  

C:\Users\Adrian\Downloads\eclipse-java-luna-SR1-win32-x86_64.zip\eclipse

Move eclipse folder into C:\Program Files\ and go to launch eclipse.exe

C:\Program Files\eclipse\eclipse.exe

Choose a workspace as below, or click on Browse to change it, and then click on OK.

C:\Users\Adrian\workspace

Go to File menu, and on New, then on Java Project.

New Java Project (Popup)

Project name: Adrian

Go to Package Explorer,

If not available, click on Window menu, and on Show View, then on Package Explorer.

Go to Adrian, and click on down arrow,

Right click on src, and on New, then on Class.

New Java Class (Popup)

Set Name: apples and click on Finish.

.


Java Programming Tutorial for Beginner – 4 – Hello YouTube

.

Go to apples.java tab, and edit as below.

package Adrian;

public class apples{

public static void main(String[] args){

System.out.println(“Hello youtube!”);

}

}

Click on Run icon.

Save and Launch (Popup)

Mark on Always save resources before launching and click on OK.

Console (Popup)

Confirm Hello youtube! comes up.

.


Java Programming Tutorial for Beginner – 5 – Variables

.

Go to apples.java tab, and edit as below.

package Adrian;

public class apples{

public static void main(String[] args){

double size; 

size = 5.28;

System.out.println(“I want ” + size + “\’ phone.” );

System.out.println(“Me, too.” );

}

}

.


Java Programming Tutorial for Beginner – 6 – Getting User Input

.

Go to apples.java tab, and edit as below.

package Adrian;

import java.util.Scanner;

public class apples{

public static void main(String[] args){

Scanner typing = new Scanner(System.in);

System.out.println(typing.nextLine());

}

}

Click on Run icon.

Type Hi, there inside console, and confirm Hi, there comes back.

.


 Java Programming Tutorial for Beginner – 7 – Building a Basic Calculator

.

Go to apples.java tab, and edit as below.

package Adrian;

import java.util.Scanner;

public class apples{

public static void main(String[] args){

Scanner typing = new Scanner(System.in);

double fnum, snum, answer;

System.out.println(“Enter your first number: “);

fnum = typing.nextDouble();

System.out.println(“Enter your second number: “);

snum = typing.nextDouble();

answer = fnum + snum;

System.out.println(“Your total is  “ + answer);

}

}

Click on Run icon.

Type 7.123, and  8.349 inside console, and confirm 15.472 comes back.

.


Java Programming Tutorial for Beginner – 8 – Math Operators

.

Go to apples.java tab, and edit as below.

package Adrian;

import java.util.Scanner;

public class apples{

public static void main(String[] args){

Scanner typing = new Scanner(System.in);

int girls, boys, people;

girls = 6;

boys = 3;

people = girls + boys;

System.out.println(“there are altogether ” + people + ” people.”);

}

}

Click on Run icon.

Confirm There’re altogether 9 people. comes back.

.


Java Programming Tutorial for Beginner – 9 – Increment Operators

.

import java.util.Scanner;

public class apples{

public static void main(String[] args){

Scanner typing = new Scanner(System.in);

int timeA = 5;

++timeA;

System.out.println(timeA);   // Confirm 6 comes up

int timeB = 5;

System.out.println(++timeB);   // Confirm 6 comes up

int timeC = 5;

System.out.println(timeC++);   // Confirm 5 comes up 

System.out.println(timeC);   // Confirm 6 comes up 

int timeD = 10;

timeD = timeD  + 10;

System.out.println(timeD);   // Confirm 20 comes up 

int timeE = 10;

timeE += 10;

System.out.println(timeE);   // Confirm 20 comes up 

}

.


Java Programming Tutorial for Beginner – 10 – If Statement

.

public class apples{

public static void main(String[] args){

int test = 6;

if (test  == 10){

System.out.println(“Great!”);

}else{

System.out.println(“So So.”);   // Confirm So So. comes up 

}

}

}

.


Java Programming Tutorial for Beginner – 11 – Logical Operators

public class apples{

public void static main(String[] args){

int boy, girl, man, woman;

boy = 18, girl = 28, man = 58, woman = 68; 

if (boy > 17 && girl > 17){

System.out.println(“Your couple are welcome”);  // Confirm this comes up.

}else{

System.out.println(“One of you are too young to come in.”);

}

if (man > 60 || woman > 60){

System.out.println(“One of you are too old to come in.”);  // Confirm this comes up.

}else{

System.out.println(“Your couple are welcome”);

}

}

.


Java Programming Tutorial for Beginner – 12 – Switch Statement

.

public class apples{

public static void main(String[] args){

int age = 3;

switch (age){

case 1: System.out.println(“You can crawl“); break;

case 2: System.out.println(“You can talk“); break;

case 3: System.out.println(“You can cause trouble“); break;  //confirm this comes up.

default: System.out.println(“Your age is not known“); break;

}

}

}

.


Java Programming Tutorial for Beginner – 13 – While Loop

.

public class apples{

public static void main(String[] args){

int counter  = 0;

while (counter < 10){

System.out.println(counter);  //confirm 0 1 2 3 4 5 6 7 8 9  comes up.

counter++;

}

}

}

.


Java Programming Tutorial for Beginner – 14 – Using Multiple Classes

.

public class Apple{

public static void main(String[] args ){

ApplePie AppPieA = new ApplePie();

ApplePieA.simpleMessage();   //confirm This is a subclass  comes up.

}

public class ApplePie{

public void simpleMessage(){

System.out.println(“This is a subclass”);

}

.


Java Programming Tutorial for Beginner – 15 – Use Methods with Parameters

.

import java.util.Scanner;

public class Apple{

public static void main(String[] args){

Scanner input = new Scanner(System.in);

ApplePie ApplePieA = new ApplePie();

System.out.println(“Enter your name here: “);

String name = input.nextLine();

ApplePieA.simpleMessage(name);

}

public class ApplePie{

public void simpleMessage(String name){

System.out.println(“Hello, “ + name);

}

}

.


Java Programming Tutorial for Beginner – 16 – Many Methods and Instances

.

import java.util.Scanner;

public class Apple{

public static void main(String[] args){

System.out.println(“Enter the name of your fist girlfriend here: “);

Scanner input = new Scanner(System.in);

String temp = input.nextLine();

ApplePie ApplePieA = new ApplePie();

ApplePIeA.setName(temp);

ApplePIeA.sayName();

}

public class ApplePie{

private String girlName

public void setName(String temp){

girlName = temp; 

}

public String getName(){

return girlName;

}

public void sayName(){

System.out.printf(“Your first girlfriend was %s“, getName());

}

}

.


Java Programming Tutorial for Beginner – 17 – Constuctors

 

import java.util.Scanner;

public class Apple{

public static void main(String[] args){

System.out.println(“Enter the name of your first girlfriend: “);

Scanner inputA = new Scanner(System.in);

Sting nameA = inputA.nextLine();

ApplePie ApplePieA = new ApplePie(nameA);

ApplePieA.sayName();

}

}

public class ApplePie{

private String girlName;

public AppPie(String name){

girlName = name;

}

public void setName(String name){

girlName = name;

}

public void getName(){

return girlName;

}

public void sayName(){

System.out.printf(“Your first girlfriend was %s. “, getName());

}

}

.


Java Programming Tutorial for Beginner – 18 – Nested if Statements

.

import java.util.Scanner;

public class Apple {

public static void main(String[] args){

System.out.println(“Enter your age: “);

Scanner inputA = new Scanner(System.in);

int ageA = inputA.nextInt;

if(ageA < 50){

System.out.println(“You’re young.”);

}else{

if(ageA > 75){

System.out.println(“You’re really old.”);

}else{

System.out.println(“You’re not that old.”);

}

}

}

.


Java Programming Tutorial for Beginner – 19 – else if Statement

.

import java.util.Scanner;

public class Apple {

public static void main(Strings[] args){

System.out.println(“Enter your age: “);

Scanner inputA = new Scanner(System.in);

int agaA = inputA.nextInt;

if (ageA >= 60)

System.out.println(“You’re a senior citizen”);

else if (ageA >= 50)

System.out.println(“You’re in your 50s”);

else if (ageA >= 40)

System.out.println(“You’re in your 40s”);

else if (ageA >= 30)

System.out.println(“You’re still young”);

}

.


Java Programming Tutorial for Beginner – 20 – Conditional Operators

.

import java.util.Scanner;

public class Apple {

public static void main(String[] args){

System.out.println(“Enter your age: “);

Scanner inputA = new Scanner(System.in);

int ageA = inputA.nextInt();

System.out.println( ageA > 50 ? “You’re old.”:“You’re young.”);

}

} 

.


Java Programming Tutorial for Beginner – 21 – Simple Averaging Program

.

import java.util.Scanner;

public class Apples {

public static void main (String[] args){

System.out.println(“Enter 10 grades of yours”);

Scanner inputA = new Scanner(System.in);

int counterA =0;

int totalA = 0;

while (counterA <10){

int gradeA = inputA.nextLine();

totalA = total A + gradeA;

counterA++;

}

int averageA = total A/counterA;

System.out.println(“Your average is “ + averageA );

}

}

.


Java Programming Tutorial for Beginner – 22 – for Loops

.

public class Apples {

public static void main(String[] args) {

for(int counterA =1; counterA <= 10; counterA++){

System.out.println(“counterA “ + counterA);

}

for(int counterB =1; counterB <= 10; counterB+=3){

System.out.println(“counterB “ + counterB);

}

for(int counterC =6; counterC <= 21; counterC+=3){

System.out.println(“counterC “ + counterC);

}

}

}

.


Java Programming Tutorial for Beginner – 23 – Compound Interest

.

public class Apples {

public static void main(String[] args){

double amountA;

double principalA = 10000;

double rateA = .01;

for (int dayA =1; dayA <= 20; dayA++){

amountA = principalA*math.pow(1 + rateA, dayA);

System.out.println(“Day “ + dayA + ” $” + amountA);

}

}

}

.


Java Programming Tutorial for Beginner – 24 – Do While Loops

.

public class Apple {

public static void main(String[] args){

int counterA = 0;

do{

System.out.println(“counterA “ + counterA);

counter++;

}while(counterA <= 10); 

int counterB = 15;

do{

System.out.println(“counterB ” + counterB);

counter++;

}while(counterB <= 10); 

}

}

.

 


Java Programming Tutorial for Beginner – 25 – Math Class Methods

public class Apple {

public static void main(String[] args){

System.out.println(Math.abs(-26.7)); // confirm 26.7 comes up.

System.out.println(Math.ceil(7.2)); // confirm 8 comes up.

System.out.println(Math.floor(7.8)); // confirm 7 comes up.

System.out.println(Math.max(6, 9)); // confirm 9 comes up.

System.out.println(Math.min(6, 9)); // confirm 6 comes up.

System.out.println(Math.pow(5, 3)); // confirm 125 comes up.

System.out.println(Math.sqrt(9)); // confirm 3 comes up.

}

}

.


Java Programming Tutorial for Beginner – 26 – Random Number Generator

 

import java.util.Random;

public class Apple {

public static void main(String[] args){

Random randomA = new Random();

for (int counterA = 1; counterA < = 10; counter++){

int realA = 1 + randomA.nextInt(6);

System.out.println(realA + ” “); 

}

}

}

.


Java Programming Tutorial for Beginner – 27 – Introduction to Arrays

.p

public class Apple {

public static void main(String[] args){

int arrayA[] = new int [10];

arrayA[0] = 87; 

arrayA[1] = 543;

arrayA[2] = 65;

System.out.prinln(arrayA[1]);

.

int arrayB[] = {2, 4, 5, 7, 9};

System.out.println(arrayB[1]); 

}

}

.


Java Programming Tutorial for Beginner – 28 – Creating an Array Table

.

public class Apple {

public static void main(Strings[] args){

System.out.println(“Index\tValue”);

int ValueA[] = {32, 12, 18, 54, 2};

for(int counterA=0; counterA <ValueA.length; counterA++ ){

int IndexA = 1 + counterA; 

System.out.println(IndexA + “\t” + ValueA[counterA]);

}

}

}

.


Java Programming Tutorial for Beginner – 29 – Summing Elements of Arrays

.

public class Apples {

public static void main(String[] args) {

int valueA[] = {11, 22, 33, 44, 55};

int totalA = 0; 

for (int counterA = 0; counterA<valueA.length; counterA++){

total A += valueA[counterA];

System.out.println(“the total is ” + totalA); 

}

}

}

.


Java Programming Tutorial for Beginner – 30 – Array Elements as Counters

.

import java.util.Scanner;

import java.util.Random;

public class Apple {

public static void main(String[] args){

System.out.printlin(“Face\tFrequency”);

Random randA = new Random();

int freqA[] = new int[7];

for (int rollA = 0; rollA < 1000; rollA++){

 ++freq[1 + randA.Int(6);

}

for (int faceA =0; faceA < freqA.length; face A++){

System.out.println(faceA “\t” +freqA[faceA]);

}

}

}

.


Java Programming Tutorial for Beginner – 31 – Enhanced for Loop

.

public class Apples {

public static void main(String[] args){

int adrianA[] = {1, 2, 3, 4, 5};

int totalA = 0;

for (int x:adrianA){

total += x; 

}

System.out.println(total);

}

}

.


Java Programming Tutorial for Beginner – 32 – Arrays in Methods

.

public class Apple {

public static void main (String[] args){

int shine[] = {3, 4, 5, 6, 7};

change (shine);

for (int a:shine)

System.out.println (a);

}

public static void change(int A[]){

for (int counter = 0; counter <A.length; counter++){

A[counter] += 5;

}

}

}

.


Java Programming Tutorial for Beginner – 33 – Multidimensional Arrays

.

public class Apple {

public static void main(String[] args){

int A[][] = {{8, 9, 10, 11}, {12, 13, 14, 15}};  //  00, 01, 02, 03  &  10, 11, 12, 13

int B[][] = {{30, 31, 32, 33}, {43}, {4, 5, 6}};  //  00, 01, 02, 03  &  10  & 20 21 22

}

}

.


Java Programming Tutorial for Beginner – 34 – Table for Multi Arrays

.

public class Apple {

public static void main(String[] args){

int firstArray[][] = {{8, 9, 10, 11}, {12, 13, 14, 15}};  //  00, 01, 02, 03  &  10, 11, 12, 13

int secondArray[][] = {{30, 31, 32, 33}, {43}, {4, 5, 6}};  //  00, 01, 02, 03  &  10  & 20 21 22

System.out.println(“This is the first array”);

display(firstArray);

System.out.println(“This is the first array”);

display(secondArray);

}

public static void display(int x[][]){

for(int row = 0; row < x.length; row++){

for(int column = 0; column <x[row].length; column++){

System.out.print( x[row][column] + “\t”);

}

System.out.println();

}

}

}

.


Java Programming Tutorial for Beginner – 35 – Variable Length Arguments

.

public class Apple{

public static void main(String[ ] args){

System.out.println(average(43, 56, 76, 8, 65, 76, 2, 31));

}

public static int average(int…numbers){

int total = 0;

for(int x:numbers)

total+= x;

return total/numbers.length;

}

}

.


Java Programming Tutorial for Beginner – 36 – Time Class

.

public class Apple{

public static void main(String[] args){

ApplePie ApplePieObject = new ApplePie();

ApplePieObject.setTime(13, 27, 6);

System.out.println(ApplePieObject.inMilitary());

}

}

public class ApplePie{

private int hourminutesecond;

public void setTime(int h, int m, int s){

hour = ((h>=0 && h<24) ? h:0);

minute = ((m>=0 && m<60) ? m:0);

second = ((s>=0 && s<60) ? s:0);

}

public String inMilitary(){

return String.format(“%02d:%02d:%02d”hourminutesecond);

}

.


Java Programming Tutorial for Beginner – 37 – Display Regular time

.

public class Apple{

public static void main(String[] args){

ApplePie ApplePieObject = new ApplePie();

ApplePieObject.setTime(13, 27, 6);

System.out.println(ApplePieObject.inStandard());

}

}

public class ApplePie{

private int hour, minute, second;

public void setTime(int h, int m, int s){

hour =((h>=0 && h<24) ? h:0);

minute = ((m>=0 && m<60)? m:0);

second = ((s>= 0 && s<60) ? s:0);

}

public String inStandard(){

return String.format(“%d:%02d:%02d %s”, ((hour = 0 || hour = 12) ? 12:hour%12), minute, second, (hour <12 ? “AM”: “PM”));

}

}

.


Java Programming Tutorial for Beginner – 38 – Public, Private and this

.

public class Apple{

public static void main(String[] args){

ApplePie ApplePieObject = new ApplePie();

ApplePieObject.setTime(7, 8, 9);

System.out.println(ApplePieObject.inMilitary()); //04:05:06 comes out instead of 07:08:09 or 01:02:03

}

}

public class ApplePie{

private int hour = 1;  

private int minute = 2;  

private int second = 3;

public void setTime(int h, int m, int s){

this.hour = 4;  

this.minute = 5;  

this.second = 6;

}

public String inMilitary(){

return String.format(“%02d:%02d:%02d”hourminutesecond);

}

.


Java Programming Tutorial for Beginner – 39 – Multiple Constructors

.

public class ApplePie{

private int hour;

private int minute;

private int second;

public ApplePie(){

this(0,0,0);

}

public ApplePie(int h){

this(h,0,0);

}

public ApplePie(int h, int m){

this(h,m,0);

}

public ApplePie(int h, int m, int s){

setTime(h,m,s);

}

.


Java Programming Tutorial for Beginner – 40 – Set and Get Methods

.

public void setTime(int h, int m, int s){

setHour(h);

setMinute(m);

setSecond(s);

public void setHour(int h){

hour = ((h>=0 && h<24) ? h:0);

}

public void setMinute(int m){

minute = ((m>= 0 && m<60) ? m:0);

}

public void setSecond(int s){

second = ((s>= 0 && s<60) ? s:0);

}

public int getHour(){

return hour;

}

public int getMinute(){

return minute;

}

public int getSecond(){

return second;

public String inMilitary(){

return String.format(“%02d:%02d:%02d”, getHour(), getMinute(), getSecond());

}

.


Java Programming Tutorial for Beginner – 41 – Building Objects for Constructors

.

public class Apple{

public static void main(String[] args){

ApplePie ApplePie1 = new ApplePie();

ApplePie ApplePie2 = new ApplePie(5);

ApplePie ApplePie3 = new ApplePie(5,13);

ApplePie ApplePie4 = new ApplePie(5, 13, 43);

System.out.printf(“%s\n”, ApplePie1.inMilitary());

System.out.printf(“%s\n”, ApplePie2.inMilitary());

System.out.printf(“%s\n”, ApplePie3.inMilitary());

System.out.printf(“%s\n”, ApplePie4.inMilitary());

}

}

.


Java Programming Tutorial for Beginner – 42 – toString

.

public class Apple{

public static void main(String[] args){

AppleBar AppleBar1 = new AppleBar (4,5,6);

}

}

public class AppleBar{

private int month;

private int day;

private int year;

public AppleBar(int m, int d, int y){

month = m;

day = d;

year = y;

System.out.printf(“The constructor for this is %s\n”, this); 

}

public String toString(){

return String.format(“%d/%d/%d”, month, day, year);

}

}

.


Java Programming Tutorial for Beginner – 43 – Composition

.

public class Apple{

public static void main(String[] args){

AppleBar AppleBar1 = new AppleBar(2001, 2, 3);

ApplePie ApplePie1 = new ApplePie(“Adrian”, AppleBar1);

System.out.println(ApplePie1);

}

}

public class ApplePie{

private String name;

private AppleBar birthday;

public ApplePie(String theName, AppleBar theDate){

name = theName;

birthday = theDate;

}

public String toString(){

return String.format(“my name is %s, my birthday is %s.”, name, birthday);

}

}

.


Java Programming Tutorial for Beginner – 44 – Enumeration

.

public class Apple{

public static void main(String[] args){

for(AppleMen x: AppleMen.values())

System.out.printf(“%s\t%s\t%s\n”, x, x.getDesc(), x.getAge());

}

}

public enum AppleMen{

Adam(“nice”, “22”),

Betty(“cute”, “10”),

Cindy(“big”, “12”),

Demi(“slim”, “14”),

Emily(“kind”, “16”),

Fanny(“hot”, “18”);

private final String desc;

private final String age;

AppleMen(String theDesc, String theAge){

desc = theDesc;

age = theAge;

}

public String getDesc(){

return desc;

}

public String getAge(){

return age;

}

}

.


Java Programming Tutorial for Beginner – 45 – EnumSet range

.

import java.util.EnumSet;

public class Apple{

public static void main(String[] args){

for(AppleMen AppleMen1: EnumSet.range(AppleMen.Cindy, AppleMen.Emily))

System.out.printf(“%s\t%s\t%s\n”, AppleMen1, Applemen1.getDesc(), AppleMen1.getAge());

}

}

.


Java Programming Tutorial for Beginner – 46 – Statc

.

public class Front{

public static void main(String[] args){

Back1 member1 = new Back1(“Megan”, “Fox”);

Back1 member2 = new Back1(“Lady”“Gaga”);

Back1 member3 = new Back1(“Taylor”, “Swift”);

}

}

public class Back1{

private String fn;

private String ln;

private static int members;

public Back1(String firstName, String lastName){

fn = firstName;

ln = lastName;

members++;

System.out.printf(“This club has %d members including %s %s.\n”, members, fn, ln);

}

.


Java Programming Tutorial for Beginner – 47 – More on Static

.

public class Front(){

public static void main(String[] args){

Back1 member1 = new Back1(“Megan”, “Fox”};

Back1 member2 = new Back1(“Lady”, “Gaga”);

Back1 member3 = new Back1(“Taylor”,“Swift”);

System.out.println(member1.getFn()); //Megan comes out.

System.out.println(member2.getLn()); //Gaga comes out.

System.out.println(member3.getMb()); //3 comes out.

System.out.println(Back1.getMb()); //3 comes out.

}

}

public class Back1(){

private String fn;

private String ln;

private static int mb; // members

public Back1(String firstName, String lastName){

fn = firstName;

ln = lastName;

mb++;

System.out.printf(“This club has %d members including %s %s\n.”, mb, fn, ln);

}

public String getFn(){

return fn;

}

public String getLn(){

return ln;

}

public static int getMb(){

return mb;

}

}

.


Java Programming Tutorial for Beginner – 48 – final

public class Front{

public static void main(String[] args){

Back1 Back1A = new Back1(10);

for(int i =0; i <5; i++){

Back1A.add();

System.out.printf(“%s”, Back1A);

}

}

}

public class Back1{

private int num;

private final int NUMBER;

public Back1(int x){

NUMBER =x;

}

public void add(){

num+ = NUMBER;

}

public String toString(){

return String.format(“number = %d\n”, num);

}

}

.


Java Programming Tutorial for Beginner – 49 – Inheritance

.

public class Front{

public static void main(String[] args){

BackSub1 BS1 = new BackSub1();

BackSub2 BS2 = new BackSub2();

BS1.back();

BS2.back();

}

}

public class Back{

public void back(){

System.out.println(“This is backup method”);

}

}

public class BackSub1 extends Back {

}

public class BackSub2 extends Back {

}

.


Java Programming Tutorial for Beginner – 50

 

import java.swing.JOptionPane;

public class Front{

public static void main(String[] args){

String fn = JOptionPane.showInputDialog(“Enter the first number”);

String sn = JOptionPane.showInputDialog(“Enter the second number”);

int num1 = Integer.parseInt(fn);

int num2 = Integer.parseInt(sn);

int sum = num1 + num2;

JOptonPane.showMessageDialog(null, “The sum is” + sum, “Here’s the answer.”, JOptionPane.PLAIN_MESSAGE);

}

}

.


Java Programming Tutorial for Beginner – 51 – GUI with JFrame

.

import javax.swing.JFrame;

public class Apple{

public static void main(String[] args){

BackSub1 BS1 = new BackSub1( );

BS1.setVisible(true);

BS1.setSize(275, 180);

BS1.setDefaultCloseOperation(JFrame_EXIT_ON_CLOSE);

}

}

import java.awt.FlowLayout;

import javax.swing.JFrame;

import javax.swing.JLabel;

public class BackSub1 extends JFrame{

private JLabel item1;

public BackSub1(){

super(“The title bar”);

setLayout(new FlowLayout()); 

item1 = new JLabel(“This is a sentence”);

item1.setToolTipText(“This is going to show up”);

add(item1);

}

}

.


Java Programming Tutorial for Beginner – 52 – Event Handling

.

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JFrame;

import javax.swing.JTextField;

import javax.swing.JpasswordField;

import javax.swing.JOptionPane;

public class BackSub1 extends JFrame{

private JTextField JTF1;

private JTextField JTF2;

private JTextField JTF3;

private JpasswordField JPF;

public BackSub1(){

super(“The Title”); 

setLayout(new FlowLayout());

JTF1 = new JTextField(10);

add(JTF1);

JTF2 = new JTextField(“Enter text here”);

add(JTF2);

JTF3 = new JTextField(“uneditable”, 20);

JTF3.setEditable(false);

add(JTF3);

JPF = new JpasswordField(“mypass”); 

add(JPF);

}

.


Java Programming Tutorial for Beginner – 53 – ActionListener

.

handler H = new handler();

JTF1.addActionListener(H);

JTF2.addActionListener(H);

JTF3.addActionListener(H);

JPF.addActionListener(H);

}

private class handler implements ActionListener{

public void actionPerformed(ActionEvent AE){

String S = “”;

if (AE.getSource()==JTF1)

S = String.format(“field1: %s”, AE.getActionCommand());

else if(AE.getSource() == JTF2)

S = String.format(“field2: %s”, AE.getActionCommand());

else if(AE.getSource() == JTF3)

S = String.format(“field3: %s”, AE.getActionCommand());

else if(AE.getSource() == JPF)

S = String.format(“Password: %s”, AE.getActionCommand());

JOptionPane.showMessageDialog(null, S);

}

}

 

.


Java Programming Tutorial for Beginner – 54 – Event Handler Program

.

import javax.swing.JFrame;

public class Apple{

public static void main(String[] args){

BackSub1 BS1 = new BackSub1();

BS1.setVisible(true);

BS1.setSize(350, 100);

BS1.setDefaultCloseOperation(JFrame.Exit_ON_CLOSE);

}

}

.


Java Programming Tutorial for Beginner – 55 – Introduction to Polymorphism

.

public class Apple{

public static void main(String[] args){

Back B[] = new Back[2];

B[0] = new BackSub1();

B[1] = new BackSub2();

for(int x=o; x<2; x++){

B[x].back();

}

}

}

public class Back{

void back(){

System.out.println(“This Back is great”);

}

}

public class BackSub1 extends Back{

void back(){

System.out.println(“This BackSub1 is great”);

}

}

public class BackSub1 extends Back{

void back(){

System.out.println(“This BackSub2 is great”);

}

}

.


Java Programming Tutorial for Beginner – 56 – Polymorphic Arguments

public class Front1{

public static void main(String[] args){

Back1 B1 = new Back1();

Back2 B2a = new Back2A();

Back2 B2b = new Back2B();

B1.back1(B2a);

B1.back1(B2b);

}

}

       public class Back1{

void back1(Back2 x){

x.back2();

}

}

       public class Back2{

void back2(){

System.out.println(“This Back2 is good”);

}

}

       public class Back2A extends Back2{

void back2(){

System.out.println(“This Back2A is good”);

}

}

       public class Back2B extends Back2{

void back2(){

System.out.println(“This Back2B is good”);

}

}

.


Java Programming Tutorial for Beginner – 57 – Overriding Rules

.

 public class Back2{

void back2(){

System.out.println(“This Back2 is good”);

}

}

       public class Back2A extends Back2{

void back2(){

System.out.println(“This Back2A is good”);

}

}

       public class Back2B extends Back2{

void back2(){

System.out.println(“This Back2B is good”);

}

}

.


Java Programming Tutorial for Beginner – 58 – Abstract & Concrete Classes

 .

abstract public class Back2{

public abstract void back2();

}

}

       public class Back2A extends Back2{

public void back2(){

System.out.println(“This Back2A is good”);

}

}

       public class Back2B extends Back2{

public void back2(){

System.out.println(“This Back2B is good”);

}

}

.


Java Programming Tutorial for Beginner – 59 – Class To Hold Objects

.

public class Front1{

public static void main(String[] args){

Back1 B1 = new Back1();

Back2Sub1 B2S1 = new Back2Sub1();

B1.back1(B2S1);

}

}

public class Back1{

private Back2Sub1[] B2S1A = new Back2Sub1[5];

private int i = o;

void back1(Back2Sub1 B2S1){

if(i<B2S1A.length){

B2S1A[i] =B2S1;

System.out.println(B2S1 is added at index “ +i);

i++;

}

}

}

public class Back2{

void back2(){

}

}

       public class Back2Sub1 extends Back2{

}

       public class Back2Sub2 extends Back2{

}

.


Java Programming Tutorial for Beginner – 60 – Array Holding Many Objects

.

public class Front1{

public static void main(String[] args){

Back1 B1 = new Back1();

Back2Sub1 B2S1 = new Back2Sub1();

Back2Sub2 B2S2 = new Back2Sub2();

B1.back1(B2S1);

B1.back1(B2S2);

}

}

public class Back1{

private Back2[] B2A = new Back2[5];

private int i = o;

void back1(Back2 B2){

if(i<B2A.length){

B2A[i] =B2;

System.out.println(Back2 is added at index “ +i);

i++;

}

}

}

public class Back2{

void back2(){

}

}

       public class Back2Sub1 extends Back2{

}

       public class Back2Sub2 extends Back2{

}

.


Java Programming Tutorial for Beginner – 61 – Simple Polymorphic Program 

.

public class Front1{

public static void main(String[] args){

Back2[] B2A = new Back2[2];

Back2Sub1 B2S1 = new Back2Sub1();

Back2Sub2 B2S2 = new Back2Sub2();

B2A[0] = B2S1;

B2A[1] = B2S2;

for(Back2 x: B2A){

x.back2();

}

}

}

public class Back2{

public void back2(){

System.out.println(“Back2 doesn’t make noise”);

}

}

public class Back2Sub1 extends Back2{

public void back2(){

System.out.println(“Back2Sub1 makes noise1”);

}

}

public class Back2Sub2 extends Back2{

public void back2(){

System.out.println(“Back2Sub2 makes noise2”);

}

}

.


Java Programming Tutorial for Beginner – 62 – JButton 1

.

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JFrame;

import javax.swing.JButton;

import javax.swing.JIcon;

import javax.swing.JImageIcon;

import javax.swing.JOptionPane;

public class Back1Sub1 extends JFrame{

private JButton JB1;

private JButton JB2;

public Back1Sub1(){

super(“The title”);

setLayout(new FlowLayout());

JB1 = new JButton(“regular button”);

add(JB1);

Icon I1 = new ImageIcon(getClass().getResource(“picture1.jpg”);

Icon I2 = new ImageIcon(getClass().getResource(“picture2.png”);

JB2 = new JButton(“custom button”, I1);

JB2.setRolloverIcon(I2);

add(JB2);

Handler H = new Handler();

JB1.addActionListener(H);

JB2.addActionListener(H);

}

.


Java Programming Tutorial for Beginner – 63 – JButton 2

.

private class Handler implements ActionListener{

public void actionPerformed(ActonEvent AE){

JOptionPane.showMessageDialog(null, String.format(“%s”, AE.getActionCommand()));

}

}

}  // The end of Back1Sub1

.

import javax.swing.JFrame;

public class Front1{

public static void main(String[] args){

Back1Sub1 B1S1 = new Back1Sub1();

B1S1.setVisible(true);

B1S1.setSize(“300”, “200”);

B1S1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

.


Java Programming Tutorial for Beginner – 64 – JCheckBox 1

.

import java.awt.*;

import java.awt.event.*;

import java.swing.*;

public class Back1Sub1 extends JFrame{

private JTextField JTF;

private JCheckBox JCB1;

private JCheckBox JCB2;

public Back1Sub1(){

super(“The Title”);

setFlowLayout(new FlowLayout());

JTF = new JTextField(“This is a sentence”, 20);

JTF.setFont(new Font(“Serif”, Font.PLAIN, 14);

add(JTF);

JCB1 = new JCheckBox(“Bold”);

add(JCB1);

JCB2 = new JCheckBox(“Italic”);

add(JCB2);

Handler H = new Handler();

JCB1.addItemListener(H);

JCB2.addItemListener(H);

}

.


Java Programming Tutorial for Beginner – 65 – JCheckbox 2

.

private class Handler implements ItemListener{

public void itemStateChanged(ItemEvent IE){

Font F = null;

if(JCB1.isSelected() && JCB2.isSelected())

F = new Font(“Serif”, Font.BOLD + Font.ITALIC, 14);

else if(JCB1.isSelected())

F = new Font(“Serif”, Font.BOLD, 14);

else if(JCB2.isSelected())

F = new Font(“Serif”, Font.ITALIC, 14);

else

F = new Font(“Serif”, Font.PLAIN, 14);

JTF.setFont(F);

}

}

}  // The end of Back1Sub1

.

import javax.swing.JFrame;

public class Front1{

public static void main(String[] args){

Back1Sub1 B1S1 = new Back1Sub1();

B1S1.setVisible(true);

B1S1.setSize(“300”, “200”);

B1S1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

.


Java Programming Tutorial for Beginner – 66 – JRadioButton 1

.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Back1Sub1 extends JFrame{

private JTextField jtf;

private Font fp, fb, fi, fbi;

private JRadioButton jrbp, jrbb, jrbi, jrbbi;

private ButtonGroup bg;

public back1sub1(){

super(“The Title”);

setLayout(new FlowLayout());

jtf = new JTextField(“Adrian is awesome”, 25); add(jtf);

jtf.setFont(fp);

fp = new Font(“Serif”, Font.PLAIN, 14);

fb = new Font(“Serif”, Font.BOLD, 14);

fi = new Font(“Serif”, Font.ITALIC, 14);

fbi = new Font(“Serif”,Font.BOLD Font.ITALIC , 14);

jrbp = new JRadioButton(“Plain”, true); add(jrbp);

jrbb = new JRadioButton(“Bold”, false); add(jrbb); 

jrbi = new JRadioButton(“Italic”, false); add(jrbi);

jrbbi = new JRadioButton(Bold & Italic”, false); add(jrbbi); 

 

bg = new ButtonGroup();

bg.add(jrbp); bg.add(jrbb); BG.add(jrbi); BG.add(jrbbi); 

.


Java Programming Tutorial for Beginner – 67 – JRadioButton 2

.

jrbp.addItemListener(new Handler(fp));

jrbb.addItemListener(new Handler(fb));

jrbi.addItemListener(new Handler(fi));

jrbbi.addItemlistener(new Handler(fbi));

}

private class Handler implements ItemListener{

private Font f;

public Handler(Font F){

f = F;

}

public void itemStateChanged(ItemEvent IE{

jtf.setFont(f);

}

}

}  // The end of Back1Sub1

.

import javax.swing.JFrame;

public class Front1{

public static void main(String[] args){

Back1Sub1 b1s1 = new Back1Sub1();

b1s1.setVisible(true);

b1s1.setSize(“300”, “200”);

b1s1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

.


Java Programming Tutorial for Beginner – 68 – JComboBox 1

.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class BackSub1 extends JFrame{

private JComboBox jcb;

private static String[] fn = {“x.jpg”, “y.jpg”};

private JLabel jl;

private Icon [] i = {new ImageIcon(getClass(), getResource(fn[0])),

                                      new ImageIcon(getClass().getResource(fn[1]))};

.


Java Programming Tutorial for Beginner – 69 – JComboBox 2

.

public Back1Sub1(){

super(“the title”);

setLayout(new FlowLayout());

jcb = new ComboBox(fn);

jcb.addItemListener(

new ItemListener(){

public void itemStateChanged(ItemEvent IE){

if(IE.getStateChange() == ItemEvent.SELECTED)

jl.setIcon(i[jcb.getSelectedIndex()];

}

}

);

add(jcb);

jl = new JLabel(i[0]);

add(jl);

}

}  // The end of Back1Sub1

.

import javax.swing.JFrame;

public class Front1{

public static void main(String[] args){

Back1Sub1 b1s1 = new Back1Sub1();

b1s1.setVisible(true);

b1s1.setSize(“300”, “200”);

b1s1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

.


Java Programming Tutorial for Beginner – 70 – JList 1

.

import java.awt.*

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

public class Back1Sub1 extends JFrame{

private static String[] cn = {“white”, “Red”, “Blue”, “Black”};

private static Color [] c = {Color.WHITE, Color.RED, Color.BLUE, Color.Black};

private JList jl = new Jlist(cn);

private JScrollPane jsp = new JScrollPane(jl);

public Back1Sub1(){

super (“JList App”);

setLayout(new FlowLayout());

jl.setVisibleRowCount(4);

jl.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

add(jsp);

.


Java Programming Tutorial for Beginner – 71 – JList 2

.

jl.addListSelectionListener(

new ListSelectionListener(){

public void valueChanged(ListSelectionEvent lse){

getContentPane().setBackground(c[jl.getSelectedIndex()]);

}

}

);

}

}  // The end of Back1Sub1

.

import javax.swing.JFrame;

public class Front1{

public static void main(String[] args){

Back1Sub1 b1s1 = new Back1Sub1();

b1s1.setVisible(true);

b1s1.setSize(“300”, “200”);

b1s1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

.


Java Programming Tutorial for Beginner – 72 – Multiple Selection List 1

.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

.

public class Back1Sub1 extends JFrame{

private static String [] fn = {“f1”, “f2”, “f3”, “f4”, “f5”};

private JList jl1 = new JList(fn);

private JList jl2 = new JList();

private JScrollPane jsp1 = new JScrollPane(jl1);

private JScrollPane jsp2 = new JScrollPane(jl2);

private JButton jb = new JButton(“Move to the right”);

.

public Back1Sub1(){

super(“Multi Selecition List App”);

setLayout(new FlowLayout());

jl1.setVisibleRowCount(3);

jl1.setSelectionMode(ListSelecitonModel.MULTIPLE_INTERVAL_SELECTION);

add(jsp1);

jb.addActionListener(

new ActionListener(){

public void actionPerformed(ActionEvent AE){

jl2.setListData(jl1.getSelectedValuesList().toArray());

}

}

);

add(jb);.

.


 Java Programming Tutorial for Beginner – 73 – Multiple Selection List 2

.

jl2.setVisibleRowCount(3);

jl2.setFixedCellWidth(100);

jl2.setFixedCellHeight(20);

jl2.setSelectionMode(ListSelecitonModel.MULTIPLE_INTERVAL_SELECTION);

add(jsp2);

}

}  // The end of Back1Sub1

.

import javax.swing.JFrame;

public class Front1{

public static void main(String[] args){

Back1Sub1 b1s1 = new Back1Sub1();

b1s1.setVisible(true);

b1s1.setSize(“300”, “200”);

b1s1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

.


 Java Programming Tutorial for Beginner – 74 – Mouse Event 1

.

import java.awt.*;

import java.awt.event.*;

import java.swing.*;

import java.swing.event.*;

.

public class Back1Sub1{

private JPanel jp = new JPanel();

private JLabel jl = new JLabel(“default”);

public Back1Sub1(){

super(“Mouse Event App”);

jp.setBackground(Color.WHITE);

add(jp, BorderLayout.CENTER);

add(jl, BorderLayout.SOUTH);

.

Handler H = new Hander();

jp.addMouseListener(H);

jp.addMouseMotionListener(H);

}

.


 Java Programming Tutorial for Beginner – 75 – Mouse Event 2

 .

private class Handler implements MouseListener, MouseMotionListener{

public void mouseClicked(MouseEvent ME){

jl.setText(String.format(“Mouse got clicked at %d, %d”, ME.getX(), ME.getY());

}

public void mousePressed(MouseEvent ME){

jl.setText(“Mouse got pressed”);

}

public void mouseReleased(MouseEvent ME){

jl.setText(“Mouse got released”);

}

public void mouseEntered(MouseEvent ME){

jl.setText(“Mouse entered the panel”);

}

public void mouseExited(MouseEvent ME){

jl.setText(“Mouse exited the panel”);

}

public void mouseMoved(MouseEvent ME){

jl.setText(“Mouse got moved”);

}

public void mouseDragged(MouseEvent ME){

jl.setText(“Mouse got dragged”);

}

}

.


 Java Programming Tutorial for Beginner – 76 – Mouse Event 3

.

import javax.swing.JFrame;

public class Front1{

public static void main(String[] args){

Back1Sub1 b1s1 = new Back1Sub1();

b1s1.setVisible(true);

b1s1.setSize(“300”, “200”);

b1s1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

.


 Java Programming Tutorial for Beginner – 77 – Adapter Classes

 .

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

.

public class Back1Sub1 extends JFrame{

private String s;

private JLabel jl = new JLabel(“Default”);

public Back1Sub1(){

super(“Adapter Classes App”);

add(jl, BorderLayout.SOUTH);

addMouseListener(new MouseListener());

}

private class MouseListener extends MouseAdapter{

public void mouseClicked(MouseEvent ME){

s = String.format(“Mouse got clicked %dtimes “, ME.getClickCount());

if(ME.isMetaDown())

s += “with the right button”;

else if(ME.isAltDown())

s += “with the center button”;

else

s += “with the left button”;

jl.setText(s));

}

}

}

import javax.swing.JFrame;

public class Front1{

public static void main(String[] args){

Back1Sub1 b1s1 = new Back1Sub1();

b1s1.setVisible(true);

b1s1.setSize(“300”, “200”);

b1s1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

.


 Java Programming Tutorial for Beginner – 78 – File Classes 

.

import java.io.File;

class Filing{

public static void main(String[] args){

File f = new File(“C:\\Users\\Adrian\\Desktop\\test1.txt”);

if(f.exists())

System.out.println(f.getName() + ” exists.”);

else

System.out.println(“This file doesn’t exist.”);

}

}

.


 Java Programming Tutorial for Beginner – 79 – Creating Files 

.

import java.util.*;

class Filing{

public static void main(String[] args){

final Formatter f;

try {

f = new Formatter(“test2.txt”);

System.out.println(“created a file”); 

//go to Package Explorer and right click on the project name “Adrian”

//click on Refresh and confirm file is shown below JRE System Library

//right click on it and click on Property and check for the location of it.

}

catch(Exception e){

System.out.println(“got an error”);

}

}

}

.


 Java Programming Tutorial for Beginner – 80 – Writing to Files

.

import java.io.*;

import java.util.*;

import java.lang.*;

public class CreatingFile{

private Formatter f;

public void openFile(){

try{

f = new formatter (“chinese.txt”);

}

catch(Exception e){

System.out.println(“got some error”);

}

}

public void editFile(){

f.format(“%s %s %s”, “20 “, “Adrian”, “Alpha”);

}

public void closeFile(){

f.close();

}

}

.

public class FileToCreate{

public static void main(String[] args){

CreatingFile cf = new CreatingFile();

cf.openFile();

cf.editFile();

cf.closeFile();

}

}

.


 Java Programming Tutorial for Beginner – 81 – Reading from Files

 

import java.io.*;

import java.util.*;

public class ReadingFile{

private Scanner s;

public void openFile(){

try{

s = new Scanner(new File(“chinese.exe”);

}

catch(Exception e){

System.out.println(“couldn’t open the file.”);

}

}

public void readFile(){

while(s.hasNext()){

String a = s.next();

String b = s.next();

String c = s.next();

System.out.printf(“%s %s %s\n”, a, b, c);

}

}

public void closeFile(){

s.close();

}

}

.

public class FileToRead{

public static void main(Strin[] args){

ReadingFile rf = new ReadingFile();

rf.openFile();

rf.readFile();

rf.closeFile();

}

}

.


 Java Programming Tutorial for Beginner – 82 – Exception Handling

.

import java.io.*;

import java.util.*;

.

public class ExceptionToHandle{

public static void main(String[] args){

Scanner s = new Scanner(System.in);

int t = 1;

do{

try{System.out.println(“enter the first number: “);

int n1 = s.nextInt();

System.out.println(“enter the second number: “);

int n2 = s.nextInt();

int n3 = n1/n2

System.out.println(n3);

t=2;

}

catch(Exception e){

System.out.println(“got an error”);

}

}while(t ==1);

}

.


 Java Programming Tutorial for Beginner – 83 – Flow Layout

.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

.

public class HandlingLayout extends JFrame{

private JButton jb1, jb2, jb3;

private FlowLayout fl;

private Container c;

public HandlingLayout (){

super(Handling Layout App”);

fl = new FlowLayout();

= getContentPane();

setLayout(fl);

jb1 = JButton(“left”);

add(jb1);

jb1.addActionListener(

new actionListener(){

public void actionPerformed(Action Event){

fl.setAlignment(FlowLayout.LEFT);

fl.layoutContainer(c);

}

}

);

jb2 = JButton(“center”);

add(jb2);

jb2.addActionListener(

new actionListener(){

public void actionPerformed(Action Event){

fl.setAlignment(FlowLayout.CENTER);

fl.layoutContainer(c);

}

}

);

jb3 = JButton(“right”);

add(jb3);

jb3.addActionListener(

new actionListener(){

public void actionPerformed(Action Event){

fl.setAlignment(FlowLayout.Right);

fl.layoutContainer(c);

}

}

);

}

}

.

import javax.swing.*;

.

public class LayoutToHandle {

public static void main(String[] args){

HandlingLayout hl = new HandlingLayout();

hl.setVisible(true);

hl.setSize(300, 200);

hl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

.


 Java Programming Tutorial for Beginner – 84 – Drawing Graphics

.

import java.awt.*:

import javax.swing.*;

.

public class HandlingPanel extends JPanel{

public void paintComponent(Graphics g){

super.paintComponent(g);

this.setBackground(Color.WHITE);

g.setColor(Color.BLUE);

g.fillRect(25, 25, 100, 30);

g.setColor(new Color(190, 80, 215));

g.fillRect(25, 65, 100, 30);

g.setColor(Color.RED);

g.drawString(“It’s done on JPanel”, 25, 120);

}

}

.

import javax.swing.*;

public class PanelToHandle {

public static void main(String[] args){

HandlingPanel hp = new HandlingPanel();

JFrame jf = new JFrame(Handling Panel App”);

jf.add(hp);

jf.setSize(400, 250);

jf.setVisible(true);

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

.


 Java Programming Tutorial for Beginner – 85 – JColorChooser

.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

.

public class ChoosingColors extends JFrame{

private Color c = (Color.WHITE);

private JPanel jp = new JPanel();

private JButton jb = new JButton(“Choose a color, please!);

public ChoosingColors (){

super(Choosing Colors App”);

jp.setBackground(c);

jb.addActionListener(

new ActionListener(){

public void actionPerformed(ActionEvent AE){

c = JColorChooser.showDialog(null, “Pick a color, please!”, c);

if (== null)

= (Color.WHITE);

jp.setBackground(c);

}

}

);

add(jp, BorderLayout.CENTER);

add(jb, BorderLayout.SOUTH);

}

}

.

import javax.swing.*;

.

public class ColorsToChoose {

public static void main(String[] args){

ChoosingColors cc = new ChoosingColors();

cc.setVisible(true); 

cc.setSize(300, 200);

cc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

.


 Java Programming Tutorial for Beginner – 86 – Drawing More Stuff

.

import java.awt.*;

import javax.swing.*;

.

public class DrawingThings extends JPanel{

public void paintComponent(Graphics g){

super.paintComponent(g);

this.setBackground(Color.WHITE);

g.setColor(Color.BLUE);

g.drawLine(10, 25, 100, 45);

g.setColor(Color.RED);

g.drawRect(10, 55, 100, 30);

g.setColor(Color.GREEN);

g.draw3DRect(10, 55, 100, 30, true);

g.setColor(Color.ORANGE);

g.drawOval(10, 95, 100, 30);

}

}

.

import javax.swing.*;

.

public class ThingsToDraw {

public static void main(String[] args){

DrawingThings dt = new DrawingThings ();

JFrame jf = new JFrame();

jf.add(dt);

jf.setVisible(true);

jf.setSize(300, 300);

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

.


 Java Programming Tutorial for Beginner – 87 – equalsIgnoreCase

public class EqualsToHandle {

public static void main(String[] args){

String a = “apples”;

String b = “adrian”;

String c = “Adrian”;

System.out.println(a.length);

if(a.equals(“apples”))

System.out.println(“It equals apples!”);

if(b.equals(c))

System.out.println(“They match!”);

if(b.equalsIgnoreCase(c))

System.out.println(“They match.”);

}

}

.

Leave a Reply

Your email address will not be published. Required fields are marked *