南昌大学Java实验报告(1).doc_第1页
南昌大学Java实验报告(1).doc_第2页
南昌大学Java实验报告(1).doc_第3页
南昌大学Java实验报告(1).doc_第4页
南昌大学Java实验报告(1).doc_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

南昌大学实验报告学生姓名: 学 号: 专业班级: 实训类型: 验证 综合 设计 创新 实验日期:2017.11.1 实验成绩:一、 实验项目名称Java开发环境搭建和Java语言基础二、 实验的评分标准实验分为AF,A为最高,F最低。F:在规定时间内没有完成所有的实验,而且没有及时提交实验报告,或者实验过程中出现了抄袭复制他人实验代码。D:能完成实验,但是实验结果出现严重错误,不能体现对教学内容的理解。C:能基本完成实验,实验结果基本正确。但是实验内容有较少的错误,提交的实验代码质量一般。B:能较好的完成实验,实验报告条理清楚,实验代码结构清晰,代码质量较高,及时更正试验中出现的错误,并对运行中一些异常错误进行分析,解释错误产生的原因。A:能较好的完成实验,实验代码质量高,实验报告完成度高,能在实验完成的基础上,根据个人的理解增加实验的新功能,具有一定的创新能力。三、 实验目的和要求1. 掌握Java的基础知识2. 掌握和运用Java的控制语句和数组3. 使用Eclipse平台开发Java应用四、 实验内容1. HelloWorld的应用程序和Applet程序。A. javap工具解析HelloWord字节码的类B.javap反汇编HelloWorld字节码C.修改代码,使之成为应用程序和Applet小程序两种程序的“入口”不同,一个是main()函数,一个是init()函数,所以直接添加代码即可,运行时,根据IDE的run功能,选择相应的程序执行即可。 第一个和最后一个分别是Applet程序和Application程序 D.写文档注释,生成文件一些效果截图2. 打印日历程序 (长图)部分截图3. 枚举类统计Java成绩非法输入程序会报错退出正确结果自学了一下javafx,写了个简陋的UI。(要配合控制台输入数据,有待改进)4. 四连子游戏继续自学了一下javafx,给这个游戏包装了一个UI。本题和老师理解的要求可能有出入,游戏规则有点差别,但是底层逻辑是差不多的,于是我还是按照自己的想法写了下去,希望老师理解。 某一列下满,按钮失效获胜,信息提示绿色指示当前局该哪一方落子平局Restart:清盘重玩Quit:退出实验源代码如下:-1.helloworld-package one;import java.applet.Applet;import java.awt.*;/* * This class is used to show hello world information * in two kinds of ways * * 1.print the information in the console * 2.generate an applet to display the info * * author Yuchen Tian */public class HelloWorld extends Applet /* * main function for Application * param args */ public static void main(String args)/for application System.out.println(Hello World!); public void init() /for applet /* * create an graph on applet to show info * param g */ public void paint(Graphics g) g.drawString(Hello World!,20,20); -2.printCalendar-Part1-package two;/*this Month enum defines the 12 months of all yearincluding the information of: index:the order in current class name:the full name of the month day:days in this month */public enum Month JAN(1,January,30),FEB(2,February,28),MAR(3,March,31),APR(4,April,30), MAY(5,May,31),JUN(6,June,30), JUL(7,July,31),AUG(8,August,31), SEP(9,September,30),OCT(10,October,31),NOV(11,November,30),DEC(12,December,31); private int index; private String name; private int day; Month(int index,String name,int day) this.index = index; = name; this.day = day; public String getName() return name; public int getDay() return day; /when a month call this method,it becomes the month following it public Month next() int index = (this.index+1 12) ? 1 : this.index+1; return getMonthByIndex(index); /get a month according to the its index in the enum public Month getMonthByIndex(int index) for(Month c:Month.values() if(c.index = index) return c; return null; -part2-package two;import java.util.Calendar;import java.util.GregorianCalendar;public class MyCalendar private int year; private int startDay; private Month m; public MyCalendar(int year) this.year = year; this.startDay = getFirstDay(0,1) % 7;/month1,day1 m = Month.JAN; /start month of the year public int getFirstDay(int month, int day) GregorianCalendar tool = new GregorianCalendar(this.year,month,day); return tool.get(Calendar.DAY_OF_WEEK);/0 for Sun ,1 for Mon. public void printMonth(Month m) /generate the head information of each month System.out.printf(%20sn,m.getName(); System.out.println(-); System.out.println(Su Mo Tu We Th Fr St); int count = 0; for(int i=0;istartDay;+i)/print blanks System.out.printf(%3c, ); count+; for(int i=1;i=getDays(m);+i)/print days System.out.printf(%2d ,i); count+; if(count%7=0) System.out.println(); System.out.println(); startDay = (getDays(m) + startDay) % 7; /change the startDay for the next month public boolean isLeapYear() return year%400 = 0 | (year%4 = 0 & year%100 !=0); /get days in specific month /days in February is unique when it is leap year /hence,a special approach is implement to alter it right public int getDays(Month m) int days = m.getDay(); if(isLeapYear() & m.getName().equals(Month.FEB.getName()/special for February days += 1; return days; /once printed the first month /change the m to the next month public void printAllMonth() for(int i = 0;i=90 & grade=100) g = GradeRank.A; else if(grade=80) g = GradeRank.B; else if(grade=70) g = GradeRank.C; else if(grade=60) g = GradeRank.D; else if(grade=0) g = GradeRank.E; else g = null; return g; -part2-package three;import java.util.Scanner;public class EvaluateGrade private int collection; private GradeRank g; /the standard for Aggregating private int num; /how many scores you need to calculate public EvaluateGrade(int num) this.collection = new int5;/n different ranks g = GradeRank.A; this.num = num; /when a new score is input /g is gonna to change according to the value of the score public void checkRank(Double grade) g = g.changeRank(grade); if(g=null) System.out.println(Invalid input!); System.exit(1); collectiong.getIndex()+; public void countRank() Scanner input = new Scanner(System.in); System.out.println(Input the scores:); for(int i = 0;inum;i+) Double grade = input.nextDouble(); checkRank(grade); public void display() for(int i=0;i5;i+) System.out.printf(%s%c%5.1f%cn,indexToStr(i),:,getPercent(i),%); /acquire the information using index /to display the outcome better public String indexToStr(int i) for(GradeRank c : GradeRank.values() if(c.getIndex()=i) return c.getInfo(); return null; public double getPercent(int i) return (double) collectioni/num)*100; -part3-package three;import java.util.Scanner;public class Aggregate public static void main(String args) Scanner number = new Scanner(System.in); System.out.print(Please input the number of scores you want to deal with:); int n = number.nextInt(); EvaluateGrade checker = new EvaluateGrade(n); checker.countRank(); checker.display(); -4,chessgame-part1-/*this class is designed to store data in playing gamesand make the rule of the playing as well */public class Game private int board; private int full; private boolean isBlack; public Game() this.board = new int7; for(int i=0;i7;i+) this.boardi = new int6; this.isBlack = true;/black side is first full = new int7; for(int i=0;i7;i+) fulli = 5; public boolean isBlack() return isBlack; /change to the other side to play public void inTurn() this.isBlack = !this.isBlack; public boolean isFull(int i) return fulli = -1; public boolean isAllFull() for(int i=0;i7;+i) if(!isFull(i) return false; return true; /the most important part /check whether one side has won the game public boolean checkWin(int i,int j) if(line(i,j)|column(i,j)|leftR(i,j)|rightL(i,j) return true; else return false; public boolean line(int i,int j) int cx = 0; for(int m =i+1;m=0 & boardmj=boardij;-m) cx+; if(cx=3) return true; else return false; public boolean column(int i,int j) int cx = 0; for(int n =j+1;n=0 & boardin=boardij;-n) cx+; if(cx=3) return true; else return false; public boolean leftR(int i,int j) int cx = 0; for(int m=i+1,n=j+1;m7 & n=0 & n=0 & boardmn=boardij;-m,-n) cx+; if(cx=3) return true; else return false; public boolean rightL(int i,int j) int cx = 0; for(int m=i-1,n=j+1;m=0 & n6 & boardmn=boardij;-m,+n) cx+; for(int m=i+1,n=j-1;m=0 & boardmn=boardij;+m,-n) cx+; if(cx=3) return true; else return false; /change the data of game when one side drop the chess public int dropChess(int i) boardifulli = (isBlack() ? 1 : 2; inTurn(); fulli-; return fulli+1; -part2-import javafx.geometry.Insets;import javafx.scene.control.Button;import javafx.scene.control.TextField;import javafx.scene.layout.BorderPane;import javafx.scene.layout.GridPane;import javafx.scene.paint.Color;import javafx.scene.shape.*;import javafx.scene.text.*;/*this class is designed to append graph of each element into the game determined the layout of these elements as wellmore importantly, the change of the elements as the game playing */public class ChessBoard private Game rule; /the elements private Font font; private Rectangle recs; private TextField result; private Button bts; private Button indicator; private Button choice; private Circle light; private Circle chess; /the framework private GridPane board; private BorderPane frame; private GridPane control; /initialize all the elements ChessBoard() this.rule = new Game(); this.font = Font.font(null, FontWeight.NORMAL, null, 20); this.recs = new Rectangle42; this.result = new TextField(); this.bts = new Button7; this.indicator = new Button2; this.choice = new Button2; this.light = new Circle2; this.chess = new Circle42; this.board = new GridPane(); this.frame = new BorderPane(); this.control = new GridPane(); this.result = new TextField(); /set all the properties of the elements public void init() for (int i = 0; i 42; i+) recsi = new Rectangle(60, 60); recsi.setFill(Color.gray(0.36); chessi = new Circle(25); chessi.setFill(Color.gray(0.36); for (int i = 0; i 7; i+) btsi = new Button(Drop); btsi.setPrefSize(60, 40); for (int i = 0; i 2; i+) lighti = new Circle(16); lighti.setFill(Color.rgb(0, 230, 0, 1);/control capacity choice0 = new Button(Restart); choice1 = new Button(Quit); indicator0 = new Button(Black); indicator1 = new Button(White); for (int i = 0; i 2; i+) indicatori.setFont(font); indicatori.setPrefSize(100, 40); indicatori.setMouseTransparent(true); choicei.setPrefSize(150, 40); choicei.setFont(font); result.setFont(font); result.setPrefSize(180,50); result.setEditable(false); /add all the elements into the main frame /set the layout between them public void load() board.setHgap(1); control.setVgap(10); /fill the chess board for (int i = 0; i 42; +i) board.add(recsi, i % 7, (i / 7) + 1); board.add(chessi,i % 7, (i / 7) + 1); GridPane.setMargin(chessi, new Insets(0, 0, 0, 5); /for control part for (int i = 0; i 2; i+) GridPane.setMargin(lighti, new Insets(0, 20, 0, 20); GridPane.setMargin(choicei, new Insets(20, 0, 0, 0); control.add(choicei, 0, i + 10, 2, 1); control.add(indicatori, 0, i+1); control.add(lighti, 1, i+1); control.add(result,0,3,2,1); GridPane.setMargin(result,new Insets(20, 20, 0, 0); for (int i = 0; i 7; i+) board.add(btsi, i, 0); frame.setCenter(board); frame.setRight(control); public void clearBoard() for(int i = 0;i42;+i) chessi.setFill(Color.gray(0.36); this.rule = new Game(); result.clear(); for(int i = 0;i7;+i) getButton(i).setOpacity(1); public int addChess(int i) int pos = rule.dropChess(i);/(i,pos) is the right point if (!rule.isBlack() chesspos*7+i.setFill(Color.BLACK); else chesspos*7+i.setFill(Color.WHITE); return pos; public void setBtsOff(int tag) if(tag0) for(int i = 0;i7;+i) getButton(i).setOpacity(0); else getButton(tag).setOpacity(0); public void changeLight() if (rule.isBlack() light0.setFill(Color.rgb(0, 230, 0, 1); light1.setFill(Color.rgb(0, 230, 0, 0); else light0.setFill(Color.rgb(0, 230, 0, 0); light1.setFill(Color.rgb(0, 230, 0, 1); public boolean isWin(int i,int j) return rule.checkWin(i,j); public BorderPane getFrame() return frame; public Button getButton(int i) return btsi; public boolean isFull(int i) return rule.isFull(i); public Button getChoice(int i) return choicei; public void printInfo(int i,int j) if(isWin(i,j) if(rule.isBlack() result.setText(White side win!); else result.setText(Black side win!); if(rule.isAllFull() result.setText(Draw!); -part3-import javafx.application.Application;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.GridPane;import javafx.stage.Stage;/*This class is designed to organize the changes of the game from the last lever logicallyWhats more,add events to certain circumstances, enabling the game run more fluently */public class ControlGame extends Application private ChessBoard chessBoard = new ChessBoard(); private boolean lock = false; public void start(Stage stage) chessBoard.init(); chessBoard.load(); chessBoard.changeLight(); addInfoEvents(); addQuitEvent(); addRestartEvent(); Scene scene =

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论