Krishna Report
Krishna Report
CORE JAVA
Submitted
In partial fulfillment
For award of degree of
Bachelor of Technology
In Department of COMPUTER SCIENCE ENGINEERING
SESSION: 2022-2026
CERTIFICATE
This is to certify that the work which is begin presented in the B.Tech seminar report entitled
“CORE JAVA” has been submitted on the Chartered Institute of Technology, Aburoad
fulfilment of the requirement for the award of degree of Bachelor of technology in Computer
Science & Engineering.
HOD PRINCIPAL
Computer Science & Engineering Department Chartered Institute of technology
Chartered Institute of technology Abu Road, Rajasthan
i
DECLARATION
In accordance with the requirements for the degree of Bachelor of Technology in
Computer Science & Engineering, in Faculty of Engineering & technology, I present this
Seminar report is completed under the Supervision of Ms. Heena Bano.
I declare that the work presented in the report is my own work except as acknowledged
in the text and footnotes, and that to my knowledge this material has been submitted
either in whole or in part, for a degree at this university or at any other such Institution.
ii
iii
CONTENTS
TITLE ...................................................................................................................... Page No.
CERTIFICATE ..................................................................................................................... i
DECLARATION ...................................................................................................................ii
CONTENTS ........................................................................................................................ iii
LIST OF FIGURES .............................................................................................................. v
Chapter:1 INTRODUCTION.................................................................................................1
1.1 What is java......................................................................................................1
1.2 Why use java ...................................................................................................1
1.3 Why java is so important today.? ................................................................... 2
Chapter: 2 BUILDING STRONG FOUNDATIONS ........................................................... 3
2.1 Primitive data type ......................................................................................... 3
2.2 Data types and variables in java ..................................................................... 3
2.3 Strings in java ................................................................................................ 3
2.4 Using indexes with strings ............................................................................. 4
Chapter: 3 CONTROL FLOW ............................................................................................. 5
3.1 Mapping out program control flow ................................................................ 5
3.2 Operators in java ............................................................................................ 6
3.3 Decision-making with if in java ..................................................................... 6
3.4 Understanding scope in java .......................................................................... 6
3.5 While loops .................................................................................................... 8
Chapter: 4 DEBUGGING IN JAVA ..................................................................................... 9
4.1 Debugging syntax and logical errors ............................................................. 9
4.2 Debugging with an IDE ................................................................................. 9
Chapter: 5 FUNCTION IN JAVA ........................................................................................ 10
5.1 What are function .......................................................................................... 10
5.2 Return types in java ....................................................................................... 10
5.3 Using built-in functions in java...................................................................... 10
Chapter: 6 CLASSES IN JAVA ......................................................................................... 12
6.1 Classes in java ............................................................................................. 12
6.2 Constructors in java ...................................................................................... 13
iv
6.3 Instance methods vs. class methods ............................................................... 14
6.4 Class variable in java................................................................................... 15
6.5 Review: Classes vs. instance ...................................................................... 15
PROJECT
CONCLUSION
REFERENCES
v
LIST OF FIGURES
Fig. 3.1: Pick number ........................................................................................................... 5
Fig. 3.5: while loop .............................................................................................................. 8
Fig. 3.5: Condition true ......................................................................................................... 8
vi
INTRODUCTION
CHAPTER 1
Java is a popular programming language, created in 1995.
It is owned by Oracle, and more than 3 billion devices run java.
It is used for:
• Mobile applications
• Desktop applications
• Web applications
• Web servers and applications servers
• Game
• Database connection
• And more…
1.1 What is java
• Java is a high level object oriented programming language as well as platform.
• Java was developed by a team led by James Gosling at sun Microsystems and released
in 1995.
• Java runs on a variety of platforms such as Windows, Mac os and the various version of
UNIX.
• Java is an object oriented language which gives a clear structure to programs and allows
code to be reused, lowering development costs.
1
• The Java platform has attracted more than 6.5 million software developers.
• 1.1 billion desktops run Java.
• 930 million Java Runtime Environment downloads each year.
• 3 billion mobile phones run Java.
• 31 times more Java phones ship every year than Apple and Android combined.
2
BUILDING STRONG FOUNDATIONS
CHAPTER 2
2.1 Primitive data type
Primitive data type:- int, char, double, long, Boolean, byte.
3
2.4 Using indexes with strings
• Index: 0 1 2 3 4 5
• String: b h a r a t
4
CONTROL FLOW
CHAPTER 3
3.1 Mapping out program control flow
• A program’s control flow is the order in which the program’s instructions or code
statements are executed.
• All of the programs we’ve looked at so far execute one statement after another,
sequentially.
• Here is where explanations or definitions can go; try and keep each built two or three
lines max.
• We can manipulate which line of code is executed with special control flow statements
and conditions.
• A line of code might never be executed, be executed once, or multiple times.
• The conditions determine how many times a given line of code is executed.
Program:
• Let’s map out the control flow of a virtual fortune teller program.
• The user will pick a number.
• Depending on the number picked, the user receives a fortune.
5
3.2 Operator in Java
• Relational operators: >, <, ==, >=, <=, !=
• input Num <= 5 less than or equal to 5.
• input Num >=8 greater than or equal to 8.
• input Num == 8 equal to 8.
• input Num != 3 not equal to 3.
3.3Decision-making with if in Java
If statement
• An if statement is a control flow statement, where if the condition is true, it
performs some kind of action.
• Condition: input Num < 5
• Action: print out “Enjoy the good luck a friend brings you”. If (inputtedNum <
5) {
// print out “Enjoy the good luck a friend brings you”
}
If-else statement
• Condition: input Num < 5
• If block action: print out “enjoy the good luck a friend brings you”.
• Else block action: print “your shoe selection … today”.
If (inputtedNum <5) {
// print out “Enjoy the good luck a friend brings you”
} else {
// print out “your shoe selection will make you happy today”
}
6
• All of this has to do with a topic called scope.
Scope
• The scope of a variable is the part of the program where a piece of code is
accessible or in which it can be used.
If (inputtedNum<100) { int
favouriteNumber =5;
System.out.println(favoriteNumber);
favouriteNumber = 10;
System.out.println(favoriteNumber);
// In scope (accessible) for favoriteNumber
} else {
// Out of scope (not accessible) for favoriteNumber
}
// Out of scope (not accessible) for favoriteNumber
3.5 While loops
• A loop allows code to be executed repeatedly based on a Boolean condition.
While loop control flow
If true
START Condition
Statements
Checking
If false
7
Fig. 3.5: while loop
If true
Is Current
START print(“playing same song
Song On
Repeat again”);
If false
8
DEBUGGING IN JAVA
CHAPTER 4
4.1 Debugging syntax and logical errors
Debugging
• Debugging involves locating and fixing a program’s errors.
• These errors are often called bugs.
• In this chapter, we’ll look at some debugging strategies.
Debugging with print statement
• Before we can solve errors, we need to understand what our program is doing.
• A print statement is one tool that can often help us locate errors.
• We can use print statements to print the value of a given variable and follow the
control flow of our code.
Syntax Errors
• Syntax errors can cause your program to fail before it is run.
• Your code must be in the right format for a computer to read.
• Some common syntax errors are a misspelled variable or missing semicolon.
4.2 Debugging with an IDE
IDE: The Ultimate Tool
• An IDE helps us compiler and run our Java programs.
• An IDE can also help us debug our Java programs.
• We’ve already seen it detect and reveal syntax errors
Breakpoints
• A breakpoint is an intentional stopping point put into a program for debugging
purposes.
• With a breakpoint, we can temporarily halt a program in order to inspect its
internal state.
• Internal state: variable values, the result of certain lines if code, whether or not
a FUNCTIONS IN JAVA certain line of code is executed.
9
FUNCTIONS IN JAVA
CHAPTER 5
5.1 What is a function
A function is a series of finite steps that accomplish some task.
Example: make a PB&J
• Step 1: Gather Ingredients
• Step 2: Spread peanut butter on the other slice • Step 3: Combine two
slices and place on plate Using a function
• Task: Make a PB&J labeled makePB&JSandwich
• To use a function, we can just use its name or label, such as
makePB&&JSandwich.
• makePB&&JSandwich();
• makePB&&JSandwich();
• makePB&&JSandwich();
Defining and Using functions
• In order to use a function, we must define it first.
• We have to write code that defines the name of the function (for example, task)
as well as all of the steps needed to complete the task.
• Once the task and its steps are defined, we can use the function throughout our
program.
10
Built-In Functions
• There are many common functions that developers want to use, but don’t want
to define every time.
• Java has defined some functions for us.
• We can just call a built-in function because it’s already defined.
• System.out.println() is an example of a built-in function.
Operations
• Many of these operations on specific data types are built-in.
• .equals for Strings is built-in.
• “res” .equals(“blue”); Dot Operator
• We use the dot operator to get access to many of these built-in functions.
• System.out.println();
• “red” .equals(“blue”);
• Sometimes we’ll define our own functions and sometimes we’ll use built-in
functions.
11
CLASSES IN JAVA
CHAPTER 6
6.1 Classes in Java
Classes
• One way to make our programs easier to read is with classes.
• All of the code we’ve written so far was contained in the Main class.
• Everything we see and hear in everyday life can be represented in code.
• Example: “on-repeat” on a music player, restaurant receipt totals.
• How well something is represented is up to us.
• Classes often act as blueprints for these things.
• A class is a user defined blueprint that has a set of attributes and behaviors that
define the item that it is supposed to represent.
Behaviors of a Class
• Attributes/properties: base, height, sideLenOne, sideLenTwo, sideLenThree.
• Behaviors: functions that are related to (and located inside) the class.
• The findArea() function can use the base and height attributes to calculate the
area of the triangle.
• We can organize our code by adding this function as a behavior to our Triangle
class.
• Attributes/properties: base, height, sideLenOne, sideLenTwo, sideLenThree.
• Behaviors/methods: findArea(), calculateTriangleType().
• The calculateTriangleType() function can use the side lengths to decide the
triangle is equilateral, isosceles, or scalene.
12
• There are a bunch of different ways you can represent a triangle in a program;
this is just one way.
• The behaviors we’ve talked about so far use or manipulate attributes of the class.
• Although this is often the case, it doesn’t have to be. • We add the keyword static
if the function does not use the attributes of a class, but still relates to the overall
theme or idea of the class.
6.2 Constructors in Java
How Do We Make Individual Triangles
• We had to define functions before we could call them; we have to define classes
before we can use them.
• Defining a Triangle class does not create any triangles.
• In this video, we are going to learn how to make individual triangle instances in
our program.
What is an Instance?
• An instance is an object created from a class blueprint.
• Just like we make buildings from blueprints, we can use a class to create an
object in code.
• The Triangle class defines base, height, sideLenOne, sideLenTwo, and
sideLenThree as attributes. Triangle Instances
• An instance is an object created from a class blueprint.
• The Triangle class defines base, height, sideLenOne, sideLenTwo, and
sideLenThree as attributes.
• Example Instance One: triangleA: base = 15, height = 8, sideLenOne = 15,
sideLenTwo = 8, sideLenThree = 17.
• Example Instance Two: triangleB: base = 3, height = 2.598, sideLenOne = 3,
sideLenTwo = 3, sideLenThree = 3.
13
• We will use a constructor in the Triangle class to construct and initialize Triangle
instances
• In order to create a Triangle instance in our code, we just have to call the
constructor method of the Triangle class.
Creating a Constructor
• A constructor is a special method or behavior inside every class that creates and
initializes instances.
14
this.sideLenOne = sideLenOne;
this.sideLenTwo = sideLenTwo;
this.sideLenThree = sideLenThree;
}
• The this keyword helps our program make a distinction between the attribute
variable.
• When used with the this keyword, the dot operator allows us to access the new
Triangle’s attribute variables.
• It’s a particular way we’ve decided to store and organize data about a triangle.
Triangle triangleA = new Triangle ( 15, 8, 15, 8, 17 );
Triangle triangleB = new Triangle ( 3, 2.598, 3, 3, 3 );
Static Methods
• we did not need to create an instance of Math to use the pow methods.
15
• Pow() is a static method (also called class methods ) so we call them using the class
name.
int expNum = Math.pow ( 2, 3 );
16
6.5 Review: Classes vs. Instances
Review: Classes
• Classes help organize our code; they contain attributes ( also referred to as
properties) and behavior (also referred to as methods).
17
PROJECT
We can create a Desktop application called “ Temperature Convertor Tool”
using a feature of Java
called “JavaFX”.
• What is JavaFX?
JavaFX is a set of graphics and media packages that enables developers to
design, create, test, debug, and deploy rich client applications that operate
consistently across diverse platforms.
• Code in Main.Java :
package com.internshala.javafx;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.IOException;
import java.util.Optional;
18
}
quitMenuItem.setOnAction(event -> {
Platform.exit();
System.exit(0);
});
fileMenu.getItems().addAll(newMenuItem,sep,quitMenuItem);
aboutApp.setOnAction(event -> {
19
aboutApp();
});
helpMenu.getItems().addAll(aboutApp);
}
private static void aboutApp()
{
Alert alertDialog = new Alert(Alert.AlertType.INFORMATION);
alertDialog.setTitle("My First Desktop Application");
alertDialog.setHeaderText("Learning JavaFx");
alertDialog.setContentText("I am a beginner, but soon i will be pro.");
alertDialog.getButtonTypes().setAll(yesBtn,noBtn);
Optional<ButtonType> clickedBtn = alertDialog.showAndWait();
@Override
public void stop() throws Exception {
super.stop();
System.out.println("Stop");
}
}
• Code in Controller.java :
package com.internshala.javafx;
20
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import java.net.URL;
import java.util.ResourceBundle;
@FXML
public Label welcomeLabel;
@FXML
public ChoiceBox<String> choiceBox;
@FXML
public TextField userInputField;
@FXML
public Button convertButton;
@Override
public void initialize(URL location, Resource Bundle resources) {
choiceBox.getItems().add(C_TO_F_TEXT);
choiceBox.getItems().add(F_TO_C_TEXT);
21
choiceBox.setValue(C_TO_F_TEXT); //to show the value as default.
choiceBox.getSelectionModel().selectedItemProperty().addListener((obs
ervable, oldValue, newValue) -> {
if(newValue.equals(C_TO_F_TEXT))
{
isC_TO_F = true;
} else{
isC_TO_F = false;
}
});
convertButton.setOnAction(event -> {
convert();
});
}
private void convert()
{
String input = userInputField.getText();
float enteredTemprature = 0.0f;
try{
enteredTemprature = Float.parseFloat(input);
}catch (Exception ex)
{
warnUser();
return;
}
22
} else{
newTemprature = (enteredTemprature - 32) *5 /9;
}
display(newTemprature);
}
• Code in App_layout.fxml :
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
• Output :
24
Fig. Desktop application of Temperature Convertor Tool
CONCLUSION
25
• One of the most signification advantages of java is its ability to move easily from one
computer system to another.
• Java is platform-independent.
• Java can found everywhere from mobile phones to electronic gadgets.
26
REFERENCE
• Java Tutorial, https://www.tutorialspoint.com/java/index.htm
• Java (programming language) – Wikipedia
https://en.wikipedia.org/wiki/java_(programming_language)
• Java Tutorials, https://www.w3schools.com/java
• Java Tutorials , https://trainings.internshala.computer/java
27
28