21xrx.com
2024-12-23 01:31:10 Monday
登录
文章检索 我的文章 写文章
JAVA经典开发案例:一篇带你入门的教程
2023-06-14 15:13:02 深夜i     --     --
Java 开发案例 计算器 阶乘 图书管理系统

Java是当今最流行的编程语言之一,由于其简单易用、跨平台的特性,越来越多的人开始学习并使用Java编程。但是,对于初学者来说,Java的学习曲线可能会感到有点陡峭,不过有了一些经典的开发案例,不仅可以更好地理解Java的语法和应用,还能提高编程的技术水平。

本文将带领初学者一步步通过Java经典开发案例入门,旨在让读者更好地掌握Java的基础知识和应用技巧。下面介绍3个经典开发案例:

1.计算器应用程序

计算器应用程序是实现Java基础操作的必备案例之一,该程序可以帮助初学者更好地理解方法调用、布尔表达式和逻辑控制流程等概念,以及Java中如何实现用户界面。

代码案例:


import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Calculator extends JFrame implements ActionListener {

  private static final long serialVersionUID = 1L;

  private JTextField tfResult;

  private JPanel panel;

  private int total1 = 0;

  private String operator = "=";

  private boolean calculating = true;

  public Calculator() {

    tfResult = new JTextField("0", 12);

    tfResult.setHorizontalAlignment(JTextField.RIGHT);

    tfResult.setEditable(false);

    add(tfResult, BorderLayout.NORTH);

    panel = new JPanel();

    panel.setLayout(new GridLayout(4, 4));

    String buttons = "789/456*123-0.=+";

    for (int i = 0; i < buttons.length(); i++) {

      JButton button = new JButton(buttons.substring(i, i + 1));

      button.addActionListener(this);

      panel.add(button);

    }

    add(panel, BorderLayout.CENTER);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setTitle("Calculator");

    setSize(180, 150);

    setLocationRelativeTo(null);

    setVisible(true);

  }

  public void actionPerformed(ActionEvent evt) {

    String command = evt.getActionCommand();

    if ('0' <= command.charAt(0) && command.charAt(0) <= '9' || command.equals(".")) {

      if (calculating) {

        tfResult.setText(command);

      } else {

        tfResult.setText(tfResult.getText() + command);

      }

      calculating = false;

    } else {

      if (calculating) {

        if (command.equals("-")) {

          tfResult.setText(command);

          calculating = false;

        } else

          operator = command;

        

      } else {

        int total2 = Integer.parseInt(tfResult.getText());

        if (operator.equals("+")) {

          total1 += total2;

        } else if (operator.equals("-"))

          total1 -= total2;

         else if (operator.equals("*")) {

          total1 *= total2;

        } else if (operator.equals("/"))

          total1 /= total2;

         else if (operator.equals("="))

          total1 += total2;

        

        tfResult.setText("" + total1);

        operator = command;

        calculating = true;

      }

    }

  }

  public static void main(String[] args) {

    Calculator calculator = new Calculator();

  }

}

2.阶乘程序

阶乘程序是一个用于演示递归和循环的简单程序,其主要功能是计算给定的数n的阶乘。通过这个案例,你将学到如何使用递归来解决问题,以及如何使用循环控制结构来优化算法。

代码案例:


import java.util.Scanner;

public class Factorial {

  public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    System.out.print("Enter a number: ");

    int n = scanner.nextInt();

    System.out.println("Factorial of " + n + " is " + factorial(n));

    scanner.close();

  }

  public static int factorial(int n) {

    if (n <= 1)

      return 1;

     else {

      return n * factorial(n - 1);

    }

  }

}

3.图书管理系统

图书管理系统是一个Java经典案例,它展示了如何将面向对象编程中的概念应用于实际应用程序中。该应用程序允许用户添加、查询和删除图书信息,以及展示其它一些基本的信息管理功能。

代码案例:


import java.util.*;

class Books{

String name,author,publisher;

int id;

Scanner s;

Books()

{

s=new Scanner(System.in);

System.out.println("Enter the id:");

id=s.nextInt();

System.out.println("Enter the name:");

name=s.next();

System.out.println("Enter the author:");

author=s.next();

System.out.println("Enter the publisher:");

publisher=s.next();

}

public String toString()

{

return(id+"\t"+name+"\t"+author+"\t"+publisher);

}

}

class Main

{

static Vector v=new Vector ();

public static void main(String args[])  //

{

Scanner s=new Scanner(System.in);

   System.out.println("\tMENU");

   System.out.println("-------------------------------");   

    while(true){

     System.out.println("Enter 1 to add book");

     System.out.println("Enter 2 to display book");

     System.out.println("Enter 3 to search for book");

     System.out.println("Enter 4 to delete book");

     System.out.println("Enter 5 to exit");

     System.out.println("-------------------------------");

     System.out.print("Enter Your Choice:");

     int a=s.nextInt();

     switch(a)

     {

     case 1:

     addbook();

     break;

     case 2:

     display();

     break;

     case 3:

     search();

     break;

     case 4:

     delete();

     break;

     case 5:

     System.exit(0);

     default:

     System.out.println("Invalid choice");

     break;

     }

    }

  }

public static void addbook()

{

v.add(new Books());

}

public static void display()

{

Iterator i=v.iterator();

System.out.println("ID\tName\tAuthor\tPublisher");

System.out.println("---------------------------------");

while(i.hasNext())

System.out.println(i.next());

}

public static void search()

{

int id,s=0;

    System.out.print("Enter ID to search:");

    Scanner sc=new Scanner(System.in);

    id=sc.nextInt();

    System.out.println("ID\tName\tAuthor\tPublisher");

    System.out.println("---------------------------------");

    Iterator i=v.iterator();

    while(i.hasNext())

    {

     Books b=i.next();

     if(id==b.id)

     {

       System.out.println(b);

       s=1;

     }

    }

    if(s==0)

     System.out.println("This Id is not available");

  }

public static void delete()

{

int id,d=0;

    System.out.print("Enter ID to delete:");

    Scanner sc=new Scanner(System.in);

    id=sc.nextInt();

    Iterator i=v.iterator();

    while(i.hasNext())

    {

     Books b=i.next();

     if(id==b.id)

     {

       d=1;

       v.remove(b);

       System.out.println("Removed Successfully");

       break;

     }

    }

    if(d==0)

     System.out.println("Invalid ID");

  }

}

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复