一、实验一
1. 用穷举法解决以下问题:36块砖,36人搬,男搬4,女搬3,两个小孩抬1砖。要求一次全搬完,问男、女、小孩有多少?要在程序中输出你的姓名-班级-学号-程序运行的日期等信息。
package carrybricks;
public class CarryBricks {
public static void main(String[] args) {
int x, y, z;
for (x = 0; x <= 9; x++) {
for (y = 0; y <= 12; y++) {
for (z = 0; z <= 36; z++) {
if (z % 2 == 0) {
if (4 * x + 3 * y + z / 2 == 36 && x + y + z == 36) {
System.out.println("x=" + x + "; y=" + y + "; z="+ z);
}
}
}
}
}
System.out.println("Py小杨-大数据2002-2021.10.28");
}
}
2. 打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。要在程序中输出你的姓名-班级-学号-程序运行的日期等信息。
提示:利用for循环控制100-999个数,每个数分解出个位、十位、百位。
package numberprint;
public class NumberPrint {
public static void main(String[] args) {
int ge,shi,bai;
for(int i = 100;i <= 999;i++){
bai = i / 100;
shi = i % 100 / 10 ;
ge = i % 10 ;
if (i == bai*bai*bai + shi*shi*shi + ge*ge*ge ){
System.out.println(i+" 为水仙花数");
}
}
System.out.println("Py小杨-大数据2002-2021.10.28");
}
}
二、实验二
1. 设计股票类Stock,包括股票名称name,股票代码symbol,股票前一天的价值previousClosingPrice,当前价值currentPrice,股票持有人holder等属性。要求所有属性私有,程序运行后显示出所有的股票属性,并显示股票涨跌幅度。其中holder属性赋值为你自己的姓名。同时在程序中输出你的姓名-班级-学号-程序运行的日期。
package stock;
public class Stock {
private String symbol;
private String name;
private double previousClosingPrice = 1900;
private double currentPrice = 1944;
private String holder = "Py小杨";
public Stock( String symbol , String name){
this.name = name;
this.symbol = symbol;
}
public double getChangePercent(){
return currentPrice / previousClosingPrice;
}
public static void main(String[] args) {
Stock stock = new Stock("600519","贵州茅台");
System.out.println("股票代码:" + stock.symbol);
System.out.println("股票名称:" + stock.name);
System.out.println("股票涨幅:" + stock.getChangePercent());
System.out.println("Py小杨-大数据2002-2021.11.10");
}
}
2. 创建一个类MyFavor用于描述你个人喜好的一类东西,必须包含至少三个属性用于描述清楚该爱好,至少两个方法和至少两个构造器,同时写测试类,测试该类中的所有构造器、方法和属性。同时在程序中输出你的姓名-班级-学号-程序运行的日期。
package myfavor;
/*MyFavour.java*/
public class MyFavor {
private String name;
private String country;
private String masterpiece;
public MyFavor(String name,String country,String work){
this.name = name;
this.country = country;
this.masterpiece = masterpiece;
}
public MyFavor() {
}
public void show(){
System.out.println("名字:" + name + "国家:" + country);
}
public void work(){
System.out.println("著作:" + masterpiece);
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getcountry(){
return country;
}
public void setCountry(String country){
this.country = country;
}
public String getMasterpiece(){
return masterpiece;
}
public void setMasterpiece(String masterpiece){
this.masterpiece = masterpiece;
}
}
package myfavor;
/*MyFavourTest.java*/
public class MyFavorTest{
public static void main(String[] args){
MyFavor m = new MyFavor();
m.setName("本杰明 富兰克林");
m.setCountry("美国");
m.setMasterpiece("富兰克林自传");
m.show();
m.work();
System.out.println("Py小杨-大数据2002-2021.11.10");
}
}
三、实验三
1. 创建一个Point类,包含坐标x、y,点的名字name。然后创建一个Line类,定义两个读写属性start和end,数据类型为Point,定义Line类方法(计算start和end之间的距离,并打印start和end坐标以及距离信息)。同时在程序中输出你的姓名-班级-学号-程序运行的日期。
package point;
//类point
public class Point {
float x;
float y;
public Point(float x, float y){
this.x = x;
this.y = y;
}
}
package point;
// 类 Line
public class Line {
Point start, end;
public Point getStart() {
return start;
}
public void setStart(Point start) {
this.start = start;
}
public Point getEnd() {
return end;
}
public void setEnd(Point end) {
this.end = end;
}
public double distance() {
double d = Math.sqrt((this.getStart().x - this.getEnd().x)
* (this.getStart().x - this.getEnd().x)
+ (this.getStart().y - this.getEnd().y)
* (this.getStart().y - this.getEnd().y));
return d;
}
public void print(){
System.out.println("Start.x = " + this.getStart().x + "; Start.y = " + this.getStart().y);
System.out.println("End.x = " + this.getEnd().x + "; End.y = " + this.getEnd().y);
System.out.println("The distance is " + this.distance());
}
public static void main(String[] arg){
Line line = new Line();
line.setStart(new Point(20, 20));
line.setEnd(new Point(25, 25));
line.print();
System.out.println("Py小杨-大数据2002-2021.12.3");
}
}
2. 设计Student类,包括属性“学号”、“姓名”和3门课程math、english和java的成绩,包括的方法有计算3门课程的“总分”、“平均分”、“最高分”及“最低分”。创建班级中期末考试成绩单。同时在程序中输出你的姓名-班级-学号-程序运行的日期。
package student;
public class Student {
private String stuno;
private String name;
private float math;
private float english;
private float java;
public Student(String stuno, String name, float math, float english, float java) {
this.setStuno(stuno);
this.setName(name);
this.setMath(math);
this.setEnglish(english);
this.setJava(java);
}
public void setStuno(String s) {
stuno = s;
}
public void setName(String n) {
name = n;
}
public void setMath(float m) {
math = m;
}
public void setEnglish(float e) {
english = e;
}
public void setJava(float j) {
java = j;
}
public String getStuno() {
return stuno;
}
public String getName() {
return name;
}
public float getMath() {
return math;
}
public float getEnglish() {
return english;
}
public float getJava() {
return java;
}
public float sum() {
return math + english + java;
}
public float avg() {
return this.sum() / 3;
}
public float max() {
float max = math;
max = max > java ? max : java;
max = max > english ? max : english;
return max;
}
public float min() {
float min = math;
min = min < java ? min : java;
min = min < english ? min : english;
return min;
}
public static void main(String[] args) {
System.out.println("Py小杨-大数据2002-2021.12.3");
Student student1 = new Student("20200001", "雷杰多", 97,98,99);
Student student2 = new Student("20200002", "白迪迦", 96,97,98);
Student student3 = new Student("20200003", "阿斯特", 95,96,97);
Student student4 = new Student("20200004", "奥特王", 94,95,96);
Student student5 = new Student("20200005", "银河奥", 93,94,95);
Student student6 = new Student("20200006", "诺亚奥", 92,93,94);
Student student7 = new Student("20200007", "光辉赛", 91,92,93);
Student student8 = new Student("20200008", "贝利亚", 90,91,92);
Student student9 = new Student("20200009", "暗迪迦", 89,90,91);
Student[] students = new Student[10];
students[0]=student1;students[1]=student2;students[2]=student3;
students[3]=student4;students[4]=student5;students[5]=student6;
students[6]=student7;students[7]=student8;students[8]=student9;
for(int i = 0;i<10;i++){
System.out.print("学号:" + students[i].getStuno()+"\t");
System.out.print("学生姓名:" + students[i].getName()+"\t");
System.out.print("数学成绩:" + students[i].getMath()+"\t");
System.out.print("英语成绩:" + students[i].getEnglish()+"\t");
System.out.print("java成绩:" + students[i].getJava()+"\t");
System.out.print("平均分:" + students[i].avg()+"\t");
System.out.print("最高:" + students[i].max()+"\t");
System.out.println(" ");
}
}
}
四、实验四
利用JavaFx创建动态时钟程序。
//ClockPane
package displayclock;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;
public class ClockPane extends Pane{
private int hour;
private int minute;
private int second;
//创建一个带有当前时间的系统默认时钟
public ClockPane(){
setCurrentTime();
}
//创建一个带有详细小时,分钟,秒的时钟
public ClockPane(int hour,int minute,int second){
this.hour = hour;
this.minute = minute;
this.second = second;
}
//返回小时
public int getHour(){
return hour;
}
//设置一个新的小时
public void setHour(int hour){
this.hour = hour;
paintClock();
}
//返回分钟
public int getMinute(){
return minute;
}
//设置一个新的分钟
public void setMinute(){
this.minute = minute;
paintClock();
}
//返回秒
public int getSecond(){
return second;
}
//设置一个新的秒
public void setSecond(){
this.second = second;
paintClock();
}
//为时钟设置当前时间
public void setCurrentTime(){
//为当前日期和时间创造日历
Calendar calendar = new GregorianCalendar();
//创建当前的小时,分钟,秒
this.hour = calendar.get(Calendar.HOUR_OF_DAY);
this.minute = calendar.get(Calendar.MINUTE);
this.second = calendar.get(Calendar.SECOND);
paintClock();//重新绘画时钟
}
//绘画时钟
private void paintClock(){
//初始化时钟参数
double clockRadius = Math.min(getWidth(), getHeight())*0.9*0.5;
double centerX = getWidth()/2;
double centerY = getHeight()/2;
//绘制圆
Circle circle = new Circle(centerX,centerY,clockRadius);
circle.setFill(Color.WHITE);
circle.setStroke(Color.BLACK);
Text t1 = new Text(centerX - 5,centerY - clockRadius +12,"12");
Text t2 = new Text(centerX - clockRadius +3,centerY +5,"9");
Text t3 = new Text(centerX + clockRadius -10,centerY +3 ,"3");
Text t4 = new Text(centerX - 3,centerY + clockRadius - 3,"6");
//绘制秒针
double sLength = clockRadius * 0.8;
double secondX = centerX + sLength* Math.sin(second*(2*Math.PI/60));
double secondY = centerY - sLength* Math.cos(second*(2*Math.PI/60));
Line sLine = new Line(centerX,centerY,secondX,secondY);
sLine.setStroke(Color.RED);
//绘制分针
double mLength = clockRadius * 0.65;
double minuteX = centerX + mLength* Math.sin(minute*(2*Math.PI/60));
double minuteY = centerY - mLength* Math.cos(minute*(2*Math.PI/60));
Line mLine = new Line(centerX,centerY,minuteX,minuteY);
mLine.setStroke(Color.BLUE);
//绘制时针
double hLength = clockRadius * 0.65;
double hourX = centerX + hLength* Math.sin(hour*(2*Math.PI/60));
double hourY = centerY - hLength* Math.cos(hour*(2*Math.PI/60));
Line hLine = new Line(centerX,centerY,hourX,hourY);
hLine.setStroke(Color.GREEN);
getChildren().clear();
getChildren().addAll(circle,t1,t2,t3,t4,sLine,mLine,hLine);
}
public void setWidth(double width){
super.setWidth(width);
paintClock();
}
public void setHeight(double height){
super.setHeight(height);
paintClock();
}
}
package displayclock;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
public class DisplayClock extends Application {
public void start(Stage primaryStage) {
ClockPane clock = new ClockPane();
String timeString = clock.getHour() + ":" + clock.getMinute()
+ ":" + clock.getSecond();
Label lblCurrentTime = new Label(timeString);
BorderPane pane = new BorderPane();
pane.setCenter(clock);
pane.setBottom(lblCurrentTime);
BorderPane.setAlignment(lblCurrentTime, Pos.TOP_CENTER);
//创建一个场景放置在窗口中
Scene scene = new Scene(pane,250,250);
primaryStage.setTitle("Py小杨-大数据2002");//创建场景的标题
primaryStage.setScene(scene);//把场景放置在窗口中
primaryStage.show();//展示窗口
}
public static void main(String[] args){
launch(args);
System.out.println("Py小杨-大数据2002-2021.12.9");
}
}
结果: