0% found this document useful (0 votes)
3 views9 pages

Java Basics: Programs & Examples

The document provides a series of simple Java program examples covering basic concepts such as printing a line, adding data fields, method calling, object creation, methods with parameters and return values, constructors, and multiple methods usage. Each example includes a complete code snippet demonstrating the concept. The document serves as a foundational guide for beginners learning Java programming.

Uploaded by

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

Java Basics: Programs & Examples

The document provides a series of simple Java program examples covering basic concepts such as printing a line, adding data fields, method calling, object creation, methods with parameters and return values, constructors, and multiple methods usage. Each example includes a complete code snippet demonstrating the concept. The document serves as a foundational guide for beginners learning Java programming.

Uploaded by

kmd34393
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Java

Write java program on


i. Simple java program to print a line
ii. Add data filed method in program
iii. Create Method calling method
iv. Create object
1. Simple Java Program to Print a
Line
public class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, this is my first
Java program.");
}
}
2. Add Data Field to a Class
public class Student {
String name = "Tarek"; // Data field

public static void main(String[] args) {


Student s = new Student();
[Link]("Student name: " + [Link]);
}
}
3. Method Calling Another Method
public class Calculator {
public void add() {
int a = 10, b = 20;
[Link]("Sum is: " + (a + b));
}

public void startCalculation() {


add(); // Calling another method
}

public static void main(String[] args) {


Calculator c = new Calculator();
[Link]();
}
}
4. Object Creation Example
public class Car {
String color = "Red";

public static void main(String[] args) {


Car myCar = new Car(); // Object creation
[Link]("Car color: " + [Link]);
}
}
5. Method with Parameters and
Return Value
public class MathOperations {
public int multiply(int x, int y) {
return x * y;
}

public static void main(String[] args) {


MathOperations obj = new MathOperations();
int result = [Link](5, 4);
[Link]("Multiplication result: " + result);
}
}
6. Constructor Example
public class Book {
String title;

// Constructor
public Book(String t) {
title = t;
}

public static void main(String[] args) {


Book b1 = new Book("Java Programming");
[Link]("Book title: " + [Link]);
}
}
7. Multiple Methods and Object
Use
public class Person {
String name = "ABC";
int age = 16;

public void displayInfo() {


[Link]("Name: " + name);
[Link]("Age: " + age);
}

public static void main(String[] args) {


Person p = new Person(); // Creating object
[Link](); // Calling method
}
}
Thank
You

You might also like