الجافا سؤال وجواب
DESCRIPTION
الجافا سؤال وجوابTRANSCRIPT
. (
...
..
:
.................. : ....
................... :
.................. :
.......... ....:
................... :
................ :
:class...............
.................variablevalue:
: ...................
...........:
....private & protected & public:
:.
.... :
... :
......... :
.....:
:Packages..........
....:overriding & overloading
............:
..... :static
.......*. : java.lang
...... && : &
) ( .....() :toString
.......... :
........ :Casting
................. :
...global & local & static & dynamic:
:
Exceptions(
:
............. :
...............
:
:
...............Generic Class:
...............Interface:
GUI
....... :GUI
:JFrame..........
.........JFrame:
....... :
..........:Swing
.......:static & methods
......... :
......Color:
:
.........:
....... :Properties
.............Locale:
...........ResourceBuodle:
......... :
......finally:
...... :Scanner
:
......Java Annotations:
..... :
Databases
: ......
Graphics 2D
................:
........... :
:capture ..........
:splash.............
:open net..........................
:count letter....
:
))Java
++)C(
" "
(
)
.
:
.
.
.
.
.
.
.
.
.
NetBeansEclipse.
: Java SE
))Java Standard Edition
)StandAlone applications.(
Java EE ))Java Enterprise Edition
. Java ME
))Java Micro Edition
.
Java Fx
"RIA" "Rich Internet Applications."
:
( ) .
:
: class IDE
Refactor
rename
: variablevalue
Variable: .
Value: .
(Constructor) :
:
: . .
: . .
public
.
The KeyWord "THIS"
.
key word "SUPER "
.
: (
)
:
public constructor(){}
public constructor(int a){}
public constructor(int a,int b){} ...
.
: )(private ) ( protected
(public)
( )acces modefires
.
:)private (
:)( protected
(
) .
:(public)
.
:
Default Access Modifier .
: Default Access Modifier
Access Modifier
private
protectedpublic .Default
.pakage
=
int i = 500;
i
..
)
StudentName
+
/
*
*
×
)
+
-
++
--
<= & >=
==
{ }
}
System.out.print(" past");
if
Methods: ) (
:
Getter:accessors methods
return .
()frog.getPostion: .
Setter: Mutater methods
)frog.setColour(RED:return
.
packages:
. import
(*) .
:
-standered java backages-------< .
-developer java packages-------< .
overriding & overloading :
overriding:
.
overloading: .
:
primitive data type<< :
int, double, byte, etc
refrenace type<< : .String , Array
escape sequence:
)\(
:
'\n' .
:static methods & variables
static methods OR variables
.
static variables variable
.
* : java.lang.
.
: &)(&)&(
:
&) :&(
( )
.
:
public void and()
System.out.println("case one");
System.out.println (b);
")")case one
..
..
&) :(
..
public void and_and ()
System.out.println("case two");
System.out.println (b);
}
")")case two
b ..
..
: ()toString
Object
.Object
String
Sun toString
()toStringpublic String
a string that "textuallythe toString method returnsof the object. In general,string representationReturns a
that is easy for a personconcise but informative representation. The result should be arepresents" this object
to read. It is recommended that all subclasses override this method.
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
}
System.out.println(p);
overridetoString .Object
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
old.";
}
}
javac arabteam.java
: compile
compile
. java
.class
java
(
Casting:
() :
;12=c,24= b,35=aint
:Constants) (
final .
:;final int s = 2
:
global
local
static
dynamic
global ..
[local
..
Static
: String and Stringbuffer
String: new
.RunTime
Stringbuffer: new
:
import java.util.GregorianCalendar
.
)
(Inheritance
: )(inheritance
( )...
.SubClass)(
:
SubClass)(
:
)(set ))get .
:
.
Exceptions)(
:
.
:
:) (Exception handle
-
.throw the exception
- exception
.
-
catch the exception.
:
-Errors class and its subclasses
:
-Exception class and its subclasses
Checked exceptions
-Runtime exceptions and its subclasses
compiler Uncheck Exception
-* Checked exceptions :
-Try.. catch blocks
:
try
finally block try
.
- exception :in the method header
Throws ..
public Boolean check format (String fileName) throws EO
Exception
;:System.out.print ('1'+1)
!
character 1
'a'
'a' +1
char Decemal
http://www.unfiction.com/dev/tutorial/ascii.html
:Deprecated
.Class1
Class1
Class2 Class1
.
Class1 deprecated
) (
.Class2 .
Deprecated Methods
JFrame: DeprecatedShow
setVisible
Generic class:
Type Generic
GenericType
Compiler
Time Type
.
: Interface
Abstract .
* : .implements
* .new
* .extends
*
implements .
GUI
(GUI: )
• GUI
.
• JOptionPane
()showInputDialog .
: ) )JFrame
•JFrame ) .)GUI
JFrame•
.
: JFrame
• )(object .JFrame
• ())(extends .Jframe
:
:Frame
swing
import javax.swing.*;
Frame2.setVisible(true); //
Frame2.setLocation(200,60); //
Frame2.setResizable(false); //
Frame2.setBackground(Color.BLUE); vcvcvcvcvv
new MainClass().setVisible(true);
Dimension dim =
int w = Frame2.getSize().width;
// Move the window
() Pack
:
Frame2.setSize(width, height);
:setSize
Example :
private Button button1 = new Button("Button 1");
private Button button2 = new Button("Button 2");
private Button button3 = new Button("Button 3");
private Button button4 = new Button("Button 4");
private Button button5 = new Button("Button 5");
public Pack() {
this.addWindowListener(new WindowAdapter() {
Pack mainFrame = new Pack();
}
}
JLabel t2 = new JLabel(message);// message is a String variable
o
Label
setText() method
t1.setText("New Text"); // You can also use variables here
o
setText(String)
JButton
:
setVisible() Frame2.add(b);
JTextField
JTextField tf = new JTextField(); // The default
constructor
JTextField of size
and default text
o
() setText
JFrame
frame.add(new JTextField("Hello World", 25)); // or // frame.add(tf);
o
combbox
JTextField
size and default text
JTextField
o : String Object
Objects
com.addItem(comtxt);
o CombBox setEditable
com.setEditable(true);
list list = new JList(data); //data has type Object[]
list
DefaultListModel
o
JList list = new JList(listModel);
index
listModel.remove(index); o:
listModel.clear();
list.setSelectedItem(n);
JList
s = (String) list.getSelectedValue();
JListobject
JList.setSelectionMode(ListSelectionModel. MULTIPLE_INTERV
: ListSelectionModel
- MULTIPLE_INTERVAL_SELECTION //(default select)
- SINGLE_INTERVAL_SELECTION // - SINGLE_SELECTION
// Create a list that allows adds and removes
DefaultListModel model = new DefaultListModel();
JList list = new JList(model);
String[] items = {"A", "B", "C", "D"};
for (int i=0; i<items.length; i++) {
listModel.add(i, items[i]);
pos = 0;
listModel.add(pos, "a");
listModel.set(pos, "b");
listModel.remove(pos);
}
o :list
// Remove all items
Swing:
:( … )
GUI Sun Microsystems Java
Swing.
:
.
: SWT
JavaGUI .SWT IBM
IBM SWT
)Heavyweight )
Look and Feel
SWT Swing
.Windows Centric
: staticclassmethod
.className.method
:
swing .1
http://docs.oracle.com/javase/tutorial/uiswing/ swing
http://docs.oracle.com/javase/6/docs/technotes/guides/awt/
IBM swt .3
http://www.eclipse.org/swt/
googl
gwt .4
. RIA javafx UI .5
http://docs.oracle.com/javafx/
QT .6
http://qt-jambi.org/
GTK.7
http://java-gnome.sourceforge.net/
Color
)lowercase)orange
JDK 1.4 )uppercase)ORANGE
(Upper_Case .)
:
)class(Desktop )Method )browse
//get a Desktop object
try {
//open the default browser using the method browse which take an URI
object representing the web page
desktop.browse(new URI("http://arabteam2000-forum.com/" ));
}
:
.
. properties
.properties
properties:
:
"labels"
"_"
"ar"
"fr"
"en."
labels_ar.properties
....
.key
properties ISO-8859-1
unicode
.
java.util.Locale :
.
.Locale
Locale .constructor
Locale arLocale = new Locale("ar");
--<2/php/code_list.php-hp://www.loc.gov/standards/iso639
java.util.ResourceBundle :
properties
Locale
.
getString .key
ResourceBundle getBundle
properties
labels_en.properties
"labels" .getBundle
.
:
BufferedReader
Scanner:
BufferedReader :readLine
BufferedReader FileReaderconstructor
loop String
.
loop ()readLine null
.
BufferedReader reader = null;
}
}
: finallyreader
//...
//...
readLine
IOException
catch bloc
.
finally bloc
exception
.
: Scanner
Scanner scanner = new Scanner(new File("myfile.txt"));
loop ()nexLine
.
while (scanner.hasNextLine()) {
JPA:
Java Persistence APIs
framework- - )Database( OOP
. -
- ..
JPA ORM)(Object Relationship Mapping
.
: JDKJREJVM
..
!
| | | | |
Compiler
jre
JRE (Java Runtime Environment)
.
JVM (Java Virtual Machine)
.class .java
Database
Databases
( ) .Data Structure
.
(
).runtime
.
.
.Data StructureRunTime
C++C
.
.Collectioninterface
Collection interface
interface:SetList.
Set .
Two MethodConCrete Class
.equalshashcode
List .
Map Interfaca Collection
.
SetList
new Listnew Setnew
ConCrete
:
Map .TreeMapHashMap
Tree Sorted .
: Collection
SetListQueue
.
Collection .
Collection
IteratableIterate
Iteratable Interface Iterator>--
.Constructor
Iteratable Interface:
Iterator(() )
Iterator :
()boolean next
()void remove
.
:collection interface
_ Informative Methods:
()Iterator Iterator
()boolean isEmpty
collection )true Or false) boolean
()int size
_ Object Based Methods:
)boolean equals(Object o
.
()int hashCode
.
_ Element Based Methods:
)boolean add(Object o
.
)boolean remove(Object o
.
)boolean contins(Object o
_ Output To Arrays Methods:
()Object[] toArray
.Arrays
)Object[] toArray(Object[] o
.
_ Collection Based Methods:
.AB
)boolean addAll(Collection c
.
)boolean containsAll(Collection c
)boolean removeAll(Collection c
.
()void clear
A .
)boolean retainAll(Collection c
:arrayListLinkedList
ArrayListLinkedList List
LinkedList
Queue , poll , peekArrayList)(
ArrayListarray
LinkedListsetget
setget
..arrayList ..
:LinkedList
List<String> staff = new LinkedList<>(); // LinkedList implements
List
iter.next(); // visit second element iter.remove(); // remove last
visited element
: List<String> list = new LinkedList<String>();
LinkedList
ArrayList
ArrayList
LinkedListArrayList LinkedList
ArrayListList interface ..
: (sorting)
Arrays.sort argument
System.out.println("array1 before sorting: " + Arrays.toString(array1));
:
compareTo
/
. comments
annotations
annotation processor :
..
annotations
JavaSE 5.
annotations
.
:
Override:@
override
.
compiler
)method signature(
.
:
override
toString
Object.
"S"
toStringlower caseupper case .
public class TestOverride {
:
TestOverride.java:2: method does not override or implement a method from a supertype
@Override
^
error @Override
override
.
Deprecated:@
deprecated
compilation
)compiler warning.(
compilation
public class TestDeprecated {
}
}
:compiler
Note: TestDeprecated.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details .
-Xlint $ javac -Xlint TestDeprecated.java
d.someMethod()
warning
SuppressWarnings:@
.compiler
}
}
unchecked
java.util.List<String> list = new java.util.ArrayList();
@SuppressWarnings compilation
:
}
}
Annotations
:
.
meta-annotations
meta-annotations
)annotating the annotation.(
)Override, @Deprecated,
@SuppressWarnings(@ -meta
.
Target:@ :
ElementType.CONSTRUCTOR: .constructor
ElementType.FIELD: .
ElementType.LOCAL_VARIABLE: .
ElementType.METHOD: .
ElementType.PACKAGE: .
ElementType.PARAMETER:
constructor.
ElementType.TYPE: .enum
constructor :
@Target(ElementType.CONSTRUCTOR)
:
.
Retention:@ meta-annotation
)scope(
:
RetentionPolicy.SOURCE:
.class
)compiler, javadoc
Override, @SuppressWarnings.@
RetentionPolicy.CLASS:
.class
.
RetentionPolicy.RUNTIME:
.class
Virtual Machine
.reflection
public @interface ConstructorAnnotation {}
Inherit:@
)subclasses(
.
.
:
.Markup Annotation
}
.
)method(
.
.
:
)primitive types.(
String
java.lang.Class
enum
java.lang.annotation.Annotation
.
:
public @interface AnAnnotation {
// .
int id();
// .
String description() default "This is an annotation for testing";
}
public class SomeClass {
//...
}
: reflection .apt
reflection
reflection
: @.RetentionRetentionPolicy.RUNTIME
reflection
java.lang.reflect.AnnotatedElement.
:Class, Constructor, Field, Method,
public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)
) constructor
(false
.
marker annotations.
.
Annotation[] getAnnotations()
.
.
Annotation[] getDeclaredAnnotations()
.
.
JavaBeans
.
.TableBean
reflection
RetentionPolicy.RUNTIMERetention.@
JavaBeans
ElementType.TYPETarget.@
}
.
JavaBean
.
) ...)NULL, NOTNULLprimary key, foreign key, unique
.
.IntVarChar Int
VarChar length
.
@Retention .RetentionPolicy.RUNTIME
@Target ElementType.FIELD
}
.
.Class
)isAnnotationPresent(TableBean.class
@.TableBean
.
@Int @VarChar
)IntVarChar( .Hashtable
.SQL
import java.lang.reflect.Field;
import java.util.Hashtable;
//A Hashtable containing the name of the column, and its
type
Hashtable<String, String>();
StringBuilder query = new StringBuilder();
annotation
*/
}
}
}
@Int private int age;
Class[] c = {Person.class};
}
}
.
Databases
: )access(
NetBeans
.…
DB Mouses
Control Panel
)Data Source (ODBC
add ODBC
NetBeans
add
-
)
(
ok
…………………………………………..
Source
SQL
Import java.sql*
)Method(
:
– con
- sql
– search
- rs
)
(
try – catch
-Class forName
- DB
–
- dtm
&
– dtm
ID
– DB
- – dtm
- – DB
Connect
JTextField3
Search
Update
BRAND
Graphics 2D
:
Swing .JPanel
paintComponent.
Graphics
.
) Graphics
drawRect(int x, int y, int width, int height)
.xy width
.
fillRect(int x, int y, int width, int height)
.xy width
.
drawOval(int x, int y, int width, int height)
.xy width
.
.xy width
.
.x1y1 x2y2
.
setColor(Color c)
.
Graphics
.Sun
JFrame frm = new JFrame();
- " ".
. .
. (CLS (
CLS C1 C2 C3 C4 C5 C6 C7
Paint P=new Paint();
{
public void BCircle(Graphics g, int xc, int yc, int r){
int x = 0, y = r, s = 3-(2*r);
while(x<=y){
g.drawLine(xc+x, yc-y, xc+x, yc-y);
g.drawLine(xc-x, yc+y, xc-x, yc+y);
g.drawLine(xc-x, yc-y, xc-x, yc-y);
g.drawLine(xc-y, yc-x, xc-y, yc-x);
double ct,st;
double xt;
double x,y;
}
}
g.drawLine(x, y, x, y);
count=0;
count=0;
count=0;
count=0;
count=0;
count=0;
count=0;
screen.jpg
* To change this license header, choose License Headers in Project
Properties.
* and open the template in the editor.
*/
*/
f=new JFrame("capture image");
JOptionPane.showMessageDialog(null,"
"+File.pathSeparator);
} catch (IOException ex) {
}
}
-splash
// Splash.java
public static void main(String[] args) {
// Throw a nice little title page up on the screen first
showSplash(3000);
}
// A simple little method to show a title screen in the
// center of the screen for a given amount of time.
public static void showSplash(int duration) {
JWindow splash = new JWindow();
splash.setBounds(200,100,455,500);
JLabel copyrt = new JLabel
try { Thread.sleep(duration); } catch (Exception e) {}
splash.setVisible(false);
Desktop desktop= Desktop.getDesktop();
//open the default browser using the method browse which take
an URI object representing the web page
desktop.browse(new URI("http://google.com"));
-count letter
import javax.swing.JOptionPane;
class CountEachLetter {
/** Main method */
// Prompt the user to enter a string
String s = JOptionPane.showInputDialog("Enter a string:");
// Invoke the countLetters method to count each letter
int[] counts = countLetters(s.toLowerCase());
String output = "";
// Display results
if (counts[i] != 0)
output += (char)('a' + i) + " appears " +
}
static int[] countLetters(String s) {
for (int i = 0; i < s.length() ; i++) {
-draw time
import java.awt.* ;
import java.awt.event.* ;
import javax.swing.* ;
{ public MaFenetre ()
{ setTitle ("PENDULE") ;
panControles.add (ok) ;
ok.addActionListener (this) ;
{ int h, m ; // pour les valeurs saisies
if (e.getSource() == ok)
saisieHeures.setText ("") ;
saisieMinutes.setText ("") ;
// si les valeurs obtenues sont valides, on les place dans
// les champs heures et minutes et on force le dessin
// sinon, on replace les anciennes valeurs dans les champs texte
repaint() ;
{ heures = h ; minutes = m ;
}
{ super.paintComponent(g) ;
boolean panTropLarge = (largeur>hauteur) ;
int rayon ;
g.drawOval (xCentre-rayon, yCentre-rayon, 2*rayon, 2*rayon) ;
// dessin grande aiguille
int minutes = fen.getMinutes() ;
g.drawLine (xCentre, yCentre,
(int)(xCentre+rayon*Math.cos(angle)),
g.drawLine (xCentre, yCentre,
(int)(xCentre+rayon/2.*Math.cos(angle)),
{ MaFenetre fen = new MaFenetre() ;
import java.awt.Color;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Action;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
mo n=new mo();
-calc
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import java.util.Currency;
import java.util.Set;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
calc ali=new calc();
// icon = new ImageIcon ("k1.jpg");
p1.setBounds(20,100,100,100);
p2.setBounds(200,100,100,100);
p3.setBounds(380,100,100,100);
// p15.setBounds(450,100,60,20);
p4.setBounds(20,250,100,100);
p5.setBounds(200,250,100,100);
p6.setBounds(380,250,100,100);
p7.setBounds(20,400,100,100);
p8.setBounds(200,400,100,100);
p9.setBounds(380,400,100,100);
p15.setBounds(550,400,100,100);
}}}
}
:
maisam alrawi
Mai Kamel Amro
Mostafa Anter
Sabeel Akhras