You are on page 1of 7

Java language Basics (part 1)

At the beginning of the program:


At the beginning of the program you can import some ready programs : As an examples the Scanner class :

import java.util.Scanner;

To import a package which consist of several related classes:

import java.util.PackegeName; To import all classes which are in package java.util:

import java.util.*;

Before the main:


To add comments (anywhere in the program) you can use one of the following ways: // ( it terminates at the end of the line ) /* .. */ ( begins with /* and ends with */ ) /** and */ enable you to embed program documentation directly in your programs

Some important information:

- For printing on the screen 1- A line and the cursor then will be at the same line System.out.print (" write the string that you want to print");

Done by: Abeer(www.UOB-BH.com)

2- A line then the cursor will go to the beginning of the next line : System.out.printIn(" write the string that you want to print");

3- To print anything with a format : System.out.printf(" the format", argument list ); To print a string use (%s) To print an int use (%d) To print floating point value in decimal format (124.56) use (%f) Ti print floating point value in exponential format (1.2362e+02) use (%e) To print a single character use (%c) To print an integer number from the right (% totalNumber Type ) To print an integer number from the left (% - totalNumber Type )

Example: int x = 10, y = 125; System.out.printf(x = %4d y = %-4d \n, x, y);

To print an float number from the right (% totalNumber .numbers after the floating point Type ) To print an float number from the left (% - totalNumber .numbers after the floating point Type )

Example: float x = 123.94163; System.out.printf(x = %9.3f \n, x); System.out.printf(x = %9.3f \n, x);

4- Another way for printing by using print or printIn with (+) : System.out.printIn(" write the string that you want to print"+ argument );

To read information from the user we can use class Scanner by this way : Int x; Scanner a= new Scanner (system.in); System.out.print("Enter first integer"); X=a.nextInt();

Done by: Abeer(www.UOB-BH.com)

To save a byte we use (nextByte) To save short type we use (nextShort) To save int we use (nextInt) And the same for long,float and double To save string we use (next ) to save only one word or (nextLine ) to save the complete line

Note: you should import class Scanner at the beginning of the program by writing: (import java.util.Scanner;)

To declare a static variable you can use ( final datatype variableName = ???)

Example: final int SIZE = 100; final double PI = 3.14159;

To create and declare an object

ClassName ObjectName = new ClassName();

How to write a class?


There are some important notes: - The first letter of the class name only must start with uppercase letter - The file should have the same name of the class. If your file has more than one class, then one of them should be public (which has the same file name) and the others are not. - Each data member (attributes)and each member function(method) should begin with the word (private/public or protected) - Example: public class GradeBook { private String courseName; public GradeBook( String name ) { courseName = name; // initializes courseName } public void setCourseName( String name ) { courseName = name; // store the course name } public String getCourseName( ) { return courseName;} public void displayMessage( ) { System.out.printf( "Welcome to the grade book for\n%s!\n", getCourseName( ) );} }

Done by: Abeer(www.UOB-BH.com)

How to write the main?


The main must be in a class It always begins by this statement : Public static void main (string[] args) You must create and declare an object for any member function of a class that you want to use Example:

public class GradeBookTest { public static void main( String[] args ) { GradeBook gradeBook1 = new GradeBook ITCS341 Object Oriented Systems"); GradeBook gradeBook2 = new GradeBook( ITCS 393 Java Concepts" ); System.out.printf( "gradeBook1 course name is: %s\n",gradeBook1.getCourseName() ); System.out.printf( "gradeBook2 course name is: %s\n",gradeBook2.getCourseName() ); }}

How to draw a UML class diagram


Class name (+/-) data members1:type (+/-) data members2: type . (+/-) data membersN: type (+/-) methode1 ( parameter : dataType ): return type (+/-)methode2 ( parameter : dataType ): return type . (+/-)methodeN ( parameter : dataType ): return type (+/-)constructor ( parameter : dataType ) // used for consrtractior and copy constructor only -

We use (-) for private and (+) for public When the return type is void we don't write any thing

GUI based Input/Output using Dialog boxes You have to import class JOptionPane by writing at the beginning :

import javax.swing.JOptionPane; To display a dialog with a message only :

JOptionPane.showMessageDialog( null, "the message" );

Done by: Abeer(www.UOB-BH.com)

To prompt the user to enter a value :

String Variable1 = JOptionPane.showInputDialog( "message " ); -

To print a message with the value entered by the user (previous ): 1- Create the message String variable2 = String.format( "format!", Variable1 ); 2Display the message:

JOptionPane.showMessageDialog( null, variable2 );

Example: import javax.swing.JOptionPane; public class NameDialog{ public static void main( String[] args ){ // prompt user to enter name String name = JOptionPane.showInputDialog( "What is your name?" );// create the message String message = String.format( "Welcome, %s, to Java Programming!", name ); // display the message to welcome the user by name JOptionPane.showMessageDialog( null, message ); } // end main } // end class NameDialog

If the user enters a number not a string then the number he entered will be save as a string so, you must convert it as follows:

1- Display a messege for a user to enter a number and then save it: String variable1 = JOptionPane.showInputDialog(null, "message ");

2- Convert the string : Variable2 = Typeclass.parseType(Variable1);

Typeclass : to convert to double = Double , to convert to integer = Integer and so on. ParseType Will be choose depend on the new type that you want to convert to:

Done by: Abeer(www.UOB-BH.com)

Type Byte Short Integer Long Float Double

Method parseByte parseShort parseInt parseLong parseFloat parseDouble

3- Display the message: JOptionPane.showMessageDialog( null, variable2 );

Example: import javax.swing.JOptionPane; public class Circle { public static void main( String[ ] args ) { final double PI = 3.14159; double radius, area, circumference; String radiusStr; radiusStr = JOptionPane.showInputDialog(null, "Enter radius: "); // read radius from user as String radius = Double.parseDouble(radiusStr); //convert to a double value area = PI * radius * radius; circumference = 2.0 * PI * radius; String msg = String.format( "Area = %7.3f \n Circumference = %7.3f \n, area, circumference ); JOptionPane.showMessageDialog(null,msg); } // end method main } // end class Circle

Some additional information:


You can merge two strings of a string with a value by using (+)

Example: Suppose we want to write a class called Bicycle and we want to assign a tag number when a new instance of the Bicycle class is created. Suppose we want the tag numbers to be ABC-101, ABC-102, ABC-103,

Done by: Abeer(www.UOB-BH.com)

public class Bicycle {private String owner; // name of Bicycle owner instance variable private String tag; // Bicycle tag instance variable private static int count = 100; // class variable // Whenever a new instance of class Bicycle is created, a unique // tag is created. public Bicycle( String name ) { owner = name; count++; tag = ABC- + count; } // end constructor }

You can use (this ) in any class function to set the data, but it must be the first statement in the function

Example: public Fraction( ) { this(0, 1); // A call to another constructor which will set the first data member to 0 //and the second data member to 1 }

Static method:
You can write a static method in the class by this way : public static returnType functionName(parameters);

To call the static method in the main there is no need for an object. You can call it in this way:

ClassName.FunctionName (parameters); Example: In order to find minimum of two integers we need only these two integers and if these two integers are passed as parameters, then we do not need any other data value. So, we can make method min as static. public class MyMath {public static int min(int x, int y) { if(x >= y) return x; else return y;}} main int n= 10, m = 20, r; r = MyMath.min (n, m);

Done by: Abeer(www.UOB-BH.com)

You might also like