Improve Your Java Skills – Series III

Java Tips

Java Tips – Series III

მიმოხილვა: In this ‘Java Tips’ series, we have mainly worked on the hands-on coding part. The objective of this series is to help you understand the coding part and implementation details. We have tried to cover various topics along with coding examples. You will also like our Java series I და Java series II








1) How to display a clock using an applet?

A: The example has used String class and Calendar class to display the applet clock. Calendar class gives the value of ‘Hours’, ‘Minutes’ and ‘second’.

იმპორტის java.awt. *;

import java.applet.*;

import java.applet.*;

იმპორტის java.awt. *;

import java.util.*;

public class ClockApplet extends Applet implements Runnable{

Thread t,t1;

public void start(){

t = new Thread(ამ);

t.start();

}

public void run(){

t1 = Thread.currentThread();

ხოლო(t1 == t){

repaint();

ვცდილობთ{

t1.sleep(1000);

}

catch(InterruptedException ვებ){}

}

}

public void paint(Graphics g){

Calendar cal = new GregorianCalendar();

String hour = String.valueOf(cal.get(Calendar.HOUR));

String minute = String.valueOf(cal.get(Calendar.MINUTE));

String second = String.valueOf(cal.get(Calendar.SECOND));

g.drawString(hour + “:” + minute + “:” + მეორე, 20, 30);

}

}

Again you have to run the applet in a browser or applet viewer to check the output.

2) How to create an event listener in Applet?

A: Following example demonstrates how to create a basic Applet having buttons to add & subtract two numbers. Methods used here are addActionListener () to listen to an event(click on a button) & Button() constructor to create a button.

import java.applet.*;
import java.awt.event.*;
იმპორტის java.awt. *;

public class EventListeners extends Applet

implements ActionListener{

TextArea txtArea;

String Add, Subtract;

int i = 10, j = 20, sum =0,Sub=0;

public void init(){

txtArea = new TextArea(10,20);

txtArea.setEditable(false);

დავამატებთ(txtArea,”center”);

Button b = new Button(“Add”);

Button c = new Button(“Subtract”);

b.addActionListener(ამ);

c.addActionListener(ამ);

დავამატებთ(ბ);

დავამატებთ(გ);

}

public void actionPerformed(ActionEvent e){

sum = i + j;

txtArea.setText(“”);

txtArea.append(“I = “+ i + “\t” + “j = ” + j + “\N”);

Button source = (Button)e.getSource();

თუ(source.getLabel() == “Add”){

txtArea.append(“Sum : ” + sum + “\N”);

}

თუ(i >j){

Sub = i – j;

}

სხვა{

Sub = j – i;

}

თუ(source.getLabel() == “Subtract”){

txtArea.append(“Sub : ” + Sub + “\N”);

}

}

}

3) How to open a link in a new window using Applet?

A: This example demonstrates how to open a particular webpage from an applet in a new window using showDocument() method with second argument as “_blank” .

import java.applet.*;

იმპორტის java.awt. *;

import java.net.*;

import java.awt.event.*;

public class testURL_NewWindow extends Applet

implements ActionListener{

public void init(){

String link_Text = “google”;

Button b = new Button(link_Text);

b.addActionListener(ამ);

დავამატებთ(ბ);

}

public void actionPerformed(ActionEvent ae){

Button source = (Button)ae.getSource();

String link = “http://www.”+source.getLabel()+”.com”;

ვცდილობთ {

AppletContext a = getAppletContext();

URL url = new URL(link);

a.showDocument(url,”_blank”);

}

catch (MalformedURLException e){

System.out.println(e.getMessage());

}

}

}








4) How to read a file using Applet?

A: The following code snippet demonstrates how to read a file using an Applet using openStream() method of URL. There are different ways to read a stream like buffered stream, customized buffered stream etc. აქ ჩვენ წაიკითხეთ ბუფერული ნაკადი მონაცემები,,en,საჯარო კლასის წაკითხვა FileApplet ვრცელდება აპლეტი,,en,სიმებიანი fileToRead =,,en,test1.txt,,en,StringBuffer strBuff,,en,სიმებიანი prHtml = this.getParameter,,en,fileToRead,,el,prHtml,,sl,fileToRead = ახალი სიმებიანი,,en,წაკითხული,,en,საჯარო ძალადაკარგულად წაკითხვადი,,en,სიმებიანი ხაზი,,en,URL url = null,,en,url = ახალი URL,,en,getCodeBase,,en,InputStream in = url.openStream,,en,BufferedReader bf = ახალი BufferedReader,,en,strBuff = ახალი StringBuffer,,en,ხაზი = bf.readline,,en,strBuff.append,,en,Ფაილის სახელი,,en,strBuff.toString,,en,როგორ შევქმნათ სხვადასხვა ფორმები აპლეტის გამოყენებით,,en,ეს მაგალითი გვიჩვენებს, თუ როგორ უნდა შექმნათ აპლეტი, რომელსაც ექნება ხაზი,,en,ოვალური,,en,მართკუთხედის გამოყენებით drawline,,en,drawOval,,en,და DrawRect,,en,გრაფიკული კლასების მეთოდები,,en,ჯავაში გრაფიკული კლასი საყოველთაოდ გამოიყენება დიაგრამების დახაზვაზე და ის გამოიყენება pie chart- ში,,en,ბარი დიაგრამა და ა.შ.,,en,საჯარო კლასის ფორმები ვრცელდება აპლეტი,,en,int x = 300, y = 100, r = 50,,en,g.drawLine,,en.

import java.applet.*;

იმპორტის java.awt. *;

import java.io.*;

import java.net.*;

public class readFileApplet extends Applet{

String fileToRead = “test1.txt”;

StringBuffer strBuff;

TextArea txtArea;

Graphics g;

public void init(){

txtArea = new TextArea(100, 100);

txtArea.setEditable(false);

დავამატებთ(txtArea, “center”);

String prHtml = this.getParameter(“fileToRead”);

თუ (prHtml != null) fileToRead = new String(prHtml);

readFile();

}

public void readFile(){

String line;

URL url = null;

ვცდილობთ{

url = new URL(getCodeBase(), fileToRead);

}

catch(MalformedURLException e){}

ვცდილობთ{

InputStream in = url.openStream();

BufferedReader bf = new BufferedReader

(new InputStreamReader(ამ));

strBuff = new StringBuffer();

ხოლო((line = bf.readLine()) != null){

strBuff.append(line + “\N”);

}

txtArea.append(“File Name : ” + fileToRead + “\N”);

txtArea.append(strBuff.toString());

}

catch(IOException ვებ){

e.printStackTrace();

}

}

}

5) How to create different shapes using Applet?

A: This example demonstrates how to create an applet which will have a line, an Oval & a Rectangle using drawLine(), drawOval() and drawRect() methods of Graphics class. In java Graphics class is widely used to draw diagrams and those are used in pie chart, bar diagram etc.

import java.applet.*;

იმპორტის java.awt. *;

public class Shapes extends Applet{

int x=300,y=100,r=50;

public void paint(Graphics g){

g.drawLine(30,300,200,10);

g.drawOval,,en,x-r,,en,y-r,,en,აღნიშნული კოდი ნიმუში წარმოებს შემდეგ შედეგებს java ჩართულია ვებ დაათვალიეროთ,,en,ხაზი,,en,ოვალური,,en,ბროუზერი იქნება მართკუთხედი,,en,როგორ გამოვიყენოთ JAVA- ში Swing აპლეტი?,,en,Swing არის მნიშვნელოვანი კომპონენტი java წარმოების UI ინტერფეისები,,en,ეს სვინგის კომპონენტები ასევე გამოიყენება აპლეტში, რათა მომხმარებლის ინტერფეისი. შემდეგ მაგალითში ცხადყოფს, თუ როგორ უნდა გამოიყენოთ Swing აპლეტი JAVA- ს მიერ ActionListener- ის განხორციელებით,,en,შექმნის JLabels,,en,საჯარო კლასის SApplet ვრცელდება Applet ახორციელებს ActionListener,,en,ტექსტის შეყვანა,,en,ლეიბლი label1, label2,,en,ღილაკი b1,,en,JLabel lbl,,en,int num,,ar,label1 = ახალი ლეიბლი,,en,გთხოვთ შეიყვანოთ ნომერი,,en,label1,,en,label1.setBackground,,en,label1.setForeground,,en,ფერი,,en,input = ახალი TextField,,en,label2 = ახალი ლეიბლი,,en,label2,,en,label2.setBackground,,en,label2.setForeground,,en,output = ახალი TextField,,en,b1 = ახალი ღილაკი,,en,b1.addActionListener,,en,int newN = theStack.pop,,el(x-r,y-r,100,100);

g.drawRect(400,50,200,100);

}

}

The above code sample will produce the following result in a java enabled web browse

A line, Oval & a Rectangle will be drawn in the browser.

6) How to use swing applet in JAVA?

A: Swing is an important component in java to produce UI interfaces. These swing components are also used in applet to make the user interface.Following example demonstrates how to go use Swing Applet in JAVA by implementing ActionListener & by creating JLabels.

იმპორტი javax.swing. *;

import java.applet.*;

იმპორტის java.awt. *;

import java.awt.event.*;

public class SApplet extends Applet implements ActionListener {

TextField input,output;

Label label1,label2;

Button b1;

JLabel lbl;

int num, sum = 0;

public void init(){

label1 = new Label(“please enter number : “);

დავამატებთ(label1);

label1.setBackground(Color.yellow);

label1.setForeground(Color.magenta);

input = new TextField(5);

დავამატებთ(input);

label2 = new Label(“Sum : “);

დავამატებთ(label2);

label2.setBackground(Color.yellow);

label2.setForeground(Color.magenta);

output = new TextField(20);

დავამატებთ(output);

b1 = new Button(“Add”);

დავამატებთ(b1);

b1.addActionListener(ამ);

lbl = new JLabel(“Swing Applet Example. “);

დავამატებთ(lbl);

setBackground(Color.yellow);

}

public void actionPerformed(ActionEvent ae){

ვცდილობთ{

num = Integer.parseInt(input.getText());

sum = sum num;

input.setText(“”);

output.setText(Integer.toString(sum));

lbl.setForeground(Color.blue);

lbl.setText(“Output of the second Text Box : ”

+ output.getText());

}

catch(NumberFormatException e){

lbl.setForeground(Color.red);

lbl.setText(“Invalid Entry!”);

}

}

}

7) How to write to a file using Applet?

A: In the following example java text area has been used to enter the user input. After entering the input it has been written in a file in local file system. The File () constructor has been used to create the file.

import java.io.*;

იმპორტის java.awt. *;

import java.awt.event.*;

იმპორტი javax.swing. *;

import java.applet.Applet;

import java.net.*;

 

public class WriteFile extends Applet{

Button write = new Button(“WriteToFile”);

Label label1 = new Label(“Enter the file name:”);

TextField text = new TextField(20);

Label label2 = new Label(“Write your text:”);

TextArea area = new TextArea(10,20);

public void init(){

დავამატებთ(label1);

label1.setBackground(Color.lightGray);

დავამატებთ(text);

დავამატებთ(label2);

label2.setBackground(Color.lightGray);

დავამატებთ(area);

დავამატებთ(write,BorderLayout.CENTER);

write.addActionListener(new ActionListener (){

public void actionPerformed(ActionEvent e){

new WriteText();

}

}

);

}

public class WriteText {

WriteText(){

ვცდილობთ {

String str = text.getText();

თუ(str.equals(“”)){

JOptionPane.showMessageDialog(ნულოვანი,

“Please enter the file name!”);

text.requestFocus();

}

სხვა{

File f = new File(ქუჩაზე);

თუ(f.exists()){

BufferedWriter out = new

BufferedWriter(ახალი FileWriter(ვ,ნამდვილი));

თუ(area.getText().უდრის(“”)){

JOptionPane.showMessageDialog

(ნულოვანი,”Please enter your text!”);

area.requestFocus();

}

სხვა{

out.write(area.getText());

თუ(f.canWrite()){

JOptionPane.showMessageDialog(ნულოვანი,

“Text is written in “+ქუჩაზე);

text.setText(“”);

area.setText(“”);

text.requestFocus();

}

სხვა{

JOptionPane.showMessageDialog(ნულოვანი,

“Text isn’t written in “+ქუჩაზე);

}

out.close();

}

}

სხვა{

JOptionPane.showMessageDialog

(ნულოვანი,”File not found!”);

text.setText(“”);

text.requestFocus();

}

}

}

catch(Exception x){

x.printStackTrace();

}

}

}

}








8) How to print summation of n numbers?

A: Following example demonstrates how to add first n natural numbers by using the concept of stack. Here the static Stack class is used to hold the numbers. The custom stackAddition () method is used to add the numbers into the stack.

იმპორტი java.io.IOException;

public class AdditionStack {

static int num;

static int ans;

static Stack theStack;

საჯარო სტატიკური ბათილად მთავარი(სიმებიანი[] args) ისვრის IOException {

num = 50;

stackAddition();

System.out.println(“Sum=” + ans);

}

public static void stackAddition() {

theStack = new Stack(10000);

ans = 0;

ხოლო (num > 0)

{

theStack.push(num);

–num;

}

ხოლო (!theStack.isEmpty())

{

int newN = theStack.pop();

ans = newN,,el,კერძო int,,en,int s,,en,maxSize = s,,en,int p,,la,= p,,en,საჯარო Int pop,,en,დაბრუნების მონაცემები,,en,საზოგადოების ინექცია,,en,თანხა = 1225,,en,როგორ მივიღოთ ბმულზე პირველი და ბოლო ელემენტი,,en,შემდეგ მაგალითს გვიჩვენებს, თუ როგორ უნდა მივიღოთ ბმულის პირველი და ბოლო ელემენტი დაკავშირებულ სიაში დახმარებით,,en,და linkedlistname.getLast,,en,LinkedList კლასი,,en,ყველა დასტის / რიგის / deque ოპერაციების შეიძლება ადვილად recast თვალსაზრისით სტანდარტული სია ოპერაციების,,en,ისინი შედის აქ პირველ რიგში კომფორტული,,en,თუმცა მათ შეუძლიათ ოდნავ უფრო სწრაფად აწარმოონ, ვიდრე ექვივალენტური სია ოპერაციები,,en,LinkedList lList = ახალი LinkedList,,en,lList.add,,en,LinkedList- ის პირველი ელემენტია,,en,lList.get პირველი,,en,LinkedList- ის ბოლო ელემენტია,,en,lList.getLast,,en,როგორ დაამატოთ ელემენტი ელემენტის სიის პირველი და ბოლო პოზიცია,,en;

}

}

}

class Stack {

private int maxSize;

private int[] data;

private int top;

public Stack(int s) {

maxSize = s;

data = new int[maxSize];

top = -1;

}

public void push(int p) {

data[++top] = p;

}

public int pop() {

return data[top–];

}

public int peek() {

return data[top];

}

public boolean isEmpty() {

return (top == -1);

}

}

The above code sample will produce the following result.

Sum=1225

9) How to get the first and the last element of a linked list?

A: Following example shows how to get the first and last element of a linked list with the help of linkedlistname.getFirst() and linkedlistname.getLast() of LinkedList class. All of the stack/queue/deque operations could be easily recast in terms of the standard list operations. They’re included here primarily for convenience, though they may run slightly faster than the equivalent List operations.

import java.util.LinkedList;

საჯარო კლასის მთავარი {

საჯარო სტატიკური ბათილად მთავარი(სიმებიანი[] args) {

LinkedList lList = new LinkedList();

lList.add(“100”);

lList.add(“200”);

lList.add(“300”);

lList.add(“400”);

lList.add(“500”);

System.out.println(“First element of LinkedList is :

” + lList.getFirst());

System.out.println(“Last element of LinkedList is :

” + lList.getLast());

}

}

The above code sample will produce the following result.

First element of LinkedList is: 100

Last element of LinkedList is: 500

10) How to add an element at first and last position of a linked list?

A: შემდეგ მაგალითს გვიჩვენებს, თუ როგორ დაამატოთ ელემენტი ელემენტის პირველ და ბოლო პოზიციაზე დამაკავშირებელი სიის გამოყენებით,,en,დაამატეთ მეტი,,en,უკავშირდება სიის კლასი,,en,სია,,ca,lList.add პირველი,,en,lList.addLast,,en,techalpine.com/improve-your-java-skills-series-iii,,en() and addLast() method of Linked List class.

import java.util.LinkedList;

საჯარო კლასის მთავარი {

საჯარო სტატიკური ბათილად მთავარი(სიმებიანი[] args) {

LinkedList lList = new LinkedList();

lList.add(“1”);

lList.add(“2”);

lList.add(“3”);

lList.add(“4”);

lList.add(“5”);

System.out.println(lList);

lList.addFirst(“0”);

System.out.println(lList);

lList.addLast(“6”);

System.out.println(lList);

}

}

The above code sample will produce the following result.

1, 2, 3, 4, 5

0, 1, 2, 3, 4, 5

0, 1, 2, 3, 4, 5, 6







 

============================================= ============================================== Buy best TechAlpine Books on Amazon
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share