Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Saturday, 15 September 2018

Getting Input From User




In this post, we are going to learn how to get input from the User. Then will do some programming exercises to enhance our programming knowledge.

There are Different - Different ways to get an input from the console -- Go HERE.
 but right now will stick to Scanner Class, because it is the most common and easiest way.

using Scanner Class will get the input from the user,

   
- The Scanner is a  class in java.
- present in java.util package
- which allows the user to read values of various types.
- There are far more methods in class Scanner than you will need in this course.

Syntax:- 


Scanner - is a class in java.

sc - is an object of Scanner class (you can give any name).

new - is an operator which is used to make an object of a class.

Scanner() - is a constructor in Scanner Class(Don't Worry will talk it later).

System - is a class in java, represents your computer system (helps to provide us system-level methods)

in - stands for input (s).

System.in - getting input from the system and it passes as an argument to Scanner Constructor.(Don't Worry, if you are unable to get it). 



# Scanner Class has Somes Methods to get Different kinds of values from the console.

- For different types of values different, types of methods are available.
- For int type nextInt() method is used,
- For double type nextDouble() method used,
- All the methods called by using Scanner Object (sc.method name).
- for example (String value) -- we have to use next() or nextLine().


String s; // variable -- where data store
Scanner sc = new Scanner(System,in); // creating Scanner class object 
s = sc.next(); // getting input from user and assgin it to "s" variable  
sc.close(); // good practic to close resourses.







Question -- WAP to Take two number from the user and return its sum.
Answer -- 



# Code 

package scannerClass;

import java.util.Scanner;

public class Sum {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
System.out.println("Enter 1st number : ");
int num1 =sc.nextInt();
System.out.println("Enter 2nd number : ");
int num2 = sc.nextInt();
int add=num1+num2;
System.out.println("Sum = "+add);
}
}








Monday, 3 September 2018

Introduction of Java (Part -1)




This tutorial is fully based on Core Java (J2SE), I will try to teach you each and everything in details
(With proper examples and definition as well as programs). 

Note- Don't panic, if you are unable to understand something or many things, just remember the names (Terms), I will explain everything later. …


Question - Why we should study Java?

Answer - Because
  • It is a general-purpose language.
  • It is Open Source and Most Popular.
  • Its syntax is similar to C or C++, therefore it is easy to learn, use and maintain.
  • Java is widely used by the IT companies.
  • You can able to create Desktop Application as well as the Web Applications.
  • You need Java to develop Andriod Applications.
  • Need to learn Java for Testing purpose.
  • Developing Java Application is very Simple because 80% work done by the API Developers as a Programmer you have to do only 20% of work (Business logics).    


Question - What Is JAVA?

Answer - Java is
  • Programming Language,
  • Object Oriented Programming Language,
  • Simple, Secure and Robust,
  • Platform Independent,
  • etc...few more points understand later.


Question - Where Java Is Used?

Answer - According to sun-micro systems, 3.5 billion devices run on java.
  • Desktop Application,
  • Web Application,
  • Mobile Application,
  • Robotics (Embedded Systems),
  • Games Etc.


Question - What are Various Java editions?

Answer - Java has 3 editions
  • J2SE
    • stands for Java Standard Edition.
    • Desktop Application can be developed using J2SE.
  • J2EE
    • stands for Java Enterprise Edition.
    • Web Sites can be Developed using J2EE. 
  • J2ME
    • stands for Java Mobile Edition.
    • Mobile and Embedded things can be done using J2ME.  






Introduction to Java (Part -3 )




*Question - How can you say "Java is Platform Independent"?

Answer - 

Before Understanding this Question, Let's understand "What is Platform? "
The platform is the combination of the processor of a system and the Operating system that is installed in your system.

[ Platform = System Processor  +  Operating System ]

Now,
decide Java is platform Independent or not?




Yes, Java is platform Independent because, in the image, there is only one JAVA APPLICATION and that application can able to execute (run) on three different machines (Windows, MacOS and Linux).
So we can say that Java is a PLATFORM INDEPENDENT Language
BUT JVM IS PLATFORM DEPENDENT.


Question - Prove that "JAVA is Platform Independent"?

Answer -



  1. Write Java program and save it as abc.java 
  2. Compile it and get equivalent byte code or class file (abc.class)
  3. Now the code is ready to execute...
  4. In the client machine (System in which code is going to run), JVM convert your Byte Code into Machine Code 
  5. Now that Machine Code is dependent and it is going to run in your system.
According to the question, write a java code(program) and compile it and get the equivalent Byte Code.Now you can able to use that bytecode in any Machine (System).

[Note- JVM should be installed at that Machine(System)]


Question - What is BYTE CODE?
Answer - BYTE CODE is the equivalent code of your JAVA Program(code).


Question - Why it is called BYTE CODE?
Answer - Because the length of each instruction is of 8 bit.