ilovepdf_merged
ilovepdf_merged
System.out.println("Hello JAVA");
2. Write a program to print the value given by the user using command-line arguments.
if (args.length > 0) {
} else {
3. Write a program to print the sum of two numbers given by the user using command-line
arguments.
if (args.length == 2) {
} else {
4. Write a program to print the sum of two numbers given by the user using the Scanner
class.
import java.util.Scanner;
scanner.close();
}
}
class DefaultConstructor {
DefaultConstructor() {
new DefaultConstructor();
constructors.
class ParameterizedConstructor {
int value;
ParameterizedConstructor(int value) {
this.value = value;
ParameterizedConstructor(ParameterizedConstructor obj) {
this.value = obj.value;
+ value);
class ThisKeyword {
int value;
ThisKeyword(int value) {
this.value = value;
void display() {
8. Write a program to demonstrate static and instance data members and methods.
class StaticInstanceDemo {
int instanceValue;
StaticInstanceDemo(int instanceValue) {
this.instanceValue = instanceValue;
void instanceMethod() {
instanceValue);
staticMethod();
obj.instanceMethod();
package mypackage;
// Main.java
import mypackage.MyClass;
obj.displayMessage();
}
10. Write a program which illustrates the concept of inheritance.
class Parent {
void display() {
void show() {
obj.display();
obj.show();
11. Write a program to demonstrate the concept of method overloading and overriding.
class Overload {
@Override
obj.display(10);
obj.display("Hello");
class Outer {
private int value = 10;
class Inner {
void display() {
inner.display();
13. Write a program illustrating the concept of super keyword at variable and method level.
class Parent {
void display() {
void display() {
super.display();
obj.display();
14. Write a program to calculate the sum of two integers and two floats using an abstract
method sum().
@Override
void sum(int a, int b) {
@Override
calc.sum(10, 20);
calc.sum(10.5f, 20.5f);
15. Write a program to demonstrate the use of implementing and extending interfaces.
interface A {
void methodA();
interface B extends A {
void methodB();
}
class C implements B {
@Override
@Override
obj.methodA();
obj.methodB();
class FinalDemo {
obj.display();
}
Java Assignment Solutions - Assignment 2
Exception.
try {
int result = 10 / 0;
System.out.println(result);
} catch (ArithmeticException e) {
2. Write a program to implement the concept of Exception Handling using throw keyword.
} else {
System.out.println("Welcome to vote!");
}
public static void main(String[] args) {
try {
validate(16);
} catch (IllegalArgumentException e) {
import java.io.FileWriter;
import java.io.IOException;
} catch (IOException e) {
}
4. Write a program to draw different shapes in an applet using graphic class.
import java.applet.Applet;
import java.awt.Graphics;
import java.applet.Applet;
import java.awt.Graphics;
6. Write a java program which will create a window and an empty area within that window
CreateWindow() {
setTitle("My Window");
setSize(400, 300);
setVisible(true);
new CreateWindow();
import java.awt.*;
import java.awt.event.*;
Label label;
KeyboardEventDemo() {
addKeyListener(this);
setSize(400, 300);
setLayout(null);
setVisible(true);
new KeyboardEventDemo();
import java.awt.*;
import java.awt.event.*;
public class MouseEventDemo extends Frame implements MouseListener {
Label label;
MouseEventDemo() {
add(label);
addMouseListener(this);
setSize(400, 300);
setLayout(null);
setVisible(true);
new MouseEventDemo();
}
9. Write a program to enter two numbers in two different text fields, add a button, and display
import java.awt.*;
import java.awt.event.*;
Button addButton;
SumCalculatorGUI() {
tf3.setEditable(false);
addButton.addActionListener(this);
add(tf1);
add(tf2);
add(tf3);
add(addButton);
setSize(400, 300);
setLayout(null);
setVisible(true);
tf3.setText(String.valueOf(num1 + num2));
new SumCalculatorGUI();
}
Java Assignment Solutions - Assignment 3
import javax.swing.*;
frame.add(textArea);
frame.setSize(400, 300);
frame.setLayout(null);
frame.setVisible(true);
import javax.swing.*;
frame.add(label);
frame.setSize(400, 300);
frame.setLayout(null);
frame.setVisible(true);
3. Write a program to display selected item from JComboBox in a label on click of Show
button.
import javax.swing.*;
import java.awt.event.*;
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.setText("Selected: " +
comboBox.getItemAt(comboBox.getSelectedIndex()));
});
frame.add(comboBox);
frame.add(label);
frame.add(button);
frame.setSize(400, 300);
frame.setLayout(null);
frame.setVisible(true);
4. Create a swing application to display a menu bar where each menu item should display a
import javax.swing.*;
import java.awt.event.*;
clicked!"));
clicked!"));
menu.add(open);
menu.add(save);
menu.add(exit);
menuBar.add(menu);
frame.setJMenuBar(menuBar);
frame.setSize(400, 300);
frame.setLayout(null);
frame.setVisible(true);
5. Create this frame message that will show according to the radio button clicked.
import javax.swing.*;
import java.awt.event.*;
public class RadioButtonExample {
group.add(rb1);
group.add(rb2);
frame.add(rb1);
frame.add(rb2);
frame.add(label);
frame.setSize(400, 300);
frame.setLayout(null);
frame.setVisible(true);
import java.awt.event.*;
button.addActionListener(e -> {
int total = 0;
if (pizza.isSelected()) {
order.append("Pizza ");
total += 10;
}
if (burger.isSelected()) {
order.append("Burger ");
total += 5;
if (sandwich.isSelected()) {
order.append("Sandwich ");
total += 7;
result.setText(order.toString());
});
frame.add(label);
frame.add(pizza);
frame.add(burger);
frame.add(sandwich);
frame.add(button);
frame.add(result);
frame.setSize(400, 400);
frame.setLayout(null);
frame.setVisible(true);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
t.start();
Interface.
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
t.start();
import java.sql.*;
try {
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username",
"password");
conn.close();
} catch (SQLException e) {
10. Write a program to enter the details of an employee in a window and add the record to the
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
button.addActionListener(e -> {
DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username",
"password")) {
stmt.setString(1, name);
stmt.executeUpdate();
});
frame.add(nameLabel);
frame.add(nameField);
frame.add(button);
frame.add(result);
frame.setSize(400, 300);
frame.setLayout(null);
frame.setVisible(true);