In-class experiment report of Java language programming in Wuhan University of Technology


Preface

The general content to be recorded in this article: The author has two experiments in class of the Java language programming course in the first semester of the sophomore year.
Development environment: Eclipse development environment


The following is the content of this article, the following cases are for reference

1. Class design and object usage

Problem description: Write an application to calculate the area of ​​trapezoids and circles.
Basic requirements: by defining two classes in the program to describe the attributes of the trapezoid and the circle and the method of finding the area respectively, master the definition method of the class; by defining the main class in the program, creating the trapezoid class and the circle class object calculation Their area, master the definition and usage of objects.

1. Import the library

package mianjji;
import java.util.Scanner;

2. Concrete realization

class Circle{
    
    
	 double pi=3.14;
	 double c;
	 double circle(double r) {
    
    
		c=pi*r*r;
		return c;
	}
}
class Trapezoid{
    
    
	 double s;
	 double trapezoid(double a,double b,double h) {
    
    
		 s=(a+b)*h/2;
		 return s;
	 }
}

public class mianjji {
    
    

	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		System.out .print("请输入你想要计算的类型(圆形输1,梯形输2):");
		Scanner scan=new Scanner(System.in);
		int i=scan.nextInt();
		if(i==1) {
    
    
		System.out.print("请输入半径:");
		double r=scan.nextDouble();
		Circle D= new Circle();
		System.out.println("圆的面积是:"+D.circle(r));
		}
		else {
    
    
		System.out.print("请分别输入上底,下底,高:");
		double a=scan.nextDouble();
		double b=scan.nextDouble();
		double h=scan.nextDouble();
		Trapezoid e=new Trapezoid();
		System.out.println("梯形的面积是:"+e.trapezoid(a,b,h));	
		}
	scan.close();
	}
}

3. Display of experimental results

(1) Calculate the circular area
Insert picture description here

(2) Calculate the area of ​​the trapezoid
Insert picture description here

2. Design of text box, text area and layout

Problem description: Conceive by yourself and write an application with the following layout. When you enter several numbers in the upper text area, the lower text box simultaneously sums the entered numbers and finds the average. That is, as the input changes, the text box continuously updates the sum and average.
Basic requirements: further familiarity with the structure and development process of the application; master the creation and use of arrays; master the use of text boxes and text areas; master the use of interfaces, master the event handling mechanism of basic components; master the use of layout classes .

1. Import the library

package helloworld;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import java.util.StringTokenizer;

2. Concrete realization

public class text {
    
    

	private JFrame frame;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
    
    
		EventQueue.invokeLater(new Runnable() {
    
    
			public void run() {
    
    
				try {
    
    
					text window = new text();
					window.frame.setVisible(true);
				} catch (Exception e) {
    
    
					e.printStackTrace();
				}
			}
		});
	}

	public text() {
    
    
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
    
    
		frame = new JFrame();
		frame.setTitle("\u548C\u4E0E\u5E73\u5747\u503C");
		frame.getContentPane().setFont(new Font("SimSun", Font.PLAIN, 12));
		frame.setBounds(100, 100, 777, 443);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JLabel lblNewLabel = new JLabel("\u548C\u503C");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 12));
		lblNewLabel.setBounds(112, 211, 58, 15);
		frame.getContentPane().add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("\u5E73\u5747\u503C");
		lblNewLabel_1.setBounds(112, 294, 58, 15);
		frame.getContentPane().add(lblNewLabel_1);
		
		textField = new JTextField();
		textField.addKeyListener(new KeyAdapter() {
    
    
			@Override
			public void keyPressed(KeyEvent e) {
    
    
				String s = textField.getText();
				StringTokenizer fenxi = new StringTokenizer(s,"s, ");
					int n = fenxi.countTokens();
					String a[]=new String[n];
					double sum=0;
					for(int i=0;i<n;i++)
					{
    
    
						String temp=fenxi.nextToken();
						a[i]=temp;
						 sum = sum + Double.parseDouble(a[i]);
					}
					
					double  average = sum/n;
					
					textField_1.setText(""+sum);
					textField_2.setText(""+average);
			}
		}
		);



		textField.setBounds(104, 28, 580, 160);
		frame.getContentPane().add(textField);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setBounds(212, 208, 472, 31);
		frame.getContentPane().add(textField_1);
		textField_1.setColumns(10);
		
		textField_2 = new JTextField();
		textField_2.setBounds(212, 291, 472, 31);
		frame.getContentPane().add(textField_2);
		textField_2.setColumns(10);
	}

}

The window builder is used to develop plug-ins.

3. Display of experimental results

Insert picture description here


to sum up

Original experiment report Baidu SkyDrive link
link: Chong Chong Chong ~
Extraction code: gpke
copy this content and open the Baidu SkyDrive mobile app, the operation is more convenient, please
take it away.

Guess you like

Origin blog.csdn.net/mo_zhe/article/details/112433222