「JAVA学期作业」Java作业
本篇文章给大家谈谈JAVA学期作业,以及Java作业对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
java简单作业题
public class MyDate {
private int year ;
private int month ;
private int day ;
public MyDate(){}
public MyDate(int year, int month, int day) {
super();
this.year = year;
this.month = month;
this.day = day;
}
public String toString() {
return "MyDate =="+year+"-"+month+"-"+day;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
}
public class MyTime {
public static void main(String[] args) {
MyTime time = new MyTime(14,53,20);
System.out.println(time.toString());
}
private int hour;
private int minute;
private int second;
public MyTime() {
}
public MyTime(int hour, int minute, int second) {
super();
this.hour = hour;
this.minute = minute;
this.second = second;
}
public String toString() {
return "current time=="+hour + ":" + minute + ":" + second;
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
public int getMinute() {
return minute;
}
public void setMinute(int minute) {
this.minute = minute;
}
public int getSecond() {
return second;
}
public void setSecond(int second) {
this.second = second;
}
}
public class FullTime {
public static void main(String[] args) {
MyDate myDate = new MyDate(2007, 10, 2);
MyTime myTime = new MyTime(14,17,35);
FullTime fullTime = new FullTime(myDate,myTime);
System.out.println(fullTime);
}
private MyDate myDate;
private MyTime myTime;
public FullTime(MyDate myDate, MyTime myTime) {
super();
this.myDate = myDate;
this.myTime = myTime;
}
public String toString() {
String text = myDate.getYear() + "年" + myDate.getMonth() + "月"
+ myDate.getDay() + "日" + myTime.getHour() + "时"
+ myTime.getMinute() + "分" + myTime.getSecond() + "秒";
return text;
}
public MyDate getMyDate() {
return myDate;
}
public void setMyDate(MyDate myDate) {
this.myDate = myDate;
}
public MyTime getMyTime() {
return myTime;
}
public void setMyTime(MyTime myTime) {
this.myTime = myTime;
}
}
第4题,你自己想办法吧。主要知识点:
1、继承
2、super和final,这个只是表面的东西,说到底还是java中overrides(重写)的要求
3、通过多层间接的继承,你要知道的是 对象被实例化的顺序。
java作业
这个要求很明显了,使用class关键字创建类,同时使用集合类ArrayList来创建对象,并添加元素,然后输出,输出可以直接输出集合对象,可以使用forEach循环输出,可以使用迭代器Iterator来输出等,只要注意集合是没有元素类型,所有输出的都是Object类型的数据,下面的例子仅作为参考:
import java.util.*;
//创建一个图书Book类
class Book
{
String s;
public Book(String s){
this.s = s;
}
//重写toString方法,使其输出指定格式的数据
public String toString(){
return "Book[s : " + s + "]";
}
}
//创建一个学生Student类
class Student
{
String s;
public Student(String s){
this.s = s;
}
//重写toString方法,使其输出指定格式的数据
public String toString(){
return "Student[s:" + s + "]";
}
}
//测试类
public class Example1
{
public static void main(String[] args)
{
//创建两个图书对象
Book b1 = new Book("图书类第一个对象");
Book b2 = new Book("图书类第二个对象");
//创建两个学生类对象
Student s1 = new Student("学生类第一个对象");
Student s2 = new Student("学生类第二个对象");
//创建一个ArrayList对象
ArrayList a = new ArrayList();
//调用ArrayList类的add()方法,将上面创建的对象添加进a对象中
a.add(b1);
a.add(b2);
a.add(s1);
a.add(s2);
//通过使用forEach方法输出a对象中的元素,因为ArrayListy是集合类
//集合类中的元素是没有类型,输出的全部都是Object类型
for(Object o : a){
System.out.println(o);
}
//也可以直接输出ArrayList对象中的元素
System.out.println(a);
}
}
需要一份500行的java程序,期末大作业,最好带详细注释。
Java生成CSV文件简单操作实例
CSV是逗号分隔文件(Comma Separated Values)的首字母英文缩写,是一种用来存储数据的纯文本格式,通常用于电子表格或数据库软件。在 CSV文件中,数据“栏”以逗号分隔,可允许程序通过读取文件为数据重新创建正确的栏结构,并在每次遇到逗号时开始新的一栏。如:
123 1,张三,男2,李四,男3,小红,女
Java生成CSV文件(创建与导出封装类)
package com.yph.omp.common.util;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.junit.Test;
/**
* Java生成CSV文件
*/
public class CSVUtil {
/**
* 生成为CVS文件
*
* @param exportData
* 源数据List
* @param map
* csv文件的列表头map
* @param outPutPath
* 文件路径
* @param fileName
* 文件名称
* @return
*/
@SuppressWarnings("rawtypes")
public static File createCSVFile(List exportData, LinkedHashMap map,
String outPutPath, String fileName) {
File csvFile = null;
BufferedWriter csvFileOutputStream = null;
try {
File file = new File(outPutPath);
if (!file.exists()) {
file.mkdir();
}
// 定义文件名格式并创建
csvFile = File.createTempFile(fileName, ".csv",
new File(outPutPath));
// UTF-8使正确读取分隔符","
csvFileOutputStream = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(csvFile), "GBK"), 1024);
// 写入文件头部
for (Iterator propertyIterator = map.entrySet().iterator(); propertyIterator
.hasNext();) {
java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator
.next();
csvFileOutputStream
.write("\"" + (String) propertyEntry.getValue() != null ? (String) propertyEntry
.getValue() : "" + "\"");
if (propertyIterator.hasNext()) {
csvFileOutputStream.write(",");
}
}
csvFileOutputStream.newLine();
// 写入文件内容
for (Iterator iterator = exportData.iterator(); iterator.hasNext();) {
Object row = (Object) iterator.next();
for (Iterator propertyIterator = map.entrySet().iterator(); propertyIterator
.hasNext();) {
java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator
.next();
/*-------------------------------*/
//以下部分根据不同业务做出相应的更改
StringBuilder sbContext = new StringBuilder("");
if (null != BeanUtils.getProperty(row,(String) propertyEntry.getKey())) {
if("证件号码".equals(propertyEntry.getValue())){
//避免:身份证号码 ,读取时变换为科学记数 - 解决办法:加 \t(用Excel打开时,证件号码超过15位后会自动默认科学记数)
sbContext.append(BeanUtils.getProperty(row,(String) propertyEntry.getKey()) + "\t");
}else{
sbContext.append(BeanUtils.getProperty(row,(String) propertyEntry.getKey()));
}
}
csvFileOutputStream.write(sbContext.toString());
/*-------------------------------*/
if (propertyIterator.hasNext()) {
csvFileOutputStream.write(",");
}
}
if (iterator.hasNext()) {
csvFileOutputStream.newLine();
}
}
csvFileOutputStream.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
csvFileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return csvFile;
}
/**
* 下载文件
*
* @param response
* @param csvFilePath
* 文件路径
* @param fileName
* 文件名称
* @throws IOException
*/
public static void exportFile(HttpServletRequest request,
HttpServletResponse response, String csvFilePath, String fileName)
throws IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/csv;charset=GBK");
response.setHeader("Content-Disposition", "attachment; filename="
+ new String(fileName.getBytes("GB2312"), "ISO8859-1"));
InputStream in = null;
try {
in = new FileInputStream(csvFilePath);
int len = 0;
byte[] buffer = new byte[1024];
OutputStream out = response.getOutputStream();
while ((len = in.read(buffer)) 0) {
out.write(buffer, 0, len);
}
} catch (FileNotFoundException e1) {
System.out.println(e1);
} finally {
if (in != null) {
try {
in.close();
} catch (Exception e1) {
throw new RuntimeException(e1);
}
}
}
}
/**
* 删除该目录filePath下的所有文件
*
* @param filePath
* 文件目录路径
*/
public static void deleteFiles(String filePath) {
File file = new File(filePath);
if (file.exists()) {
File[] files = file.listFiles();
for (int i = 0; i files.length; i++) {
if (files[i].isFile()) {
files[i].delete();
}
}
}
}
/**
* 删除单个文件
*
* @param filePath
* 文件目录路径
* @param fileName
* 文件名称
*/
public static void deleteFile(String filePath, String fileName) {
File file = new File(filePath);
if (file.exists()) {
File[] files = file.listFiles();
for (int i = 0; i files.length; i++) {
if (files[i].isFile()) {
if (files[i].getName().equals(fileName)) {
files[i].delete();
return;
}
}
}
}
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void createFileTest() {
List exportData = new ArrayListMap();
Map row1 = new LinkedHashMapString, String();
row1.put("1", "11");
row1.put("2", "12");
row1.put("3", "13");
row1.put("4", "14");
exportData.add(row1);
row1 = new LinkedHashMapString, String();
row1.put("1", "21");
row1.put("2", "22");
row1.put("3", "23");
row1.put("4", "24");
exportData.add(row1);
LinkedHashMap map = new LinkedHashMap();
map.put("1", "第一列");
map.put("2", "第二列");
map.put("3", "第三列");
map.put("4", "第四列");
String path = "d:/export";
String fileName = "文件导出";
File file = CSVUtil.createCSVFile(exportData, map, path, fileName);
String fileNameNew = file.getName();
String pathNew = file.getPath();
System.out.println("文件名称:" + fileNameNew );
System.out.println("文件路径:" + pathNew );
}
}
//注:BeanUtils.getProperty(row,(String) propertyEntry.getKey()) + "\t" ,只为解决数字格式超过15位后,在Excel中打开展示科学记数问题。
JAVA期末作业``高手来帮忙啊!
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Calculator {
public static void main(String[] args) {
MainFrame objMainFrame=new MainFrame();
objMainFrame.setVisible(true);
}
}
class MainFrame extends JFrame {
public boolean flag;
public String str1;
public String str2;
public String action;
public MainFrame() {
Dimension dim1 = Toolkit.getDefaultToolkit().getScreenSize();
Dimension dim2 = this.getSize();
int x = (dim1.width - dim2.width) / 2;
int y = (dim1.height - dim2.height) / 2;
this.setLocation(x, y);
this.setSize(400, 400);
this.setTitle("计算器:007/邦德");
JPanel contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JTextField txtResult = new JTextField();
txtResult.setBounds(2, 0, 208, 30);
txtResult.setText("");
txtResult.setFocusable(true);
JButton btn1 = new JButton();
btn1.setBounds(2, 116, 50, 40);
btn1.setText("1");
btn1.addMouseListener(new MouseAction(txtResult, btn1));
JButton btn2 = new JButton();
btn2.setBounds(54, 116, 50, 40);
btn2.setText("2");
btn2.addMouseListener(new MouseAction(txtResult, btn2));
JButton btn3 = new JButton();
btn3.setBounds(106, 116, 50, 40);
btn3.setText("3");
btn3.addMouseListener(new MouseAction(txtResult, btn3));
JButton btn4 = new JButton();
btn4.setBounds(2, 74, 50, 40);
btn4.setText("4");
btn4.addMouseListener(new MouseAction(txtResult, btn4));
JButton btn5 = new JButton();
btn5.setBounds(54, 74, 50, 40);
btn5.setText("5");
btn5.addMouseListener(new MouseAction(txtResult, btn5));
JButton btn6 = new JButton();
btn6.setBounds(106, 74, 50, 40);
btn6.setText("6");
btn6.addMouseListener(new MouseAction(txtResult, btn6));
JButton btn7 = new JButton();
btn7.setBounds(2, 32, 50, 40);
btn7.setText("7");
btn7.addMouseListener(new MouseAction(txtResult, btn7));
JButton btn8 = new JButton();
btn8.setBounds(54, 32, 50, 40);
btn8.setText("8");
btn8.addMouseListener(new MouseAction(txtResult, btn8));
JButton btn9 = new JButton();
btn9.setBounds(106, 32, 50, 40);
btn9.setText("9");
btn9.addMouseListener(new MouseAction(txtResult, btn9));
JButton btnPlus = new JButton();
btnPlus.setBounds(158, 32, 50, 40);
btnPlus.setText("+");
btnPlus.addMouseListener(new MouseOperate(txtResult, "plus"));
JButton btnSub = new JButton();
btnSub.setBounds(158, 74, 50, 40);
btnSub.setText("-");
btnSub.addMouseListener(new MouseOperate(txtResult, "sub"));
JButton btnDiv = new JButton();
btnDiv.setBounds(158, 116, 50, 40);
btnDiv.setText("/");
btnDiv.addMouseListener(new MouseOperate(txtResult, "div"));
JButton btnPro = new JButton();
btnPro.setBounds(158, 158, 50, 40);
btnPro.setText("*");
btnPro.addMouseListener(new MouseOperate(txtResult, "pro"));
JButton btn0 = new JButton();
btn0.setBounds(2, 158, 50, 40);
btn0.setText("0");
btn0.addMouseListener(new MouseAction(txtResult, btn0));
JButton btnClear = new JButton();
btnClear.setBounds(54, 158, 50, 40);
btnClear.setText("C");
btnClear.addMouseListener(new MouseDelete(txtResult));
JButton btnAnswer = new JButton();
btnAnswer.setBounds(106, 158, 50, 40);
btnAnswer.setText("=");
btnAnswer.addMouseListener(new MouseResult(txtResult));
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu menu = new JMenu("信息");
menuBar.add(menu);
JMenuItem openItem1 = new JMenuItem("学号:007");
menu.add(openItem1);
JMenuItem openItem2 = new JMenuItem("姓名:邦德");
menu.add(openItem2);
contentPane.add(txtResult);
contentPane.add(btn0);
contentPane.add(btn1);
contentPane.add(btn2);
contentPane.add(btn3);
contentPane.add(btn4);
contentPane.add(btn5);
contentPane.add(btn6);
contentPane.add(btn7);
contentPane.add(btn8);
contentPane.add(btn9);
contentPane.add(btnDiv);
contentPane.add(btnPro);
contentPane.add(btnPlus);
contentPane.add(btnSub);
contentPane.add(btnClear);
contentPane.add(btnAnswer);
}
class MouseAction extends MouseAdapter {
JTextField txtResult = new JTextField();
JButton btnResult = new JButton();
public MouseAction(JTextField txtResult, JButton btnResult) {
this.txtResult = txtResult;
this.btnResult = btnResult;
}
public void mouseClicked(MouseEvent e) {
if (flag) {
txtResult.setText(btnResult.getText());
flag = false;
} else {
txtResult.setText(txtResult.getText() + btnResult.getText());
}
}
}
class MouseDelete extends MouseAdapter {
JTextField txtResult = new JTextField();
public MouseDelete(JTextField txtResult) {
this.txtResult = txtResult;
}
public void mouseClicked(MouseEvent e) {
txtResult.setText("");
}
}
class MouseOperate extends MouseAdapter {
JTextField txtResult = new JTextField();
String lable;
public MouseOperate(JTextField txtResult, String lable) {
this.txtResult = txtResult;
this.lable = lable;
}
public void mouseClicked(MouseEvent e) {
action = lable;
str1 = txtResult.getText();
txtResult.setText("");
}
}
class MouseResult extends MouseAdapter {
int num1;
int num2;
int result;
JTextField txtResult = new JTextField();
public MouseResult(JTextField txtResult) {
this.txtResult = txtResult;
}
public void mouseClicked(MouseEvent e) {
str2 = txtResult.getText();
num1 = Integer.parseInt(str1);
num2 = Integer.parseInt(str2);
if (action.equals("plus")) {
result = num1 + num2;
} else if (action.equals("div")) {
result = num1 / num2;
} else
if (action.equals("sub")) {
result = num1 - num2;
} else {
result = num1 * num2;
}
txtResult.setText("" + result);
flag = true;
}
}
}
做一个简单的Java作业,大二期末作业
第一题直接上网搜java计算器,你想自己写也无所谓,不是多么难的事。第二题属于开放题类,你可以参考游戏中角色的属性值,比如说饱食度、血量之类的属性,六个知识点就更简单了,重载和抛异常就不说了,随便写一个继承类实现一个接口就能体现出重写、继承、接口。最后剩下一个你可以挑一个你比较熟悉的就行了,不建议选多线程,特别容易出问题,不管是正则还是文件流都是很容易体现的
JAVA学期作业的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于Java作业、JAVA学期作业的信息别忘了在本站进行查找喔。
发布于:2022-11-22,除非注明,否则均为
原创文章,转载请注明出处。