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

Java

Uploaded by

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

Java

Uploaded by

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

//task 1

A] class Main{
public static void main(String[] args){
int age = 19; //integer value
double percent = 80.5; //double value
char div = 'N'; //Character value
String name = "NATHAN JOHNCY"; //string value
boolean isstd = true; //boolean value
[Link]("name "+name);
[Link]("age "+age);
[Link]("percentage "+percent);
[Link]("divison "+div);
[Link]("is a student? "+isstd);
}
}
B] class Main{
public static void main(String[] arg){
int num1 = 20;
int num2 = 5;
int sum = num1+num2;
int diff = num1-num2;
int mult = num1*num2;
int div = num1/num2;
int rem = num1%num2;
[Link]("sum "+sum);
[Link]("difference "+diff);
[Link]("product "+mult);
[Link]("divison " + div);
[Link]("remainder "+rem);
}
}
C] import [Link].*;
class Main{
public static void main(String[] arg){
Scanner scn = new Scanner([Link]);
[Link]("Enter the number:");
int num = [Link]();
if(num%2==0){
[Link]("the number is even.");
}
else{
[Link]("the number is odd.");
}
}
}

d] import [Link].*;
class Main{
public static void main(String[] arg){
Scanner scn = new Scanner([Link]);
[Link]("welcome");
[Link]("press 1 to perform addition");
[Link]("press 2 to perform subtraction");
[Link]("press 3 to perform multiplication");
[Link]("press 4 to perform divison");
int ch = [Link]();

[Link]("enter first number");


int num1 = [Link]();
[Link]("enter second number");
int num2 = [Link]();
switch(ch){
case 1:
[Link]("result "+(num1+num2));
break;
case 2:
[Link]("result "+(num1-num2));
break;
case 3:
[Link]("result "+(num1*num2));
break;
case 4:
if(num2==0){
[Link]("division by 0 is not
permitted");
break;
}
[Link]("result "+(num1/num2));
break;
default:
[Link]("invalid response");
}
}
[Link]("thank you!!!");
[Link]();

}
}

TASK 2
class person{
String name = "NATHAN JOHNCY";
String dob = "10th april 2005";
String mob = "9876543210";
public void readdata(){
[Link]("name: "+name);
[Link]("date of birth: "+dob);
[Link]("mobile number: "+mob);
}
}
class student extends person{
String ins = "FCRIT";
int roll = 5023133;
double sgpa = 8.7;
public void biodata(){
[Link]("institute name: "+ins);
[Link]("roll number: "+roll);
[Link]("sgpa: "+sgpa);
}
}
class employee extends person{
String compn = "reliance";
String doj = "10th august 2020";
int sal = 50000;
public void info(){
[Link]("comapany name: "+compn);
[Link]("date of joining: " +doj);
[Link]("salary: "+sal);
}
}
class Main{
public static void main(String[] arg){
[Link]("student info");
student s1 = new student();
[Link]();
[Link]();
[Link]("employee info");
employee e1 = new employee();
[Link]();
[Link]();
}
}

Task3
class Hi extends Thread
{
public void run()
{
for(int i=1;i<+5;i++)
{
[Link]("Hi");
try {
[Link](1000);
}
catch(Exception e){}
}
}
}
class Hello extends Thread
{
public void run()
{
for(int i=1;i<+5;i++)
{
[Link]("Hello");
try {
[Link](1000);
}
catch (Exception e){}
}
}
}
// click the <icon src="[Link]"/> icon in the gutter.
public class Main {
public static void main(String[] args) {
Hi obj1=new Hi();
Hello obj2=new Hello();
[Link]();
try{
[Link](10);
}
catch(Exception e){}
[Link]();

}
class Hi implements Runnable
{
public void run()
{
for(int i=1;i<+5;i++)
{
[Link]("Hi");
try {
[Link](1000);
}
catch(Exception e){}
}
}
}
class Hello implements Runnable
{
public void run()
{
for(int i=1;i<+5;i++)
{
[Link]("Hello");
try {
[Link](1000);
}
catch (Exception e){}
}
}
}
// click the <icon src="[Link]"/> icon in the gutter.
public class Main {
public static void main(String[] args) {
Runnable obj1=new Hi();
Runnable obj2=new Hello();
Thread t1=new Thread(obj1);
Thread t2=new Thread(obj2);
[Link]();
try{
[Link](10);
}
catch(Exception e){}
[Link]();

}
Task 4
1) [Link]

import [Link].*;
import [Link];
import [Link];

public class DivisionGUI {

public static void main(String[] args) {


JFrame frame = new JFrame("Experiment 4");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](300, 200);
[Link](new BoxLayout([Link](),
BoxLayout.Y_AXIS));

JTextField numeratorField = new JTextField();


JTextField denominatorField = new JTextField();
JTextField resultField = new JTextField();
[Link](false);
JButton divideButton = new JButton("Divide");

[Link](numeratorField);
[Link](denominatorField);
[Link](divideButton);
[Link](resultField);

[Link](new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
double numerator =
[Link]([Link]());
double denominator =
[Link]([Link]());
if (denominator != 0) {
[Link]([Link](numerator /
denominator));
} else {
[Link]("Cannot divide by zero");
}
} catch (NumberFormatException ex) {
[Link]("Invalid input");
}
}
});

[Link](true);
}
}

2) [Link]

import [Link].*;
import [Link].*;
import [Link].*;

public class EvenOddCheckerGUI extends JFrame implements ActionListener {


private JTextField numberField;
private JButton checkButton;

public EvenOddCheckerGUI() {
setTitle("Experiment 4");
setSize(400, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout(10, 10));

JLabel headlineLabel = new JLabel("Find Numbers Even or Odd",


[Link]);
[Link](new Font("Arial", [Link], 16));

JPanel inputPanel = new JPanel(new FlowLayout());


numberField = new JTextField(10);
[Link](new JLabel("Enter the Number"));
[Link](numberField);

JPanel buttonPanel = new JPanel(new FlowLayout([Link]));


checkButton = new JButton("Ok");
[Link](this);
[Link](checkButton);

add(headlineLabel, [Link]);
add(inputPanel, [Link]);
add(buttonPanel, [Link]);
((JPanel)
getContentPane()).setBorder([Link](10, 10, 10,
10));

setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
try {
int number = [Link]([Link]());
String message = number + " is " + (number % 2 == 0 ? "Even" :
"Odd");
[Link](this, message, "Message",
JOptionPane.INFORMATION_MESSAGE);
} catch (NumberFormatException ex) {
[Link](this, "Please enter a valid
number", "Error", JOptionPane.ERROR_MESSAGE);
}
}

public static void main(String[] args) {


new EvenOddCheckerGUI();
}
}

3) [Link]

import [Link].*;
import [Link].*;
import [Link].*;

public class SimpleCalculator extends JFrame implements ActionListener {


private final JTextField display = new JTextField();
private final JButton[] numberButtons = new JButton[10];
private final JButton[] functionButtons = {
new JButton("+"), new JButton("-"), new JButton("*"), new
JButton("/"),
new JButton("."), new JButton("="), new JButton("Clr"), new
JButton("Del")
};

private double num1 = 0, num2 = 0, result = 0;


private char operator;

public SimpleCalculator() {
setTitle("Calculator");
setSize(400, 550);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);

[Link](50, 25, 300, 50);


[Link](false);
[Link]([Link]);
add(display);

setupNumberButtons();
setupFunctionButtons();

JPanel panel = new JPanel(new GridLayout(4, 4, 10, 10));


[Link](50, 100, 300, 300);

addButtonToPanel(panel, 1, 3, 0, functionButtons);
addButtonToPanel(panel, 4, 6, 1, functionButtons);
addButtonToPanel(panel, 7, 9, 2, functionButtons);
addButtonToPanel(panel, 0, 0, 3, functionButtons);
addButtonToPanel(panel, 5, 5, 3, functionButtons);

add(panel);
setupClearDeleteButtons();

setVisible(true);
}

private void setupNumberButtons() {


for (int i = 0; i < 10; i++) {
numberButtons[i] = new JButton([Link](i));
numberButtons[i].addActionListener(this);
}
}

private void setupFunctionButtons() {


for (JButton button : functionButtons) {
[Link](this);
}
}

private void setupClearDeleteButtons() {


functionButtons[6].setBounds(50, 420, 145, 50);
functionButtons[7].setBounds(205, 420, 145, 50);
add(functionButtons[6]);
add(functionButtons[7]);
}
private void addButtonToPanel(JPanel panel, int start, int end, int
row, JButton[] buttons) {
for (int i = start; i <= end; i++) {
[Link](i < [Link] ? buttons[i] : new JButton());
}
}

@Override
public void actionPerformed(ActionEvent e) {
String buttonText = ((JButton) [Link]()).getText();

if ([Link]([Link](0))) {
[Link]([Link]() + buttonText);
} else if ([Link](".")) {
if (![Link]().contains(".")) {
[Link]([Link]() + buttonText);
}
} else if ([Link]("Clr")) {
[Link]("");
} else if ([Link]("Del")) {
String text = [Link]();
if ([Link]() > 0) {
[Link]([Link](0, [Link]() - 1));
}
} else if ([Link]("=")) {
num2 = [Link]([Link]());
switch (operator) {
case '+': result = num1 + num2; break;
case '-': result = num1 - num2; break;
case '*': result = num1 * num2; break;
case '/': result = num2 != 0 ? num1 / num2 : 0; break;
}
[Link]([Link](result));
num1 = result;
} else {
num1 = [Link]([Link]());
operator = [Link](0);
[Link]("");
}
}

public static void main(String[] args) {


[Link](SimpleCalculator::new);
}
}

Task 4_GUI_solved_DN.txt
Open with Google Docs
Displaying Task 4_GUI_solved_DN.txt.

You might also like