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

java basic programs

The document contains multiple Java programs demonstrating various programming concepts including conditional statements, loops, array manipulation, and the use of the 'this' keyword. Each program serves a specific function, such as calculating the GCD of two numbers, copying elements from one array to another, and printing elements of an array. Additionally, it showcases object-oriented programming principles through class definitions and constructors.

Uploaded by

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

java basic programs

The document contains multiple Java programs demonstrating various programming concepts including conditional statements, loops, array manipulation, and the use of the 'this' keyword. Each program serves a specific function, such as calculating the GCD of two numbers, copying elements from one array to another, and printing elements of an array. Additionally, it showcases object-oriented programming principles through class definitions and constructors.

Uploaded by

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

1.

public class Student {


2. public static void main(String[] args) {
3. int x = 10;
4. int y = 12;
5. if(x+y < 10) {
6. System.out.println("x + y is less than 10");
7. } else {
8. System.out.println("x + y is greater than 20");
9. }
10. }
11. }

1. public class Student {


2. public static void main(String[] args) {
3. String city = "Delhi";
4. if(city == "Meerut") {
5. System.out.println("city is meerut");
6. }else if (city == "Noida") {
7. System.out.println("city is noida");
8. }else if(city == "Agra") {
9. System.out.println("city is agra");
10. }else {
11. System.out.println(city);
12. }
13. }
14. }

1. public class Student {


2. public static void main(String[] args) {
3. String address = "Delhi, India";
4.
5. if(address.endsWith("India")) {
6. if(address.contains("Meerut")) {
7. System.out.println("Your city is Meerut");
8. }else if(address.contains("Noida")) {
9. System.out.println("Your city is Noida");
10. }else {
11. System.out.println(address.split(",")[0]);
12. }
13. }else {
14. System.out.println("You are not living in India");
15. }
16. }
17. }

1. public class Student implements Cloneable {


2. public static void main(String[] args) {
3. int num = 2;
4. switch (num){
5. case 0:
6. System.out.println("number is 0");
7. break;
8. case 1:
9. System.out.println("number is 1");
10. break;
11. default:
12. System.out.println(num);
13. }
14. }
15. }

1. public class Calculation {


2. public static void main(String[] args) {
3. int i = 0;
4. System.out.println("Printing the list of first 10 even numbers \n");
5. do {
6. System.out.println(i);
7. i = i + 2;
8. }while(i<=10);
9. }
10. }
GCD of Numbers
1. public class FindGCD
2. {
3. public static void main(String[] args)
4. {
5. //x and y are the numbers to find the GCF
6. int x = 12, y = 8, gcd = 1;
7. //running loop form 1 to the smallest of both numbers
8. for(int i = 1; i <= x && i <= y; i++)
9. {
10. //returns true if both conditions are satisfied
11. if(x%i==0 && y%i==0)
12. //storing the variable i in the variable gcd
13. gcd = i;
14. }
15. //prints the gcd
16. System.out.printf("GCD of %d and %d is: %d", x, y, gcd);
17. }
18. }

copy all elements of one array into another array


1. public class CopyArray {
2. public static void main(String[] args) {
3. //Initialize array
4. int [] arr1 = new int [] {1, 2, 3, 4, 5};
5. //Create another array arr2 with size of arr1
6. int arr2[] = new int[arr1.length];
7. //Copying all elements of one array into another
8. for (int i = 0; i < arr1.length; i++) {
9. arr2[i] = arr1[i];
10. }
11. //Displaying elements of array arr1
12. System.out.println("Elements of original array: ");
13. for (int i = 0; i < arr1.length; i++) {
14. System.out.print(arr1[i] + " ");
15. }
16.
17. System.out.println();
18.
19. //Displaying elements of array arr2
20. System.out.println("Elements of new array: ");
21. for (int i = 0; i < arr2.length; i++) {
22. System.out.print(arr2[i] + " ");
23. }
24. }
25. }

Program to print the elements of an array


1. public class PrintArray {
2. public static void main(String[] args) {
3. //Initialize array
4. int [] arr = new int [] {1, 2, 3, 4, 5};
5. System.out.println("Elements of given array: ");
6. //Loop through the array by incrementing value of i
7. for (int i = 0; i < arr.length; i++) {
8. System.out.print(arr[i] + " ");
9. }
10. }
11. }

This keyword
1. class Student{
2. int rollno;
3. String name;
4. float fee;
5. Student(int rollno,String name,float fee){
6. rollno=rollno;
7. name=name;
8. fee=fee;
9. }
10. void display(){System.out.println(rollno+" "+name+"
"+fee);}
11. }
12. class TestThis1{
13. public static void main(String args[]){
14. Student s1=new Student(111,"ankit",5000f);
15. Student s2=new Student(112,"sumit",6000f);
16. s1.display();
17. s2.display();
18. }}

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