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

Assignment of Advanced Programming

The document contains 10 Java programs covering topics such as GET and SET methods, multithreading using thread extension and runnable interface, creating a GUI frame by extending JFrame, copying and sorting arrays, using StringBuffer, comparing strings, substring, and filling arrays. Each program contains the full code to demonstrate the relevant Java concept.

Uploaded by

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

Assignment of Advanced Programming

The document contains 10 Java programs covering topics such as GET and SET methods, multithreading using thread extension and runnable interface, creating a GUI frame by extending JFrame, copying and sorting arrays, using StringBuffer, comparing strings, substring, and filling arrays. Each program contains the full code to demonstrate the relevant Java concept.

Uploaded by

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

Assignment of Advanced Programming

On

Java Programs

Submitted To: - Submitted By:-


Mr.Anuj -------------------
--------------------
M.Tech 1st Semester

SHRI MATA VAISHNO DEVI UNIVERSITY


1. Program of GET and SET method :-

import java.util.*;

public class GETSET {

int x = 0;

void setx(int x) {

this.x = x;
}

void getx() {
System.out.println("The value of x " + x);
}

public static void main(String[] args) {

GETSET g = new GETSET();

System.out.println("Enter the Value of X");

Scanner sc = new Scanner(System.in);

int a = sc.nextInt();
g.setx(a);
g.getx();

}
}

2. Program of Multithreading by Extending Thread Class:-


public class ExtendingThread extends Thread{
public void run()
{
System.out.println("Thread is running");
}
public static void main(String[] args) {
ExtendingThread e = new ExtendingThread();
e.start();

3. Program of Implementing Runnable Interface:-

public class ImplementRun implements Runnable{


public void run()
{
System.out.println("Thread is running");
}
public static void main(String[] args) {
ImplementRun r = new ImplementRun();
Thread t = new Thread(r);
t.start();
}
}

4. Program of Frame by Extending JFrame:-

import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.*;
import java.awt.event.*;

class Frame extends JFrame {

Frame() {

setTitle("Anmol");
setSize(600, 600);
JButton btn = new JButton("yellow");
JLabel jlbl = new JLabel("color");
JPanel pnl = new JPanel();

this.add(pnl);
pnl.add(btn);
pnl.add(jlbl);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
pnl.setBackground(Color.red);
}
});

JMenuBar menu = new JMenuBar();


pnl.add(menu);
JMenu menu1 = new JMenu("file");
JMenu menu2 = new JMenu("help");
menu.add(menu1);
menu.add(menu2);
JMenuItem item1 = new JMenuItem("open");
menu1.add(item1);
item1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {

}
});

public static void main(String args[]) {

EventQueue.invokeLater(new Runnable() {
public void run() {
Frame f = new Frame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}

});

}
}

5. Program to copy one array to another :-

import java.util.Arrays;
import java.util.Scanner;

public class ArrayCopy {


public static void main(String[] args) {

int [] arrcopy;
int [] arr = new int[5];
Arrays.fill(arr,4);
String str = Arrays.toString(arr);
System.out.println("Arays is :" + str);

arrcopy = Arrays.copyOf(arr,2);

System.out.println("new Array is :" + Arrays.toString(arrcopy));

}
}

6. Program to Sort an array:-

import java.util.*;

public class Sort {

public static void main(String args[]) {


Student[] arr = new Student[3];

Scanner sc = new Scanner(System.in);

System.out.println("Enter the Salary");

for (int i = 0; i < 3; i++) {

arr[i].salary = sc.nextInt();

Arrays.sort(arr);

for (Student st : arr) {

System.out.println(st.salary);

}
}
}
7. Program of StringBuffer:-

public class StringBufferExample {

public static void main(String[] args) {

String abc = "ANmol";


StringBuffer sb = new StringBuffer(abc);
sb.append("Sharma");
System.out.println(sb);
}

8. Program to Compare two Strings:-

import java.util.Scanner;

public class CompareStrings {

public static void main(String[] args) {

System.out.println("Enter the First String");


Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
System.out.println("Enter the Second String");
Scanner scc = new Scanner(System.in);
String inputt = scc.nextLine();

if (input.equals(inputt)) {
System.out.println("Strings are equal");
} else {
System.out.println("Strings are not equal");

}
}
9.Program for SubString:-

public class SubStringExample {

public static void main(String[] args) {

String str = "Java programming Language ";

String a = str.substring(5, 7);

System.out.println("SubString is :" + a);


}
}

10.Program for Fill Array:-

import java.util.Arrays;

public class FillExample {

public static void main(String[] args) {

int[] arr = new int[5];


Arrays.fill(arr,7);
String a = Arrays.toString(arr);
System.out.println(a);
}
}

You might also like