JAVA实验报告_实验3_类与对象_
- 格式:doc
- 大小:194.00 KB
- 文档页数:10
Java Object-Oriented Programming Experimental ReportSystem.out.println ("Your lucky numbers are " + lucky1 + ", " + lucky2+ ", and " + lucky3);}}3.2 Working with Strings(Choose to do)3.3 Rolling Dice3.4 Computing Distance3.5 formatting Output3.6 Playing with Cards(Choose to do)3.7 Experimenting with the Integer Class(Choose to do) 3.8 Nested Panels(1)宽度两倍(4)添加主界面宽度高度设定(5)添加MyPanels4.Experimental results and data processing5.Analysis and discussionScore: 6.Teacher ReviewsSignature:Date:Experimental Report List1Exercises1) Source code list3.3package test_java_03;import java.util.Random;public class point3_3 {public static void main(String[] args) {Random ger = newint lucky1, lucky2, lucky3;lucky1 = ger.nextInt(6)+1;lucky2 = (int)(ger.nextFloat()*6)+1;lucky3 = (int)(ger.nextFloat()*10)+20;System.out.print(lucky1+lucky2);}}3.4package test_java_03;import java.util.Scanner;public class point_3_4 {public static void main (String[] args){double x1, y1, x2, y2;double distance;Scanner scan = new Scanner(System.in);System.out.print ("Enter the coordinates of the first point " + "(put a space between them): ");x1 = scan.nextDouble();y1 = scan.nextDouble();System.out.print ("Enter the coordinates of the second point: ");x2 = scan.nextDouble();y2 = scan.nextDouble();doubley=Math.abs(y2-y1)*Math.abs(y2-y1),x=Math.abs(x2-x1)*Math.abs(x2-x1);double z=Math.sqrt(y+x);System.out.print("The distance between the two points is "+z);}}3.5package test_java_03;import java.util.*;import java.text.NumberFormat;import java.text.DecimalFormat;public class point_3_5 {public static void main (String[] args){final double OUNCES_PER_POUND = 16.0;double pricePerPound;double weightOunces;double weight;double totalPrice;Scanner scan = new Scanner(System.in);NumberFormat money=NumberFormat.getCurrencyInstance();DecimalFormat fmt=new DecimalFormat("0.#####");System.out.println ("Welcome to the CS Deli!!\n ");System.out.print ("Enter the price per pound of your item: ");pricePerPound = scan.nextDouble();System.out.print ("Enter the weight (ounces): ");weightOunces = scan.nextDouble();weight = weightOunces / OUNCES_PER_POUND;totalPrice = pricePerPound * weight;System.out.println("total price is:"+fmt.format(totalPrice));}}3.8(1)package test_java_03;import java.awt.*;import javax.swing.*;public class point_3_8 {public static void main (String[] args){JFrame frame = new JFrame ("Nested Panels");// Set up first subpanelJPanel subPanel1 = new JPanel();subPanel1.setPreferredSize (new Dimension(150, 100));subPanel1.setBackground (Color.green);JLabel label1 = new JLabel ("One");subPanel1.add (label1);// Set up second subpanelJPanel subPanel2 = new JPanel();subPanel2.setPreferredSize (new Dimension(150, 100));subPanel2.setBackground (Color.red);JLabel label2 = new JLabel ("Two");subPanel2.add (label2);JPanel subPanel3 = new JPanel();subPanel3.setPreferredSize (new Dimension(300, 100));subPanel3.setBackground (Color.black);JLabel label3 = new JLabel ("Three");subPanel3.add (label3);// Set up primary panelJPanel primary = new JPanel();primary.setBackground (Color.blue);primary.add (subPanel1);primary.add (subPanel2);primary.add (subPanel3);frame.getContentPane().add(primary);frame.pack();frame.setVisible(true);}}(4)package test_java_03;import java.awt.*;import javax.swing.*;public class point_3_8 {public static void main (String[] args){JFrame frame = new JFrame ("Nested Panels");// Set up first subpanelJPanel subPanel1 = new JPanel();subPanel1.setPreferredSize (new Dimension(150, 100));subPanel1.setBackground (Color.green);JLabel label1 = new JLabel ("One");subPanel1.add (label1);// Set up second subpanelJPanel subPanel2 = new JPanel();subPanel2.setPreferredSize (new Dimension(150, 100));subPanel2.setBackground (Color.red);JLabel label2 = new JLabel ("Two");subPanel2.add (label2);JPanel subPanel3 = new JPanel();subPanel3.setPreferredSize (new Dimension(300, 100));subPanel3.setBackground (Color.black);JLabel label3 = new JLabel ("Three");subPanel3.add (label3);// JPanel subPanel4 = new JPanel();// subPanel4.setPreferredSize(new Dimension(320,20));// subPanel4.setBackground(Color.orange);// JLabel label4 = new JLabel("My Panels");// subPanel4.add(label4);// Set up primary panelJPanel primary = new JPanel();primary.setBackground (Color.white);primary.setPreferredSize(new Dimension(320,260));//primary.add (subPanel4);primary.add (subPanel1);primary.add (subPanel2);primary.add (subPanel3);frame.getContentPane().add(primary);frame.pack();frame.setVisible(true);}}(5)package test_java_03;import java.awt.*;import javax.swing.*;public class point_3_8 {public static void main (String[] args){JFrame frame = new JFrame ("Nested Panels");// Set up first subpanelJPanel subPanel1 = new JPanel();subPanel1.setPreferredSize (new Dimension(150, 100));subPanel1.setBackground (Color.green);JLabel label1 = new JLabel ("One");subPanel1.add (label1);// Set up second subpanelJPanel subPanel2 = new JPanel();subPanel2.setPreferredSize (new Dimension(150, 100));subPanel2.setBackground (Color.red);JLabel label2 = new JLabel ("Two");subPanel2.add (label2);JPanel subPanel3 = new JPanel();subPanel3.setPreferredSize (new Dimension(300, 100));subPanel3.setBackground (Color.black);JLabel label3 = new JLabel ("Three");subPanel3.add (label3);JPanel subPanel4 = new JPanel();subPanel4.setPreferredSize(new Dimension(320,20));subPanel4.setBackground(Color.orange);JLabel label4 = new JLabel("My Panels");subPanel4.add(label4);// Set up primary panelJPanel primary = new JPanel();primary.setBackground (Color.white);primary.setPreferredSize(new Dimension(400,400));primary.add (subPanel4);primary.add (subPanel1);primary.add (subPanel2);primary.add (subPanel3);frame.getContentPane().add(primary);frame.pack();frame.setVisible(true);}}。