Java Assignment
Java Assignment
Write a program with a method named getTotal that accepts two integers as an
argument and return its sum. Call this method from main( ) and print the results.
2. Write a method named isEven that accepts an int argument. The method should
return true if the argument is even, or false otherwise. Also write a program to test
your method
return number % 2 == 0;
int number2 = 7;
10 is even: true
7 is even: false
int sum = 0;
sum += array[i];
Object-Oriented Programming
Feature (OOP) Structural Programming
int sum = 0;
sum += numbers[i];
6. Write a Java program to sort a numeric array and a string array. The output should
print out: Original numeric array : [1789, 2035, 1899, 1456, 2013, 1458, 2458, 1254,
1472, 2365, 1456, 2165, 1457, 2456] Sorted numeric array : [1254, 1456, 1456, 1457,
1458, 1472, 1789, 1899, 2013, 2035, 2165, 2365, 2456, 2458] Original string array :
[Java, Python, PHP, C#, C Programming, C++] Sorted string array : [C Programming,
C#, C++, Java, PHP, Python]
import java.util.Arrays;
int[] numericArray = {1789, 2035, 1899, 1456, 2013, 1458, 2458, 1254, 1472, 2365, 1456, 2165,
1457, 2456};
Arrays.sort(numericArray);
Arrays.sort(stringArray);
}
Original numeric array: [1789, 2035, 1899, 1456, 2013, 1458, 2458, 1254, 1472, 2365, 1456, 2165,
1457, 2456]
Sorted numeric array: [1254, 1456, 1456, 1457, 1458, 1472, 1789, 1899, 2013, 2035, 2165, 2365,
2456, 2458]
7. Write JAVA code that creates a new JFrame object and sets its size. The code should
then create a new JButton and JLabel object, and adds them to the frame using the
add method. The layout manager of the frame should be set to FlowLayout, which
arranges the components in a horizontal row. Finally, the default close operation of
the frame is set to EXIT_ON_CLOSE, and the frame is made visible using the setVisible
method
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.FlowLayout;
frame.setSize(300, 200);
frame.setLayout(new FlowLayout());
frame.add(button);
frame.add(label);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
8. Revenue can be calculated as the selling price of the product times the quantity sold,
i.e., revenue = price × quantity. Write a program that asks the user to enter product
price and quantity and then calculate the revenue. If the revenue is more than 5000 a
discount is 10% offered. Program should display the discount and net revenue.
import java.util.Scanner;
}
System.out.println("Revenue: $" + revenue);
scanner.close();
Revenue: $6300.0
Discount: $630.0
9. Write a JAVA code that create a JFrame object called frame with the title "Add Two
Numbers". It also creates a JLabel object called resultLabel to display the result of the
addition operation, and two JTextField objects called num1Field and num2Field to
input the two numbers.
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.FlowLayout;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
frame.add(num1Field);
frame.add(num2Field);
frame.add(resultLabel);
frame.pack();
frame.setVisible(true);
10. The code then creates a JButton object called addButton with the label "Add" and
add an ActionListener to it by passing this (the current object, which implements the
ActionListener interface) to its addActionListener method. When the button is clicked,
the actionPerformed method is called, which gets the values from the text fields, adds
them, and displays the result in the resultLabel.
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
addButton.addActionListener(this);
frame.add(num1Field);
frame.add(num2Field);
frame.add(addButton);
frame.add(resultLabel);
frame.pack();
frame.setVisible(true);
@Override
try {
new AddTwoNumbers();
11. create a JPanel object called panel to hold the components, set its layout to
GridLayout, and add the components to it. Add the panel to the frame using the add
method.
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public AddTwoNumbers() {
frame.setLayout(new FlowLayout());
addButton.addActionListener(this);
panel.add(num1Field);
panel.add(num2Field);
panel.add(addButton);
panel.add(resultLabel);
frame.add(panel);
frame.pack();
frame.setVisible(true);
@Override
try {
new AddTwoNumbers();
12. Set the default close operation of the frame to JFrame.EXIT_ON_CLOSE, set its size to
300x200 pixels, and make it visible using the setVisible method. Finally, create a new
AddTwoNumbersGUI object in the main method to start the GUI.
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
frame.setSize(300, 200);
addButton.addActionListener(this);
panel.add(num1Field);
panel.add(num2Field);
panel.add(addButton);
panel.add(resultLabel);
frame.add(panel);
frame.setVisible(true);
@Override
try {
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public ChatFrame() {
setTitle("Chat Frame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
chatArea.setEditable(false);
add(scrollPane, BorderLayout.CENTER);
messageField.addActionListener(this);
add(messageField, BorderLayout.SOUTH);
sendButton.addActionListener(this);
add(sendButton, BorderLayout.EAST);
pack();
setLocationRelativeTo(null);
setVisible(true);
@Override
if (!message.isEmpty()) {
messageField.setText("");
SwingUtilities.invokeLater(new Runnable() {
@Override
new ChatFrame();
});
14. Write a program that asks for a temperature in Celsius and prints out the
temperature in Fahrenheit. Use InputBox for input and OutputBox for output. The
formula to convert Celsius to the equivalent Fahrenheit is: fahrenheit = 1.8 x celsius +
32
import javax.swing.JOptionPane;
// Calculate Fahrenheit
15. If a triangle has side lengths a,b,c, then the formula for the area of the triangle is area
= √s(s − a)(s − b)(s − c), where = (a + b + c)/2. Write a program that asks the user to
enter three sides of triangle. The progr s am should compute and display the area of
triangle.
import java.util.Scanner;
double a = scanner.nextDouble();
double b = scanner.nextDouble();
double c = scanner.nextDouble();
double s = (a + b + c) / 2;
// Calculate the area using the formula
scanner.close();