Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SWING
- 1. CheckBox:
- import javax.swing.*;
- import java.awt.event.*;
- class CheckBox {
- JFrame f;
- JLabel label;
- JCheckBox checkbox1, checkbox2;
- JList<String> list1, list2; // Define JList components
- CheckBox() {
- f = new JFrame("CheckBox Demo");
- label = new JLabel();
- label.setHorizontalAlignment(JLabel.CENTER);
- label.setSize(400, 100);
- checkbox1 = new JCheckBox("C++");
- checkbox1.setBounds(150, 100, 50, 50);
- checkbox2 = new JCheckBox("Java");
- checkbox2.setBounds(150, 150, 50, 50);
- f.add(checkbox1);
- f.add(checkbox2);
- f.add(label);
- checkbox1.addItemListener(new ItemListenerDemo(this));
- checkbox2.addItemListener(new ItemListenerDemo(this));
- f.setSize(400, 400);
- f.setLayout(null);
- f.setVisible(true);
- }
- public static void main(String args[]) {
- new CheckBox();
- }
- }
- class ItemListenerDemo implements ItemListener {
- CheckBox cbd;
- ItemListenerDemo(CheckBox cbd) {
- this.cbd = cbd;
- }
- @Override
- public void itemStateChanged(ItemEvent e) {
- String data = "";
- if (cbd.list1.getSelectedIndex() != -1) {
- data = "Programming language Selected: " + cbd.list1.getSelectedValue();
- cbd.label.setText(data);
- }
- if (cbd.list2.getSelectedIndex() != -1) {
- data += ", Framework Selected: ";
- for (Object frame : cbd.list2.getSelectedValuesList()) {
- data += frame + " ";
- }
- }
- cbd.label.setText(data);
- }
- }
- 2. Border Layout:
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class BorderLayoutDemo {
- JFrame f;
- JButton b1;
- JButton b2;
- JButton b3;
- JButton b4;
- JLabel jl;
- public BorderLayoutDemo() {
- f = new JFrame();
- // Creating buttons
- b1 = new JButton("NORTH");
- b2 = new JButton("SOUTH");
- b3 = new JButton("EAST");
- b4 = new JButton("WEST");
- jl = new JLabel("Label@CENTER");
- f.add(b1, BorderLayout.NORTH);
- f.add(b2, BorderLayout.SOUTH);
- f.add(b3, BorderLayout.EAST);
- f.add(b4, BorderLayout.WEST);
- f.add(jl, BorderLayout.CENTER);
- b1.addActionListener(new MyActionListener(this));
- b2.addActionListener(new MyActionListener(this));
- b3.addActionListener(new MyActionListener(this));
- b4.addActionListener(new MyActionListener(this));
- f.setSize(300, 300);
- f.setVisible(true);
- }
- public static void main(String[] args) {
- new BorderLayoutDemo();
- }
- }
- class MyActionListener implements ActionListener {
- BorderLayoutDemo bld;
- MyActionListener(BorderLayoutDemo bld) {
- this.bld = bld;
- }
- public void actionPerformed(ActionEvent ae) {
- bld.jl.setText(ae.getActionCommand());
- }
- }
- 3. Card Layout:
- import java.awt.*;
- import javax.swing.*;
- class CardLayoutDemo extends JFrame
- {
- javax.swing.JButton b1;
- JButton b2;
- JButton b3;
- JButton b4;
- Container c;
- CardLayout cl;
- CardLayoutDemo()
- {
- b1 = new javax.swing.JButton("NORTH");
- b2 = new JButton("SOUTH");
- b3 = new JButton("EAST");
- b4 = new JButton("WEST");
- c = getContentPane();
- cl = new CardLayout(40,30);
- c.setLayout(cl);
- c.add(b1);
- c.add(b2);
- c.add(b3);
- c.add(b4);
- b1.addActionListener(new MyActionListener(this));
- b2.addActionListener(new MyActionListener(this));
- b3.addActionListener(new MyActionListener(this));
- b4.addActionListener(new MyActionListener(this));
- setSize(300, 300);
- setVisible(true);
- }
- public static void main(String[] args) {
- new CardLayoutDemo();
- }
- }
- class MyActionListener implements java.awt.event.ActionListener {
- CardLayoutDemo cld;
- MyActionListener(CardLayoutDemo cld){
- this.cld = cld;
- }
- public void actionPerformed(java.awt.event.ActionEvent ae){
- cld.cl.next(cld.c);
- }
- }
- 4. Box Layout:
- import javax.swing.*;
- class BoxLayoutDemo extends JFrame
- {
- javax.swing.JButton b1;
- JButton b2;
- JButton b3;
- JButton b4;
- JLabel jl;
- BoxLayoutDemo()
- {
- b1 = new javax.swing.JButton("NORTH");
- b2 = new JButton("SOUTH");
- b3 = new JButton("EAST");
- b4 = new JButton("WEST");
- jl = new JLabel("LABEL@CENTRE");
- add(b1);
- add(b2);
- add(b3);
- add(b4);
- add(jl);
- setLayout (new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.Y_AXIS));
- b1.addActionListener(new MyActionListener(this));
- b2.addActionListener(new MyActionListener(this));
- b3.addActionListener(new MyActionListener(this));
- b4.addActionListener(new MyActionListener(this));
- setSize(300, 300);
- setVisible(true);
- }
- public static void main(String[] args) {
- new BoxLayoutDemo();
- }
- }
- class MyActionListener implements java.awt.event.ActionListener {
- BoxLayoutDemo bld;
- MyActionListener(BoxLayoutDemo bld){
- this.bld = bld;
- }
- public void actionPerformed(java.awt.event.ActionEvent ae){
- bld.jl.setText(ae.getActionCommand());
- }
- }
- -------------------------------------------------------------------------------------------------------------
- 2. Multilevel Inheritance:
- // Base class representing a product
- class Product {
- private String name;
- private double price;
- public Product(String name, double price) {
- this.name = name;
- this.price = price;
- }
- public String getName() {
- return name;
- }
- public double getPrice() {
- return price;
- }
- public void displayProductInfo() {
- System.out.println("Product: " + name);
- System.out.println("Price: $" + price);
- }
- }
- // Subclass representing electronic products
- class Electronics extends Product {
- private String brand;
- public Electronics(String name, double price, String brand) {
- super(name, price);
- this.brand = brand;
- }
- public String getBrand() {
- return brand;
- }
- @Override
- public void displayProductInfo() {
- super.displayProductInfo();
- System.out.println("Brand: " + brand);
- }
- }
- // Subclass representing laptops
- class Laptop extends Electronics {
- private String processor;
- public Laptop(String name, double price, String brand, String processor) {
- super(name, price, brand);
- this.processor = processor;
- }
- public String getProcessor() {
- return processor;
- }
- @Override
- public void displayProductInfo() {
- super.displayProductInfo();
- System.out.println("Processor: " + processor);
- }
- }
- public class Multi {
- public static void main(String[] args) {
- // Create a Laptop object
- Laptop laptop = new Laptop("Dell XPS 13", 1299.99, "Dell", "Intel Core i7");
- // Display product information
- laptop.displayProductInfo();
- }
- }
- ------------
- 3. Exception Handling:
- import java.util.Scanner;
- class InvalidCreditCardException extends Exception {
- public InvalidCreditCardException(String message) {
- super(message);
- }
- }
- public class CCValidate {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- System.out.print("Enter your 16-digit credit card number: ");
- String creditCardNumber = scanner.nextLine();
- try {
- if (isValidCreditCard(creditCardNumber)) {
- System.out.println("Credit card is valid.");
- } else {
- System.out.println("Credit card is invalid.");
- }
- } catch (InvalidCreditCardException e) {
- System.out.println("Invalid Credit Card: " + e.getMessage());
- }
- scanner.close();
- }
- public static boolean isValidCreditCard(String cardNumber) throws InvalidCreditCardException {
- if (cardNumber == null || cardNumber.length() != 16) {
- throw new InvalidCreditCardException("Credit card number must be exactly 16 digits long.");
- }
- return true;
- }
- }
- -------------
- 4. Multi-threading:
- class MultithreadingDemo extends Thread {
- public void run() {
- try {
- // Displaying the thread name that is running
- System.out.println(
- "Thread " + Thread.currentThread().getName()
- + " is running");
- } catch (Exception e) {
- // Throwing an exception
- System.out.println("Exception is caught");
- }
- }
- }
- public class exp7 {
- public static void main(String[] args) {
- int n = 8; // Number of threads
- for (int i = 0; i < n; i++) {
- MultithreadingDemo object = new MultithreadingDemo();
- object.start(); // Note the lowercase 'start' method
- }
- }
- }
- --------------------
- 5. Interfaces:
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- // Define the Product interface
- interface Product {
- String getName();
- double getPrice();
- }
- // Implement the Product interface for Electronics
- class Electronics implements Product {
- private String name;
- private double price;
- public Electronics(String name, double price) {
- this.name = name;
- this.price = price;
- }
- @Override
- public String getName() {
- return name;
- }
- @Override
- public double getPrice() {
- return price;
- }
- }
- // Implement the Product interface for Clothing
- class Clothing implements Product {
- private String name;
- private double price;
- public Clothing(String name, double price) {
- this.name = name;
- this.price = price;
- }
- @Override
- public String getName() {
- return name;
- }
- @Override
- public double getPrice() {
- return price;
- }
- }
- public class Interface {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<Product> shoppingCart = new ArrayList<>();
- while (true) {
- System.out.println("Select a product category:");
- System.out.println("1. Electronics");
- System.out.println("2. Clothing");
- System.out.println("3. Checkout");
- int choice = scanner.nextInt();
- if (choice == 1) {
- System.out.print("Enter electronics name: ");
- String name = scanner.next();
- System.out.print("Enter price: ");
- double price = scanner.nextDouble();
- shoppingCart.add(new Electronics(name, price));
- } else if (choice == 2) {
- System.out.print("Enter clothing name: ");
- String name = scanner.next();
- System.out.print("Enter price: ");
- double price = scanner.nextDouble();
- shoppingCart.add(new Clothing(name, price));
- } else if (choice == 3) {
- break;
- } else {
- System.out.println("Invalid choice. Please select 1, 2, or 3.");
- }
- }
- double totalCost = calculateTotalCost(shoppingCart);
- System.out.println("Total cost of items in the shopping cart: $" + totalCost);
- scanner.close();
- }
- public static double calculateTotalCost(List<Product> shoppingCart) {
- double totalCost = 0.0;
- for (Product item : shoppingCart) {
- totalCost += item.getPrice();
- }
- return totalCost;
- }
- }
- -------------
- 6. Multiple Array:
- import java.util.Scanner;
- public class ECommerceDemo {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int maxSize = 100;
- String[] productNames = new String[maxSize];
- double[] productPrices = new double[maxSize];
- int[] productQuantities = new int[maxSize];
- int cartSize = 0;
- while (true) {
- System.out.println("E-commerce Management System");
- System.out.println("1. Add Product to Cart");
- System.out.println("2. View Cart");
- System.out.println("3. Calculate Total Cost");
- System.out.println("4. Exit");
- int choice = scanner.nextInt();
- scanner.nextLine(); // Consume the newline character
- switch (choice) {
- case 1:
- System.out.print("Enter Product Name: ");
- String productName = scanner.nextLine();
- System.out.print("Enter Product Price: $");
- double productPrice = scanner.nextDouble();
- System.out.print("Enter Quantity: ");
- int quantity = scanner.nextInt();
- productNames[cartSize] = productName;
- productPrices[cartSize] = productPrice;
- productQuantities[cartSize] = quantity;
- cartSize++;
- break;
- case 2:
- System.out.println("Your Shopping Cart:");
- for (int i = 0; i < cartSize; i++) {
- System.out.println(
- productNames[i] + " - $" + productPrices[i] + " x " + productQuantities[i]
- );
- }
- break;
- case 3:
- double totalCost = 0.0;
- for (int i = 0; i < cartSize; i++) {
- totalCost += productPrices[i] * productQuantities[i];
- }
- System.out.println("Total Cost: $" + totalCost);
- break;
- case 4:
- System.out.println("Thank you for using our e-commerce system.");
- scanner.close();
- System.exit(0);
- default:
- System.out.println("Invalid choice. Please select a valid option.");
- break;
- }
- }
- }
- }
- --------------------------------------------------------------------------------------------------------------
- Packages:
- //packages
- //Main.java
- import ecommerce.Product;
- import ecommerce.Order;
- import ecommerce.ShoppingCart;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- ShoppingCart cart = new ShoppingCart();
- while (true) {
- System.out.print("Enter product ID (0 to finish): ");
- int productId = scanner.nextInt();
- if (productId == 0) {
- break;
- }
- System.out.print("Enter product name: ");
- scanner.nextLine(); // Consume the newline character
- String productName = scanner.nextLine();
- System.out.print("Enter product price: ");
- double productPrice = scanner.nextDouble();
- System.out.print("Enter quantity: ");
- int quantity = scanner.nextInt();
- Product product = new Product(productId, productName, productPrice);
- cart.addProduct(product, quantity);
- }
- // Display the contents of the shopping cart
- System.out.println("Shopping Cart Contents:");
- cart.displayCart();
- // Create an order from the shopping cart
- Order order = new Order(cart);
- // Display the order details
- System.out.println("\nOrder Details:");
- order.displayOrder();
- scanner. Close();
- }
- }
- //ecommerce/Product.java
- package ecommerce;
- public class Product {
- private int id;
- private String name;
- private double price;
- public Product(int id, String name, double price) {
- this.id = id;
- this.name = name;
- this.price = price;
- }
- // Getters and setters for id, name, and price
- public String toString() {
- return name + " (ID: " + id + ", Price: Rs. " + price + ")";
- }
- }
- //ecommerce/Order.java
- package ecommerce;
- import java.util.Date;
- public class Order {
- private ShoppingCart cart;
- private Date orderDate;
- public Order(ShoppingCart cart) {
- this.cart = cart;
- this.orderDate = new Date();
- }
- public void displayOrder() {
- System.out.print("Order Date: " + orderDate);
- System.out.println("\n");
- System.out.println("Order Items:");
- cart.displayCart();
- }
- }
- //ecommerce/ShoppingCart.java
- package ecommerce;
- import java.util.HashMap;
- import java.util.Map;
- public class ShoppingCart {
- private Map<Product, Integer> items;
- public ShoppingCart() {
- items = new HashMap<>();
- }
- public void addProduct(Product product, int quantity) {
- items.put(product, quantity);
- }
- public void displayCart() {
- for (Map.Entry<Product, Integer> entry : items.entrySet()) {
- Product product = entry.getKey();
- int quantity = entry.getValue();
- System.out.println(product + " x" + quantity);
- }
- }
- }
- --------------------------------------------------------------------------------------------------------------------------
- MASTER-----!!!!!
- import java.awt.event.*;
- import javax.swing.*;
- import java.util.Scanner;
- // Base class representing a product
- class Product {
- private String name;
- private double price;
- public Product(String name, double price) {
- this.name = name;
- this.price = price;
- }
- public String getName() {
- return name;
- }
- public double getPrice() {
- return price;
- }
- public void displayProductInfo() {
- System.out.println("Product: " + name);
- System.out.println("Price: $" + price);
- }
- }
- // Subclass representing electronic products
- class Electronics extends Product {
- private String brand;
- public Electronics(String name, double price, String brand) {
- super(name, price);
- this.brand = brand;
- }
- public String getBrand() {
- return brand;
- }
- @Override
- public void displayProductInfo() {
- super.displayProductInfo();
- System.out.println("Brand: " + brand);
- }
- }
- // Subclass representing laptops
- class Laptop extends Electronics {
- private String processor;
- public Laptop(String name, double price, String brand, String processor) {
- super(name, price, brand);
- this.processor = processor;
- }
- public String getProcessor() {
- return processor;
- }
- @Override
- public void displayProductInfo() {
- super.displayProductInfo();
- System.out.println("Processor: " + processor);
- }
- }
- class InvalidCreditCardException extends Exception {
- public InvalidCreditCardException(String message) {
- super(message);
- }
- }
- class CCValidate {
- public static boolean isValidCreditCard(String cardNumber) throws InvalidCreditCardException {
- if (cardNumber == null || cardNumber.length() != 16) {
- throw new InvalidCreditCardException("Credit card number must be exactly 16 digits long.");
- }
- return true;
- }
- }
- class MultithreadingDemo extends Thread {
- public void run() {
- try {
- System.out.println(
- "Thread " + Thread.currentThread().getName() + " is running"
- );
- } catch (Exception e) {
- System.out.println("Exception is caught");
- }
- }
- }
- // Define the Product interface
- interface ProductInterface {
- String getName();
- double getPrice();
- }
- // Implement the Product interface for Electronics
- class ElectronicsInterface implements ProductInterface {
- private String name;
- private double price;
- public ElectronicsInterface(String name, double price) {
- this.name = name;
- this.price = price;
- }
- @Override
- public String getName() {
- return name;
- }
- @Override
- public double getPrice() {
- return price;
- }
- }
- // Implement the Product interface for Clothing
- class ClothingInterface implements ProductInterface {
- private String name;
- private double price;
- public ClothingInterface(String name, double price) {
- this.name = name;
- this.price = price;
- }
- @Override
- public String getName() {
- return name;
- }
- @Override
- public double getPrice() {
- return price;
- }
- }
- public class Master {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int maxSize = 100;
- String[] productNames = new String[maxSize];
- double[] productPrices = new double[maxSize];
- int[] productQuantities = new int[maxSize];
- int cartSize = 0;
- JFrame f = new JFrame("CheckBox Demo");
- JLabel label = new JLabel();
- label.setHorizontalAlignment(JLabel.CENTER);
- label.setSize(400, 100);
- JCheckBox checkbox1 = new JCheckBox("C++");
- checkbox1.setBounds(150, 100, 50, 50);
- JCheckBox checkbox2 = new JCheckBox("Java");
- checkbox2.setBounds(150, 150, 50, 50);
- f.add(checkbox1);
- f.add(checkbox2);
- f.add(label);
- checkbox1.addItemListener(new ItemListener() {
- public void itemStateChanged(ItemEvent e) {
- if (checkbox1.isSelected()) {
- label.setText("Programming language Selected: C++");
- }
- }
- });
- checkbox2.addItemListener(new ItemListener() {
- public void itemStateChanged(ItemEvent e) {
- if (checkbox2.isSelected()) {
- label.setText("Programming language Selected: Java");
- }
- }
- });
- f.setSize(400, 400);
- f.setLayout(null);
- f.setVisible(true);
- while (true) {
- System.out.println("E-commerce Management System");
- System.out.println("1. Add Product to Cart");
- System.out.println("2. View Cart");
- System.out.println("3. Calculate Total Cost");
- System.out.println("4. Exit");
- int choice = scanner.nextInt();
- scanner.nextLine();
- switch (choice) {
- case 1:
- System.out.print("Enter Product Name: ");
- String productName = scanner.nextLine();
- System.out.print("Enter Product Price: $");
- double productPrice = scanner.nextDouble();
- System.out.print("Enter Quantity: ");
- int quantity = scanner.nextInt();
- productNames[cartSize] = productName;
- productPrices[cartSize] = productPrice;
- productQuantities[cartSize] = quantity;
- cartSize++;
- break;
- case 2:
- System.out.println("Your Shopping Cart:");
- for (int i = 0; i < cartSize; i++) {
- System.out.println(
- productNames[i] + " - $" + productPrices[i] + " x " + productQuantities[i]
- );
- }
- break;
- case 3:
- double totalCost = 0.0;
- for (int i = 0; i < cartSize; i++) {
- totalCost += productPrices[i] * productQuantities[i];
- }
- System.out.println("Total Cost: $" + totalCost);
- break;
- case 4:
- System.out.println("Thank you for using our e-commerce system.");
- scanner.close();
- System.exit(0);
- default:
- System.out.println("Invalid choice. Please select a valid option.");
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement