Core Java 1
Core Java 1
import java.awt.*;
import java.awt.event.*;
public StudentDetailsForm() {
setSize(300, 200);
setLayout(new FlowLayout());
setResizable(false);
submitButton.addActionListener(this);
add(sidLabel);
add(sidField);
add(snameLabel);
add(snameField);
add(saddrLabel);
add(saddrField);
add(submitButton);
setVisible(true);
addWindowListener(new WindowAdapter() {
System.exit(0);
});
}
displayFrame.setTitle("Student Details");
displayFrame.setSize(300, 200);
displayFrame.setLayout(new FlowLayout());
displayFrame.setResizable(false);
displayFrame.add(sidLabel);
displayFrame.add(snameLabel);
displayFrame.add(saddrLabel);
displayFrame.setVisible(true);
2) Write a package MCA which has one class student. Accept student details through parameterized
constructor. Write display() method to display details. reate a main class which will use package and calculate
Total marks and percentage
package MCA;
public Student(String name, int rollNumber, int marks1, int marks2, int marks3) {
this.name = name;
this.rollNumber = rollNumber;
this.marks1 = marks1;
this.marks2 = marks2;
this.marks3 = marks3;
3) Write Java programy w which accepts string from user, if its length is less than five, then throw user defined
exception "Invalid String" otherwise display string is uppercase
import java.util.Scanner;
super(message);
}}
try {
if (input.length() < 5) {
throw new InvalidStringException("Invalid String");
} else {
} catch (InvalidStringException e) {
import java.applet.Applet;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
setLayout(new FlowLayout());
passwordField.setEchoChar('*');
loginButton.addActionListener(this);
add(usernameLabel);
add(usernameField);
add(passwordLabel);
add(passwordField);
add(loginButton);
if (e.getSource() == loginButton) {
String username = usernameField.getText();
showStatus("Login successful!");
} else {
usernameField.setText("");
passwordField.setText(""); } } }
5) What is recursion is Java? Write a Java Program to final factorial of a given number using recursion
int number = 5;
if (n == 0 || n == 1) {
return 1;
} else {
6) Write a java program to copy the dates from one file into another file
import java.io.*;
try {
File sourceFile = new File(sourceFilePath);
int bytesRead;
fos.write(buffer, 0, bytesRead);
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
7) Write a java program to accept' 'n' integers from the user & store them in an ArrayList Collection. Display the
elements of ArrayList collection in reverse order
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
int n = scanner.nextInt();
numbers.add(num);
Collections.reverse(numbers);
System.out.println(num);
int n = 100;
if (isPerfectNumber(i)) {
System.out.println(i);
int sum = 0;
if (number % i == 0) {
sum += i;
9) Write a Java program to calculate area of circle, Triangle and Rectangle (Use Method over loading)
10) Write a Java program to count number of digits, spaces and characters from a file
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
try {
int character;
int digitCount = 0;
int spaceCount = 0;
int charCount = 0;
if (Character.isDigit(c)) {
digitCount++;
} else if (Character.isWhitespace(c)) {
spaceCount+
charCount++;
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
11) Write a java program to count number of Lines, words and characters from given file
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
int lineCount = 0;
int wordCount = 0;
int charCount = 0;
String line;
lineCount++;
charCount += line.length();
wordCount += words.length;
} catch (IOException e) {
e.printStackTrace();
12) Write a Java program to design email registration form. (Use swing component)
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public EmailRegistrationForm() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 200);
setLocationRelativeTo(null);
registerButton.addActionListener(new ActionListener() {
@Override
JOptionPane.showMessageDialog(EmailRegistrationForm.this,
});
add(emailLabel);
add(emailField);
add(passwordLabel);
add(passwordField);
SwingUtilities.invokeLater(new Runnable() {
@Override
form.setVisible(true);
});
13) Create a class Teacher (Tid, Tname, Designation, Salary, Subject). Write a java program to accept 'n'
teachers and display who teach Java subject (Use Array of object)
import java.util.Scanner;
public Teacher(int tid, String tname, String designation, double salary, String subject) {
this.tid = tid;
this.tname = tname;
this.designation = designation;
this.salary = salary;
this.subject = subject;
return subject;
}
return "Teacher ID: " + tid + "\nTeacher Name: " + tname + "\nDesignation: " + designation + "\nSalary: $"
+ salary + "\nSubject: " + subject;
int n = scanner.nextInt();
System.out.print("Designation: ");
System.out.print("Salary: ");
System.out.print("Subject: ");
if (teacher.getSubject().equalsIgnoreCase("Java")) {
System.out.println(teacher);