DEV Community

Abdelrhman khaled
Abdelrhman khaled

Posted on • Edited on

Java HackerRank AUFS Season 6

What we toke in session 2

1- how to print a sentence
System.out.print("hello in HackerRank");
or
System.out.println("hello in HackerRank with");

special characters needed to print

  • \\ will print \
  • \' will print '
  • \" will print "
  • \n will make a new line
  • \t will make a horizontal tab

let's print that sentence

hello\Abdelrhman\2002\new Year

System.out.println("hello\\Abdelrhman\\2002\\new year");
Enter fullscreen mode Exit fullscreen mode

take care if you want to print \ or another special characters, to print it in the form\\.

2- naming identifiers
an identifier (a variable name) can be a sequence of Unicode characters that represent letters, digits 0-9, a dollar sign ($), or an underscore (_).

some limitations

  • The first symbol of an identifier cannot be a digit.
  • An identifier cannot have the same spelling as a keyword.
  • It cannot be spelled as the boolean literal true or false, and or as the literal null.
  • an identifier cannot be just an underscore (_).

Here are a few unusual but legal examples of identifiers:
$$
_30
String
ατηρε

3- Data Types
here is some data types we have in java

  • Boolean
    to store true or false

  • short

  • int

  • long
    to store Integer numbers

  • float

  • double
    to store Decimal numbers like 3.31

  • char
    to store a single character

  • Stirng
    to store a text

that's good but what is the difference between short, int, long ?
Is the maximum and minimum value that can hold .

So let's see what is the maximum and minimum value that short dataType can hold

System.out.println(Short.MAX_VALUE);
System.out.println(Short.MIN_VALUE);
Enter fullscreen mode Exit fullscreen mode

that will print
32767
-32768

so if you try to store a value like 999999 in a short variable it will give an error.

try to know maximum and minimum value that int and long can hold

System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);

System.out.println(Long.MAX_VALUE);
System.out.println(Long.MIN_VALUE);
Enter fullscreen mode Exit fullscreen mode

NOW let's assign a value to a variable

int age = 21;
String name = "Abdelrhman Khaled";
System.out.println("My name is: "+name +" And my age is: "+age);
Enter fullscreen mode Exit fullscreen mode

4- some usefull functions in class Integer

Let's try to convert numbers from int to Binary and Octal...

System.out.println(Integer.toBinaryString(42));
System.out.println(Integer.toOctalString(42));
System.out.println(Integer.toHexString(42));
Enter fullscreen mode Exit fullscreen mode

That is very great the representation of number 42
in Binary is 101010
and in Octal 2a
and in HexaDecimal 52

5- Some Logic operations in java

  • to represent AND operation we use that symbol &&
  • to represent OR operation we use that symbol ||
  • to know the equality we use ==
  • and to assign a value to variable we use =

6- Ternary operation

The ? : operator is called a ternary operator. It evaluates a condition (before the sign ?) and, if it results in true, assigns to a variable the value calculated by the first expression (between the ? and : signs); otherwise, it assigns the value calculated by the second expression (after the : sign)

int n = 1, m = 2;
float k = n > m ? (n * m + 3) : ((float)n / m);
System.out.println(k);            //prints: 0.5
Enter fullscreen mode Exit fullscreen mode

7- we spoke about typeCasting

if you try to divide integer number on interger number the outPut must be integer.

System.out.println(5/2); //that will print 2

And that is a problem the fraction disappeared !
So if you want to save the fraction you must divide Double or Float on that number

System.out.println(5.0/2); // Wow that's awesome the outPut is 2.5

that we done is called type case, and there is to ways to case from int to double

  1. multiply the number with 1.0
  2. or and (double)before the number

8- finally we spoke about if statement

and the style of write it is

if(condition){
            //do that orders
}
Enter fullscreen mode Exit fullscreen mode

OR

if(condition){
            //if the condition is true do that 
}else{
            // it the condition is false do that 
}
Enter fullscreen mode Exit fullscreen mode

OR

if(condition1){
           //if condition1 is true do that 
}else if(condition2){
           //if condition1 is false and condition2 is true do that 
}else {
           // if there isn't any condition is true do that
}
Enter fullscreen mode Exit fullscreen mode

9- At the end of that session we spoke about loops and forLoop

if you want to repeat some thing many times

for(inti , condition , increment){
      //the thing we want to be repeated
}
Enter fullscreen mode Exit fullscreen mode

example

for (int i = 0; i < 10; i++) {
            System.out.println(i);
}
Enter fullscreen mode Exit fullscreen mode

find the summation of numbers from 1 to n

Scanner sc = new Scanner(System.in);
int number = sc.nextInt(); 
int sum = 0 ;
for(int i = 1; i <= number ; i++){
      sum +=i;
}
System.out.println("The summation of numbers from 1 to "+n+" is equals "+sum);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)