0% found this document useful (0 votes)
12 views7 pages

Exp 1

Uploaded by

Noor Anad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views7 pages

Exp 1

Uploaded by

Noor Anad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Experiment 1

Java Basics
Introduction
Java is a high-level, object-oriented, general-purpose programming language that was originally developed by
James Gosling, a Canadian computer scientist, at what was then Sun Microsystems, in the U.S. state of
California in 1991. Sun Microsystems was later acquired by Oracle Corporation, also in California, in 2010.

Two kinds of programs translate high-level languages into low-level languages:


A compiler reads the entire program and translates it completely before the program starts running. In this
context, the high-level program is called the source code, and the translated program is called the object code
or the executable.
An interpreter processes the program a little at a time, alternately reading lines and performing computations.

Java takes a hybrid approach to offer the benefits of compilation and interpretation. Its source code is not
compiled directly to machine code like in C and C++. Instead, a compiler generates what is known as bytecode
and stores that in one or more files with a “.class” extension. Bytecode is not fixed to a specific type of
processor’s instruction set. However, it comes close to the low level of machine code without making significant
assumptions about the kind of processor that will run it.

Once the bytecode for a Java program is generated, you can then actually run it using an interpreter that can
translate the bytecode to the machine language of the particular processor of the target computer. Recall that
interpreted languages tend to be much slower than compiled languages because they typically translate from
high-level code down to low-level. However, with Java, since bytecode is already at a low-level, the translation
costs are not as significant.

You’ll often find the Java interpreter being referred to as the Java Virtual Machine (or just JVM). The “Virtual
Machine” part of the name is derived from the fact that compiled Java code is not executed by a real processor.
Rather a piece of software, the interpreter, performs the execution.
By incorporating both compilers and interpreters, Java can provide the platform independence of interpreted
languages while minimizing their inefficiencies.

1
Each Java release is distributed as two different packages:
The Java Runtime Environment (JRE) is for running Java programs and is intended for end users.
The JRE consists of the JVM and runtime libraries. You can use the JRE when you don’t need to compile the
Java program.
The Java Development Kit (JDK) is for software developers to compile, debug, and document Java
programs. You will need to use the JDK here, as you will need to compile your Java programs.

Example 1: Write a Program to print the text “Welcome to World of Java”. Save it with name
Welcome.java in your folder.

Class Welcome
{
public static void main (String args[])
{
System.out.println (“welcome to world of Java”);
} }

Note:
main() in Java is the entry point for any Java program. It is always written as public static void main(String
args[]).
 public: Public is an access modifier, which is used to specify who can access this method. Public means
that this Method will be accessible by any Class.
 static: It is a keyword in Java which identifies it as class-based. main() is made static in Java so that it
can be accessed without creating the instance of a Class. In case main is not made static then the compiler
will throw an error as main() is called by the JVM before any objects are made and only static methods
can be directly invoked via the class.
 void: It is the return type of the method. Void defines the method which will not return any value.
 main: It is the name of the method which is searched by JVM as a starting point for an application with
a particular signature only. It is the method where the main execution occurs.
 String args[]: It is the parameter passed to the main method.

Q. Why Java is platform independent?


Java is called platform independent because of its byte codes which can run on any system irrespective of its
underlying operating system.

Example 2: Write a Program to print the area of triangle. Save it with name Area.java in your folder.
class Area
{
public static void main(String args[])
{
int height =10, base=6;
float area=0.5F*base* height;
System.out.println(“area of triangle = ”+area); } }

2
Java supports eight primitive types of variables: byte, short, int, long, float, double, char, and boolean.

 Variable names in Java are case-sensitive


 They must start with a letter, an underscore, or a dollar sign ($).
 They cannot start with a number.
 After the first character, a variable name can include any combination of letters and numbers.
 Spaces and special symbols, such as !, "", £, %, &, *, #, @, ~, and so on, are not allowed in variable
names.
 In Java, all variables must be declared before they can be used.

Example 3:
double minute = 59.0;
System.out.print("Fraction of the hour that has passed: ");
System.out.println(minute / 60.0);

The output is:


Fraction of the hour that has passed: 0.9833333333333333

double y = 1 / 3; // common mistake


You might expect the variable y to get the value 0.333333, which is a legal floating point value. But instead it
gets the value 0.0

Example 4:
double a = 5.0/2.0; // a = 2.5 int b = 4/2; // b = 2
int c = 5/2; // c = 2
double d = 5/2; // d = 2.0

3
When you output a double using print or println, it displays up to 16 decimal places:
System.out.print(4.0 / 3.0);
The result is:
1.3333333333333333

System.out provides another method, called printf, that gives you more control of the format. The “f ” in printf
stands for “formatted”.

Change to: System.out.printf("Four thirds = %.3f", 4.0 / 3.0);

Example 5:
int inch = 100;
double cm = inch * CM_PER_INCH;
System.out.printf("%d in = %f cm\n", inch, cm);

Example 6:
class GravityCalculator {
public static void main(String[] args) {
double gravity = -9.81;
double initialVelocity = 0.0;
double fallingTime = 10.0;
double initialPosition = 0.0;
double finalPosition = .5 * gravity * fallingTime * fallingTime;
finalPosition = finalPosition + initialVelocity * fallingTime;
finalPosition = finalPosition + initialPosition;
System.out.println("An object's position after " + fallingTime + " seconds is " + finalPosition + “ m."); } }

4
Java Control Statements

Example 7: Write a java Program to check the number is Prime or not.


Import java.util.Scanner;
class Prime
{
public static void main(String arr[])
{
int c;
Scanner in=new Scanner(System.in);
System.out.println("Enter the number to be tested for prime ");
int n=in.nextInt();
for ( c = 2 ; c <= n - 1 ; c++ )
{
if ( n%c == 0 )
{
System.out.println(n+">>>>> not prime");
break;
}
}
if ( c == n )
System.out.println(n+ ">>>>Number is prime."); } }

5
Example 8: Write a java Program to generate a Ladder of numbers.
import java.util.Scanner;
class Ladder
{
public static void main(String arr[])
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the number of rows");
int a=in.nextInt();
for(int i=1;i<=a;i++)
{
for(int j=1;j<=i;j++)
System.out.print(j);
for(int k=i-1;k>=1;k--)
System.out.print(k);
System.out.print("\n");
}}}

Input and Output


Input and output are important in almost all applications. In Java, System.out is used to print messages on the
computer screen; this is called standard output. System.in and System.console are used to read text from the
keyboard; this is called standard input.

public class InputOutputExample {


public static void main(String args[]) {
System.out.print("Enter something:");
String x = System.console().readLine();
System.out.println("You wrote: "+ x );
}
}

There are other forms of Java input and output. In this code, System.in is used to read from the keyboard.
Combined with the Scanner class, it can read one line at a time from the keyboard; it can also read an integer
value from the keyboard.

Example 9:
import java.util.Scanner;
class InputOutputExample2 {
public static void main(String[] args) {
System.out.println("Enter your name: ");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine(); // remember that Java is case-sensitive
System.out.println("Your name is " + name);
System.out.println("Enter your age: ");
int age = scanner.nextInt();
System.out.println("Your age is " + age);
}}

6
Assignment

Q1\Write a Java program that read student name and four marks form the keyboard. Then, it determines a
student’s final grade and indicate whether it is a passing or failing grade. The final grade is calculated as the
average of the four marks.
Q2\ Write a Java program to obtain the factorial of 20.
Q3\ Draw a flowchart to sum the even numbers between 0 and 59.
Q4\ Draw a flowchart to accepts N numbers and get the summation of negative numbers, the summation of
positive numbers, then count the numbers in each group.
Q5\ write a Java program for the following flowchart:

You might also like