Learn Basic Programs
By Salar Mastoi
[Type the abstract of the document here. The abstract is typically a short summary of the contents of the document. T
IN JAVA, DATA TYPES SPECIFY THE SIZE AND TYPE OF VALUES THAT CAN
BE STORED IN VARIABLES. JAVA HAS TWO CATEGORIES OF DATA TYPES:
PRIMITIVE DATA TYPES AND REFERENCE DATA TYPES.
1. PRIMITIVE DATA TYPES:
- NUMERIC TYPES:
- `BYTE`: 8-BIT SIGNED INTEGER. RANGE: -128 TO 127.
- `SHORT`: 16-BIT SIGNED INTEGER. RANGE: -32,768 TO 32,767.
- `INT`: 32-BIT SIGNED INTEGER. RANGE: -2^31 TO 2^31 - 1.
- `LONG`: 64-BIT SIGNED INTEGER. RANGE: -2^63 TO 2^63 - 1.
- `FLOAT`: 32-BIT FLOATING-POINT. EXAMPLE: 3.14F.
- `DOUBLE`: 64-BIT FLOATING-POINT. EXAMPLE: 3.14.
- **BOOLEAN TYPE**:
- `BOOLEAN`: REPRESENTS TRUE OR FALSE.
- **CHARACTER TYPE**:
- `CHAR`: 16-BIT UNICODE CHARACTER. EXAMPLE: 'A', '\U0041'.
2. **REFERENCE DATA TYPES**:
- **CLASSES**: USER-DEFINED DATA TYPES.
- **INTERFACES**: LIKE CLASSES BUT WITH ONLY METHOD
DECLARATIONS .
- **ARRAYS**: COLLECTIONS OF ELEMENTS OF THE SAME TYPE.
HERE'S AN EXAMPLE DEMONSTRATING THE DECLARATION
AND USAGE OF DIFFERENT DATA TYPES:
PUBLIC CLASS DATATYPESEXAMPLE {
PUBLIC STATIC VOID MAIN(STRING[] ARGS) {
// PRIMITIVE DATA TYPES
BYTE B = 10;
SHORT S = 100;
INT I = 1000;
LONG L = 100000L; // NOTE: L OR L SUFFIX TO
INDICATE LONG
FLOAT F = 3.14F; // NOTE: F OR F SUFFIX TO
INDICATE FLOAT
DOUBLE D = 3.14159;
BOOLEAN BOOL = TRUE;
CHAR C = 'A';
// REFERENCE DATA TYPES
STRING STR = "HELLO, WORLD!";
INT[] ARR = {1, 2, 3, 4, 5};
PUBLIC CLASS FIRST {
PUBLIC STATIC VOID MAIN(STRING[] ARGS) {
SYSTEM.OUT.PRINTLN("HELLO, WORLD!");
}
}
PUBLIC CLASS SECOND {
PUBLIC STATIC VOID MAIN(STRING[] ARGS) {
SYSTEM.OUT.PRINTLN("BS DATA SCIENCE!");
}
}
PUBLIC CLASS THIRD {
PUBLIC STATIC VOID MAIN(STRING[] ARGS) {
SYSTEM.OUT.PRINTLN("SALAR HERE PROGRAMMER!");
}
}
IMPORT JAVA.UTIL.SCANNER;
PUBLIC CLASS ADDITION {
PUBLIC STATIC VOID MAIN(STRING[] ARGS) {
SCANNER SCANNER = NEW SCANNER(SYSTEM.IN);
SYSTEM.OUT.PRINT("ENTER FIRST NUMBER: ");
INT NUM1 = SCANNER.NEXTINT();
SYSTEM.OUT.PRINT("ENTER SECOND NUMBER: ");
INT NUM2 = SCANNER.NEXTINT();
INT SUM = NUM1 + NUM2;
SYSTEM.OUT.PRINTLN("SUM: " + SUM);
SCANNER.CLOSE();
}
}
IMPORT JAVA.UTIL.SCANNER ;
PUBLIC CLASS NUMBERCHECK {
PUBLIC STATIC VOID MAIN(STRING[] ARGS) {
SCANNER SCANNER = NEW SCANNER(SYSTEM.IN);
SYSTEM.OUT.PRINT("ENTER A NUMBER: ");
INT NUM = SCANNER.NEXTINT();
IF (NUM % 2 == 0) {
SYSTEM.OUT.PRINTLN(NUM + " IS EVEN.");
} ELSE {
SYSTEM.OUT.PRINTLN(NUM + " IS ODD.");
IF (NUM > 0) {
SYSTEM.OUT.PRINTLN(NUM + " IS POSITIVE.");
} ELSE IF (NUM < 0) {
SYSTEM.OUT.PRINTLN(NUM + " IS NEGATIVE.");
} ELSE {
SYSTEM.OUT.PRINTLN(NUM + " IS NEITHER POSITIVE NOR NEGATIVE.");
SCANNER .CLOSE();
IMPORT JAVA .UTIL .S CANNER ;
PUBLIC CLASS GRADESYSTEM {
PUBLIC STATIC VOID MAIN (S TRING [] ARGS ) {
S CANNER SCANNER = NEW SCANNER(SYSTEM.IN);
S YSTEM.OUT.PRINT("ENTER MARKS OBTAINED (OUT OF 100): ");
INT MARKS = SCANNER.NEXTINT();
CHAR GRADE = CALCULATE GRADE(MARKS);
S YSTEM.OUT.PRINTLN("GRADE: " + GRADE);
SCANNER .CLOSE ();
PUBLIC STATIC CHAR CALCULATE GRADE ( INT MARKS ) {
CHAR GRADE ;
IF (MARKS >= 90) {
GRADE = 'A';
} ELSE IF (MARKS >= 80) {
GRADE = 'B';
} ELSE IF (MARKS >= 70) {
GRADE = 'C';
} ELSE IF (MARKS >= 60) {
GRADE = 'D';
} ELSE IF (MARKS >= 50) {
GRADE = 'E';
} ELSE {
GRADE = 'F';
RETURN GRADE ;
IMPORT JAVA .UTIL .S CANNER ;
PUBLIC CLASS CALCULATOR {
PUBLIC STATIC VOID MAIN (S TRING [] ARGS ) {
S CANNER SCANNER = NEW SCANNER(SYSTEM.IN);
S YSTEM.OUT.PRINT("ENTER FIRST NUMBER: ");
DOUBLE NUM 1 = SCANNER.NEXTDOUBLE();
S YSTEM.OUT.PRINT("ENTER SECOND NUMBER: ");
DOUBLE NUM 2 = SCANNER.NEXTDOUBLE();
S YSTEM.OUT.PRINT("ENTER OPERATOR (+, -, *, /): ");
CHAR OPERATOR = SCANNER.NEXT().CHARAT(0);
DOUBLE RESULT ;
SWITCH (OPERATOR ) {
CASE '+':
RESULT = NUM1 + NUM2;
BREAK ;
CASE '-':
RESULT = NUM1 - NUM2;
BREAK ;
CASE '*':
RESULT = NUM1 * NUM2;
BREAK ;
CASE '/':
IF (NUM 2 != 0) {
RESULT = NUM1 / NUM2;
} ELSE {
S YSTEM.OUT.PRINTLN("ERROR: DIVISION BY ZERO!");
RETURN ;
BREAK ;
DEFAULT :
S YSTEM.OUT.PRINTLN("ERROR: INVALID OPERATOR !");
RETURN ;
S YSTEM.OUT.PRINTLN("RESULT: " + RESULT);
SCANNER .CLOSE ();
FOR LOOP PROGRAM - PRINT NUMBERS FROM 1 TO 10:
PUBLIC CLASS F ORLOOP EXAMPLE {
PUBLIC STATIC VOID MAIN (S TRING [] ARGS ) {
FOR (INT I = 1; I <= 10; I++) {
S YSTEM.OUT.PRINT(I + " ");
WHILE LOOP PROGRAM - PRINT NUMBERS FROM 1 TO 100:
PUBLIC CLASS WHILELOOPEXAMPLE {
PUBLIC STATIC VOID MAIN (S TRING [] ARGS ) {
INT I = 1;
WHILE (I <= 100) {
S YSTEM.OUT.PRINT(I + " ");
I++;
FOR LOOP PROGRAM - PRINT MULTIPLICATION TABLE:
IMPORT JAVA .UTIL .S CANNER ;
PUBLIC CLASS MULTIPLICATION TABLE {
PUBLIC STATIC VOID MAIN (S TRING [] ARGS ) {
S CANNER SCANNER = NEW SCANNER(SYSTEM.IN);
S YSTEM.OUT.PRINT("ENTER A NUMBER: ");
INT NUM = SCANNER.NEXTINT();
S YSTEM.OUT.PRINTLN("MULTIPLICATION TABLE OF " + NUM + ":");
FOR (INT I = 1; I <= 10; I++) {
S YSTEM.OUT.PRINTLN(NUM + " X " + I + " = " + ( NUM * I));
SCANNER .CLOSE ();
IMPORT JAVA.UTIL.SCANNER;
PUBLIC CLASS MULTIPLICATIONTABLE {
PUBLIC STATIC VOID MAIN(STRING[] ARGS) {
SCANNER SCANNER = NEW SCANNER(SYSTEM.IN);
SYSTEM.OUT.PRINT("ENTER A NUMBER: ");
INT NUM = SCANNER.NEXTINT();
SYSTEM.OUT.PRINTLN("MULTIPLICATION TABLE OF " + NUM
+ ":");
INT I = 1;
WHILE (I <= 10) {
SYSTEM.OUT.PRINTLN(NUM + " X " + I + " = " + (NUM *
I));
I++;
SCANNER.CLOSE();
}
}
IMPORT JAVA.UTIL.SCANNER;
PUBLIC CLASS RANGEPRINTER {
PUBLIC STATIC VOID MAIN(STRING[] ARGS) {
SCANNER SCANNER = NEW SCANNER(SYSTEM.IN);
S YSTEM.OUT.PRINT("ENTER THE STARTING
NUMBER: ");
INT START = SCANNER.NEXTINT();
SYSTEM.OUT.PRINT("ENTER THE ENDING NUMBER: ");
INT END = SCANNER.NEXTINT();
SYSTEM.OUT.PRINTLN("NUMBERS WITHIN THE RANGE
" + START + " TO " + END + ":");
FOR (INT I = START; I <= END; I++) {
SYSTEM.OUT.PRINT(I + " ");
}
SCANNER.CLOSE();
}
}
IMPORT JAVA .UTIL .S CANNER ;
PUBLIC CLASS DAYSMONTHSSEASONS {
PUBLIC STATIC VOID MAIN (S TRING [] ARGS ) {
S CANNER SCANNER = NEW SCANNER(SYSTEM.IN);
S YSTEM.OUT.PRINT("ENTER A MONTH NUMBER (1-12): ");
INT MONTH = SCANNER.NEXTINT();
S TRING SEASON;
INT DAYS ;
SWITCH (MONTH ) {
CASE 1: CASE 3: CASE 5: CASE 7: CASE 8: CASE 10: CASE 12:
SEASON = "W INTER";
DAYS = 31;
BREAK ;
CASE 4: CASE 6: CASE 9: CASE 11:
SEASON = "SUMMER ";
DAYS = 30;
BREAK ;
CASE 2:
SEASON = "SPRING";
DAYS = 28; // A SSUMING NON-LEAP YEAR FOR SIMPLICITY
BREAK ;
DEFAULT :
S YSTEM.OUT.PRINTLN("INVALID MONTH NUMBER!");
SCANNER .CLOSE ();
RETURN ;
S YSTEM.OUT.PRINTLN("NUMBER OF DAYS IN MONTH " + MONTH + ": " + DAYS);
S YSTEM.OUT.PRINTLN("SEASON: " + SEASON);
SCANNER .CLOSE ();
}
IMPORT JAVA .UTIL .S CANNER ;
PUBLIC CLASS DAYOFWEEK {
PUBLIC STATIC VOID MAIN (S TRING [] ARGS ) {
S CANNER SCANNER = NEW SCANNER(SYSTEM.IN);
S YSTEM.OUT.PRINT("ENTER A NUMBER CHECK DAY OF THE WEEK (1-7): ");
INT DAY NUMBER = SCANNER.NEXTINT();
S TRING DAY;
SWITCH (DAY NUMBER ) {
CASE 1:
DAY = "M ONDAY";
BREAK ;
CASE 2:
DAY = "TUESDAY";
BREAK ;
CASE 3:
DAY = "W EDNESDAY";
BREAK ;
CASE 4:
DAY = "THURSDAY";
BREAK ;
CASE 5:
DAY = "FRIDAY";
BREAK ;
CASE 6:
DAY = "SATURDAY";
BREAK ;
CASE 7:
DAY = "SUNDAY";
BREAK ;
DEFAULT :
DAY = "INVALID DAY NUMBER";
S YSTEM.OUT.PRINTLN("ACCORDING YOUR NUMBER " + DAYNUMBER + " IS: " + DAY);
SCANNER .CLOSE ();
IMPORT JAVA .UTIL .S CANNER ;
PUBLIC CLASS MONTHOFYEAR {
PUBLIC STATIC VOID MAIN (S TRING [] ARGS ) {
S CANNER SCANNER = NEW SCANNER(SYSTEM.IN);
S YSTEM.OUT.PRINT("ENTER A NUMBER THE MONTH OF THE YEAR (1-12): ");
INT MONTH NUMBER = SCANNER.NEXTINT();
S TRING GREGORIANMONTH;
S TRING ISLAMICMONTH;
SWITCH (MONTH NUMBER ) {
CASE 1:
GREGORIAN MONTH = "JANUARY";
ISLAMIC MONTH = "M UHARRAM";
BREAK ;
CASE 2:
GREGORIAN MONTH = "FEBRUARY";
ISLAMIC MONTH = "SAFAR";
BREAK ;
CASE 3:
GREGORIAN MONTH = "M ARCH";
ISLAMIC MONTH = "RABI' AL-AWWAL";
BREAK ;
CASE 4:
GREGORIAN MONTH = "A PRIL";
ISLAMIC MONTH = "RABI' AL-THANI";
BREAK ;
CASE 5:
GREGORIAN MONTH = "M AY";
ISLAMIC MONTH = "JUMADA AL-AWWAL";
BREAK ;
CASE 6:
GREGORIAN MONTH = "JUNE";
ISLAMIC MONTH = "JUMADA AL-THANI";
BREAK ;
CASE 7:
GREGORIAN MONTH = "JULY";
ISLAMIC MONTH = "RAJAB";
BREAK ;
CASE 8:
GREGORIAN MONTH = "A UGUST";
ISLAMIC MONTH = "SHA'BAN";
BREAK ;
CASE 9:
GREGORIAN MONTH = "SEPTEMBER";
ISLAMIC MONTH = "RAMADAN";
BREAK ;
CASE 10:
GREGORIAN MONTH = "OCTOBER";
ISLAMIC MONTH = "SHAWWAL";
BREAK ;
CASE 11:
GREGORIAN MONTH = "NOVEMBER";
ISLAMIC MONTH = "DHU AL-QI'DAH";
BREAK ;
CASE 12:
GREGORIAN MONTH = "DECEMBER";
ISLAMIC MONTH = "DHU AL-HIJJAH";
BREAK ;
DEFAULT :
GREGORIAN MONTH = "INVALID MONTH NUMBER";
ISLAMIC MONTH = "INVALID MONTH NUMBER";
S YSTEM.OUT.PRINTLN("GREGORIAN MONTH TO THE NUMBER " + MONTHNUMBER + " IS: " +
GREGORIAN MONTH);
S YSTEM.OUT.PRINTLN("ISLAMIC MONTH TO THE NUMBER " + MONTHNUMBER + " IS: " +
ISLAMIC MONTH);
SCANNER .CLOSE ();
}
}