slip No.17
1.
import java.util.Scanner;
class Customer {
String name;
String phoneNumber;
void readCustomer(Scanner sc) {
System.out.print("Enter Customer Name: ");
name = sc.nextLine();
System.out.print("Enter Phone Number: ");
phoneNumber = sc.nextLine();
}
void displayCustomer() {
System.out.println("Customer Name : " + name);
System.out.println("Phone Number : " + phoneNumber);
}
}
class Depositor extends Customer {
String accno;
double balance;
void readDepositor(Scanner sc) {
readCustomer(sc);
System.out.print("Enter Account Number: ");
accno = sc.nextLine();
System.out.print("Enter Balance: ");
balance = sc.nextDouble();
sc.nextLine(); // consume newline
}
void displayDepositor() {
displayCustomer();
System.out.println("Account Number : " + accno);
System.out.println("Balance : " + balance);
}
}
class Borrower extends Depositor {
String loanNo;
double loanAmt;
void readBorrower(Scanner sc) {
readDepositor(sc);
System.out.print("Enter Loan Number: ");
loanNo = sc.nextLine();
System.out.print("Enter Loan Amount: ");
loanAmt = sc.nextDouble();
sc.nextLine();
}
void displayBorrower() {
displayDepositor();
System.out.println("Loan Number : " + loanNo);
System.out.println("Loan Amount : " + loanAmt);
System.out.println("-----------------------------------");
}
}
public class CustomerDetails {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of customers: ");
int n = sc.nextInt();
sc.nextLine();
Borrower[] customers = new Borrower[n];
for (int i = 0; i < n; i++) {
System.out.println("\nEnter details for Customer " + (i + 1));
customers[i] = new Borrower();
customers[i].readBorrower(sc);
}
// Display details
System.out.println("\n--- Customer Details ---");
for (int i = 0; i < n; i++) {
System.out.println("\nCustomer " + (i + 1));
customers[i].displayBorrower();
}
sc.close();
}
}
2.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class StringOperations extends JFrame implements ActionListener {
JTextField text1, text2, text3;
JButton concatBtn, reverseBtn;
public StringOperations() {
setTitle("String Operations");
setSize(400, 200);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(new JLabel("Enter First String:"));
text1 = new JTextField(20);
add(text1);
add(new JLabel("Enter Second String:"));
text2 = new JTextField(20);
add(text2);
add(new JLabel("Result:"));
text3 = new JTextField(20);
text3.setEditable(false);
add(text3);
concatBtn = new JButton("Concatenate");
reverseBtn = new JButton("Reverse");
add(concatBtn);
add(reverseBtn);
concatBtn.addActionListener(this);
reverseBtn.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == concatBtn) {
String s1 = text1.getText();
String s2 = text2.getText();
text3.setText(s1 + s2);
} else if (e.getSource() == reverseBtn) {
String s1 = text1.getText();
StringBuilder sb = new StringBuilder(s1);
text3.setText(sb.reverse().toString());
}
}
public static void main(String[] args) {
new StringOperations();
}
}
Comments
Post a Comment