当前位置:文档之家› JAVA实验报告_实验7_数组_

JAVA实验报告_实验7_数组_

Java Object-Oriented Programming Experimental Report3.2 Grading Quizzes (Choose to do)3.3 Reversing an Array(Choose to do)3.4 Adding To and Removing From an Integer List(Choose to do) 3.5 A Shopping Cart (Choose to do)3.6 Averaging Numbers (Choose to do)3.7 Exploring Variable Length Parameter Lists3.8 Magic Squares(Choose to do)3.9 A Shopping Cart Using the ArrayList Class (Choose to do) 3.10 A Polygon Person(Choose to do)3.11 An Array of Radio Buttons\3.12 Drawing Circles with Mouse Clicks3.13 Moving Circles with the Mouse(Choose to do)3.14 Moving a Stick Figure(Choose to do)4.Experimental results and data processing5.Analysis and discussionScore: 6.Teacher ReviewsSignature:Date:Experimental Report List1 Tracking Saleepackage test_java_07_01;import java.util.Scanner;import java.util.Arrays;public class test_01{public static void main(String[] args){final int SALESPEOPLE = 5;int[] sales = new int[SALESPEOPLE];Scanner scan = new Scanner(System.in);for (int i=0; i<sales.length; i++){System.out.print("Enter sales for salesperson " + i + ": ");sales[i] = scan.nextInt();}int sum,max=0,maxi=0,min=sales[0],mini=0;for(int i=1;i < sales.length;i++){if(sales[i]<min){min=sales[i];mini=i;}}System.out.println("\nSalesperson Sales");System.out.println("--------------------");sum = 0;for (int i=0; i<sales.length; i++){System.out.println(" " + i + " " + sales[i]);if(sales[i]>max){max=sales[i];maxi=i;}sum += sales[i];}float avg=sum/5;System.out.println("\nTotal sales: " + sum);System.out.println("Salesperson "+ (maxi+1) +" had the highest sale with" + max);System.out.println("Salesperson "+ (mini+1) +" had the leatst sale with " + min);System.out.println("put in the sale");int sal=scan.nextInt(),summer=0;for(int i=0;i<sales.length;i++){if(sales[i]>=sal){System.out.println("saleman "+(i+1)+" expend this sale,he has "+sales[i]);summer++;}}System.out.println("the salesmen have "+summer+" people can acquire this goal");}}7 Exploring Variable Length Parameter Listspackage test_java_07_07;import java.util.Scanner;public class test_01{//-----------------------------------------------// Calls the average and minimum methods with// different numbers of parameters.//-----------------------------------------------public static void main(String[] args){double mean1, mean2,mean3, mean4;int [] number=new int[20];Scanner scan =new Scanner(System.in);mean1 = average(42, 69, 37);mean2 = average(35, 43, 93, 23, 40, 21, 75);mean3 = average(13);mean4 = average();for(int i =0;i<number.length;i++){number[i]=scan.nextInt();}double avg=average(number);int min=minnumber(number);System.out.println ("mean1 = " + mean1); System.out.println ("mean2 = " + mean2); System.out.println ("mean3 = " + mean3); System.out.println ("mean4 = " + mean4); System.out.println ("avg = " + avg);System.out.println ("min = " + min);}public static double average (int ... list) {double result = 0.0;if (list.length != 0){int sum = 0;for (int num: list)sum += num;result = (double)sum / list.length; }return result;}public static int minnumber(int ...list){int min=list[0];for(int i=1 ;i < list.length;i++){if(list[i]<=min){min=list[i];}}return min;}}11 An Array of Radio Buttons(1)package test_java_07_11;import javax.swing.*;public class test_01{// ------------------------------------------------------------- // Creates and presents the frame for the color change panel.// ------------------------------------------------------------- public static void main (String[] args){JFrame colorFrame = new JFrame ("Color Options");colorFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);test_02 panel = new test_02();colorFrame.getContentPane().add (panel);colorFrame.pack();colorFrame.setVisible(true);}}(2)package test_java_07_11;import javax.swing.*;import java.awt.*;import java.awt.event.*;public class test_02 extends JPanel{private final int WIDTH = 350, HEIGHT = 100, FONT_SIZE = 20;private final int NUM_COLORS = 5;private Color [] color = new Color[NUM_COLORS];private JLabel heading;private JRadioButton [] colorButton ;// ------------------------------------------------------------------// Sets up a panel with a label at the top and a set of radio buttons // that control the background color of the panel.// ------------------------------------------------------------------public test_02 (){color[0] = Color.yellow;color[1] = Color.cyan;color[2] = Color.red;color[3] = Color.green;color[4] = Color.magenta;heading = new JLabel ("Choose the background color!");heading.setFont (new Font ("Helvetica", Font.BOLD, FONT_SIZE)); //刚开始add (heading);setBackground (Color.WHITE);colorButton = new JRadioButton[NUM_COLORS]; //创建选框colorButton[0] =new JRadioButton("yellow");colorButton[1] =new JRadioButton("cyan");colorButton[2] =new JRadioButton("red");colorButton[3] =new JRadioButton("green");colorButton[4] =new JRadioButton("magenta");ButtonGroup group = new ButtonGroup(); //为选框分组group.add(colorButton[0]);group.add(colorButton[1]);group.add(colorButton[2]);group.add(colorButton[3]);group.add(colorButton[4]);colorButton[0].setSelected(true);ColorListener linster =new ColorListener(); //为选框添加监听器for(int i = 0;i < colorButton.length;i++) //把选框添加到面板{colorButton[i].addActionListener(linster);add(colorButton[i]);}setPreferredSize (new Dimension (WIDTH, HEIGHT));}private class ColorListener implements ActionListener{// Updates the background color of the panel based on// which radio button is selected.private test_02 test;public void actionPerformed (ActionEvent event){if(colorButton != null){for(int i = 0;i < colorButton.length; i++){if(colorButton[i].isSelected()){//test =new test_02();setBackground(color[i]);}}}}}}12 Drawing Circles with Mouse Clicks(1)package test_java_07_12;import javax.swing.JFrame;public class test_01{//----------------------------------------------------------------- // Creates and displays the application frame.//----------------------------------------------------------------- public static void main (String[] args){JFrame circlesFrame = new JFrame ("Circles");circlesFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);circlesFrame.getContentPane().add (new test_03_JFrame());circlesFrame.pack();circlesFrame.setVisible(true);}}(2)package test_java_07_12;import java.awt.*;import java.util.Random;public class test_02{private int centerX, centerY;private int radius;private Color color;static Random generator = new Random();//---------------------------------------------------------// Creates a circle with center at point given, random radius and color// -- radius 25..74// -- color RGB value 0..16777215 (24-bit)//---------------------------------------------------------public test_02(Point point){radius = Math.abs(generator.nextInt())%50 + 25;color = new Color(Math.abs(generator.nextInt())% 16777216);centerX = point.x;centerY = point.y;}//---------------------------------------------------------// Draws circle on the graphics object given//---------------------------------------------------------public void draw(Graphics page){page.setColor(color);page.fillOval(centerX-radius,centerY-radius,radius*2,radius*2);}public void drawwhite(Graphics page){page.setColor(Color.WHITE);page.fillOval(centerX-radius,centerY-radius,radius*2,radius*2);}public void move(Point p){centerX = p.x;centerY = p.y;}public boolean isInside(Point p){if(Math.sqrt((p.x-centerX)*(p.x-centerX)+(p.y-centerY)*(p.y-centerY))>radi us){return false;}else{return true;}}}(3)package test_java_07_12;import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.util.*;public class test_03_JFrame extends JPanel{private final int WIDTH = 600, HEIGHT = 400;private test_02 circle;//-----------------------------------------------------------------// Sets up this panel to listen for mouse events.//-----------------------------------------------------------------public test_03_JFrame(){addMouseListener (new CirclesListener());setPreferredSize (new Dimension(WIDTH, HEIGHT));}//-----------------------------------------------------------------// Draws the current circle, if any.//-----------------------------------------------------------------public void paintComponent (Graphics page){super.paintComponent(page);if (circle != null)circle.draw(page);}//***************************************************************** // Represents the listener for mouse events.//***************************************************************** private class CirclesListener implements MouseListener{//--------------------------------------------------------------// Creates a new circle at the current location whenever the// mouse button is pressed and repaints.//--------------------------------------------------------------public void mousePressed (MouseEvent event){if (circle == null){circle = new test_02(event.getPoint());}else{if(circle.isInside(event.getPoint())){circle=null;}else{circle = new test_02(event.getPoint());circle.move(event.getPoint());}}repaint();}//-----------------------------------------------------------------// Provide empty definitions for unused event methods.//-----------------------------------------------------------------public void mouseClicked (MouseEvent event) {circle = new test_02(event.getPoint());circle.move(event.getPoint());}public void mouseReleased (MouseEvent event) {}public void mouseEntered (MouseEvent event) {setBackground(Color.WHITE);;}public void mouseExited (MouseEvent event) {setBackground(Color.blue);}}}。

相关主题