0% found this document useful (0 votes)
23 views

ASSIGNMENT (Java)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

ASSIGNMENT (Java)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

ASSIGNMENT(JAVA)

1. Java versions :
• Java released different versions , with new features,improvements and less errors.
• It includes JDK 1.0 TO JDK 23(Latest version).

2. High Level Language Vs Low Level Language:

• High Level Language: This type of language is closer to human language because
they are easy to read , understand and write. Machine or Computer can’t understand
this language only humans can so, it is called High Level Programming Langauge.
Examples: python, c, c++ , java etc…

• Low Level Language: This type of Language is closer to machines or computers


because they are in the form of 0s and 1s (Binary). Humans can’t understand this
language so, it is called Low Level Programming Language.
Examples: Assesmbley or Machine Code.

3. Data Types:
1. Primitive Data types:
TYPE SIZE EXAMPLE
byte 8-bit byte a = 10;
short 16-bit Short s = 1000;
int 32-bit int b = 50;
long 64-bit long num = 150000000L;
float 32-bit float decnum = 5.4f;
double 64-bit double dec = 19.99;
char 16-bit char c = ‘B’;
boolean 1-bit boolean isJavaFun = true;

2. Non-Primitive Data Types:

TYPE USE EXAMPLE

String Used for text String greet = “Hello”;

Arrays Storing a collection of data. Int[] n ={1,2,3};


class Represents real world entity Class car {
}
interface Defines something that other interface Vehicle{
classes can implement. }
enums Represents constants like days of enum
week or directions etc… days{Monday,….}

4. Variables:
• Variables are like a containers which stores some value according to the specified
datatype.
• We assign value to the variable by using assignment operator “=”.
• One variable can only store one value.
• Example : int n = 10;
• Here, int is datatype , n is variable and 10 is value.

5. Naming Conventions:

Naming conventions are a set of rules for naming variables, methods, classes, etc., to
make code more readable. For example:

• CamelCase for variables and methods.


• Example: myVariable , calculateSum.
• PascalCase for class names .
• Example: MyClass, Student Record.

6. Operators
• Operators are symbols that perform operations on variables and values.
1. Arthematic operators:
OPERATOR NAME EXAMPLE

+ ADDITION A+B

- SUBTRACTION A-B

* MULTIPLICATION A*B

/ DIVISION A/B

% MODULUS A%B

++ INCREMENT A++

-- DECREMENT A--
2.Assignment Operators:
OPERATOR EXAMPLE
= A=3
+= A=A+3
-= B=B-4
*= X=X*3
/= Y=Y/3
%= X=X%3
&= Y=Y&6
|= Z=Z|5
^= X=X^2
>>= K = K>>4
<<= P = P<<2

3.Comparision Operators
OPERATOR NAME EXAMPLE

== EQUAL TO A= = B

!= NOT EQUAL A!=B

> GREATER THAN A>B

< LESS THAN A<B

>= GREATER THAN OR EQUAL TO A>=B

<= LESS THAN OR EQUAL TO A<=B

4.Logical operators:
OPERATOR NAME USE EXAMPLE

&& LOGICAL RETURNS TRUE IF BOTH A<3&&A<5


AND STATEMENTS ARE TRUE.
|| LOGICAL RETURNS TRUE IF ONE A<3| | a>4
OR STATEMENT IS TRUE.
! LOGICAL REVERSE OF RESULT !(A<3&&A>4)
NOT
7.Conditional Statements:
1. if condition:
if (condition) {
// Code to execute if the condition is true

}
package Condition;
import java.util.Scanner;
public class Evenodd {
public static void main(String[] args) {
System.out.println("Enter a number:");
Scanner sc=new Scanner(System.in);
int a = sc.nextInt();
if(a<5) {
System.out.println(a);
}
}
}
2.if else condition:
if (condition) {
// Code to execute if the condition is true
} else
{ // Code to execute if the condition is false
}
package Condition;
import java.util.Scanner;
public class Evenodd {
public static void main(String[] args) {
System.out.println("Enter a number:");
Scanner sc=new Scanner(System.in);
int a = sc.nextInt();
if(a%2==0) {
System.out.println("Even number");
}
else {
System.out.println("Odd number");
}
}
}

3. else if ladder:
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else if (condition3) {
// Code to execute if condition3 is true
} else {
// Code to execute if none of the above conditions are true
}
Example:
package Condition;

import java.util.Scanner;

public class Grade {


public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int marks = sc.nextInt();
if(marks>=80) {
System.out.println("Grade A");
}
else if(marks>=70 && marks<80)
{
System.out.println("Grade B");
}
else if(marks>=50 && marks<80)
{
System.out.println("Grade c");
}
else {
System.out.println("Fail");
}
}
}

8.Continue Vs Break:
• Continue: Skips the current iteration of a loop and moves to the next one.
• Break: Exits the loop entirely.
Example:
package Loops;

public class Number {


public static void main(String[] args)
{
for (int i = 0; i < 5; i++) {
if (i == 2) {
continue; // Skip the rest of this loop iteration
}
if (i == 4) {
break; // Exit the loop
}
System.out.println(i);
}
}
}
9. class :
A class is like a blueprint to create objects. It defines properties(attributes), methods that an
object have.
package Condition;

public class car {


public static void main(String[] args) {
String color;
void drive() {
System.out.println("Driving");
}
}

10. Method:
A method is a block of code that performs a specific task. In Java, methods are defined inside
a class.
package test;

public class car {


public static void main(String[] args) {
public void greet() {
System.out.println("Hello, World!");
}
}
}

11. String:
A String is a data type that represents a sequence of characters, like words or sentences.
package Assignment;

public class Name {


public static void main(String[] args) {
String name = "Hima";
System.out.println(name);
}
}

12. Scanner:

The Scanner class is used to get user input in Java.


package Condition;

import java.util.Scanner;

public class Time {


public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int hours = sc.nextInt();
int minutes=sc.nextInt();
double time = hours+minutes/60.0;
if(time>0) {
if( time<=12) {
System.out.println("Good morning");
}
else if(time<16) {
System.out.println("Good Afternoon");
}
else if(time>=16 && time<=20)
{
System.out.println("Good Evening ");
}
else
{
System.out.println("Good Night ");
}
}
else
{
System.out.println("Enter positive value");
}
}
}

13. Object:
An object is an instance of a class. If Car is a class then myCar is object.
Example:
Car myCar = new Car(); // 'myCar' is an object of class 'Car'.

14. Key words:


Key words are in-built words in java that have special meanings. Total there are 52
keywords in java. These keywords cannot be used as identifiers such as variable names ,class
names, method names.
Examples: int, if, class, public.
package Condition;
import java.util.Scanner;
public class Pos {
public static void main(String[] args) {
System.out.println("Enter a number:");
Scanner sc=new Scanner(System.in);
int b = sc.nextInt();
if(b>=0) { System.out.println("positive");
}
else {
System.out.println("Negitive");
}

}
}
15. Integer.parseInt():

Integer.parseInt() is a method used to convert a String to an int.

Example:

String number = "123";

int num = Integer.parseInt(number); // Converts the String "123" to an integer 123.

16.Access Modifiers:

Access modifiers define the scope or accessibility of a class, method, or variable.


Examples:
• public: Accessible from anywhere.
• private: Accessible only within the class.
• protected: Accessible within the package and subclasses.

17.Main Method Syntax:

The main method is the entry point of a Java application. It is like heart of java.
Syntax:
public static void main(String[] args) {

// Code to run

18. Import:
import is used to bring other classes or packages into a Java program.
Example:
import java.util.Scanner; // Imports the Scanner class for input.

19. Package:
A package is like a folder that organizes classes and interfaces.
Example:
package mypackage; // Declares this class is part of 'mypackage'.

20. Comments in java:

Comments are used to explain code and are ignored by the compiler.
• Single-line Comment: // This is a comment
• Multi-line Comment: /* This is a multi-line comment */

21. Brackets used in java:


rackets define code blocks or array declarations.
• Curly Braces {}: Define a block of code.
• Square Brackets []: Used for arrays.
• Parentheses (): Used for method parameters.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy