Professional Documents
Culture Documents
import java.util.Scanner;
import java.util.*;
- 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");
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 )
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();
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 = ???)
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() ); }}
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 :
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:
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 ");
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:
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
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,
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);