University of Khartoum
Electrical and Electronic Engineering
Java Lab – 3rd Year – Lab 1 (Java Basics)
Electrical and Electronic Engineering
Java Lab – 3rd Year – Lab 1 (Java Basics)
Introduction:
Java is a
pure object-oriented programming language. It is designed to be written and
compiled once, and run everywhere. This is possible because a Java program is
not compiled directly to the machine code; instead it is compiled to bytecode
that can run on every Java virtual machine (JVM). The JVM then takes the
responsibility of translating bytecode to the machine code and
handling other low-level details.
Java
programs are organized into classes. They are written and saved in .java
files and compiled into .class files. These classes are grouped into packages.
Packages in Java acts similarly to namespaces in C++.
·
To compile java file from cmd: javac file_name.java
·
To execute the compiled file from cmd: java file_name
Data
types:
Variables
types in Java are either primitive or reference types. The primitive types are:
byte, short, int, long, float, double, char, and boolean. Reference types
include arrays and classes.
A
floating-point literal is of type float if it ends with the
letter F or f; otherwise its type is double and it can
optionally end with the letter D or d.
Beside the
primitive data there are: Integer class, Float class and Double Class which
wraps a value of the primitive type int, float and double respectively in an
object, these classes provides several methods for converting the primitive
data to a String and a String to primitive data. Example :
int num =
Integer.parseInt(“3”); //
num will be assigned value of 3 as integer.
float fnum
= Float.parseFloat(“3”); // fnum will be assigned value of
3.0 as float.
Arrays:
An array is
a container object that holds a fixed number of values of a single
type. The length of an array is established when the array is created. After
creation, its length is fixed.
Array
declaration and creation:
type[]
array_name ; // type is the
data type of the contained elements i.e. int, String
array_name =
type[size] // or in one line:
type[] array_name = type[size].
Each array has a built-in length property
(arrayname.length) to determine the size of the array by using.
Operators:
Java
operators are almost the same as in C++: (arithmetic, bitwise, relational,
logical, … )
Control
statements:
In addition
to the well-known statements (if-else, switch, for, while, and do-while), Java
presents the for-each statement (Enhanced for loop “used mainly for unknown
array size”) throughout its execution, it iterates through the items of an
iterable object (e.g. an array).
String[]
jobs = {“Accountant”, “Manager”, “Engineer”}; //
Define array of type String hold 3 //data
for(String job: jobs) { // In first iterate job will have
“Accountant” value, in second //iterate “Manager” value, …
// do
something
}
Java
standard I/O
STD output
syntax:
System.out.println();
STD input syntax:
import java.util.Scanner Scanner sc = new Scanner(System.in);
int i = sc.nextInt(); // to
get the next integer
String str =
sc.next(); // to get the next
string
Method
Overloading
Feature that
allows a class to have two or more methods having same name, if their argument
lists are different. Argument lists could differ in:
1. Number of
parameters.
2. Data type
of parameters.
3. Sequence
of Data type of parameters.
Method
overloading is also known as Static Polymorphism.
String API
Strings are
a sequence of characters.
In java Strings are treated as objects from String class.
The String class is
immutable, so that once it is created a String object
cannot be changed.
Important
String class constructors:
String str1 = new String(); //Initializes a newly created
String object so that it represents //an empty character sequence.
String str2 = new String(char[]
value) //Allocates a new String so that it
represents the sequence of //characters currently contained in the character
array //argument.
String str3 = new String(String
original); //Initializes a newly
created String object so that it represents //the same sequence of characters
as the argument; in other //words, the newly created string is a copy of the
argument //string.
String str4 = “java lab”;
Important
String class methods:
int length = str1.length(); // Returns the length of this –str1- string.
int length = str1.length(); // Returns the length of this –str1- string.
char ch =
str2.charAt(int index); // Returns
the char value at the specified index.
int result =
str3.compareTo(String anotherString); //Compares
two strings lexicographically.
int indx = str4.indexOf(char ch); // Returns the index within this string
of the first //occurrence of the specified character.
String str5 = str4.toLowerCase(); //Converts all of the characters in this
String to lower case.
String str6 = str5.concat(str4); // Concatenates the specified string to the end of this
string (str5), or simply; String str6 = str5 + str4;
Program
structure:
The file
name should match the main class name. Also, only one class can be public per
file.
package
package_name;
import
imported_package_name;
// …
public Class
MainClass {
Public static void main(String[] args) {
// Program starts execution from here
}
// Other
methods definitions
}
// Other classes and interfaces definitions
TASK 1:
Purpose of
this task is to:
1. Use Java
Standard I/O.
2. Use control
statement.
Ø Create class
called CheckVowelsDigits, which
prompts the user for a string, then counts the number of vowels (a, e, i, o, u,
A, E, I, O, U) and digits (0-9) contained in the string, and prints the counts
and the percentages (with 2 decimal digits). For example,
Enter a String: testing12345
Number of vowels: 2 (16.67%)
Number of digits: 5 (41.67%)
TASK 2:
Purpose of
this task is to:
1. Use method
overloading.
Ø Create class called Summation that overload the main
method in order to sum two integer numbers, two float numbers and two double
numbers.
TASK 3:
Purposes of
this task to:
1. Understand main
function.
2. Use control
statement.
Create a class
called ResistorColorCode inside a package called electronics, the user
must specify the colors of the resistor at the execution time and the class
should calculate the equivalent resistance value according to the table shown
below.
Helpful
method:
Math.pow(double
a, double b) : Returns the value of the first argument raised to the power of
the second argument.
TASK 4:
Purpose of
this task is to:
1. Work with String
API
Ø Create a class called SeventyThree which do the
following:
i)
Convert the
number 73 into its binary representation and construct a new String object to
hold that binary representation then print it.
ii) Construct a new String object of the first four bits of the
binary representation then print it.
iii) Construct a new String object of the last four bits of the
binary representation then print it.
iv) Compare the String object in (ii) and (iii) and print the
compression result.
v) Reverse the binary representation in (i) and display
it.
Helpful
method:
String
str = Integer.toBinaryString(73); //
Returns a string representation of the integer //argument as an unsigned
integer in base 2.

No comments:
Post a Comment