JAVA作业

JAVA作业,第1张

4 final

5 方法名称、返回值、参数签名

6 public class C extends B implements C

7 class

8 false,true,false

9 private

10 Object

11 编译时类型与运行时类型不一致,重写,重载

12 import

13 抽象方法,final修饰或者private修饰

14 aceg

判断:

1 不一定 protect修饰的 父类与子类在不同包里不能继承

2 错

3 错

4 对

5 错

6 对

更多JAVA问题,请去ITMUCH,IT木虫

1选择题

1,B

2D

3D

2int i = 30;

double d[] = new double[30];

char[] r = new char[30];

int[] i = {3,4,5,6};

float f[]  ={23F,35F,66F};

char[] c = new char[3];

3 int类型,最小为0,a[2]

4  double[] d = new double[10];

  d[9] = 55D;

double result = d[0] + d[1];

5 public static void method1(int n, int m) {

n += m;

method2((int) 34);

}

public static int method2(int n) {

if (n > 0)

return 1;

else if (n == 0)

return 0;

else

return -1;

}

6

public double getPromotion(double amount, double percent)

public void print(int year, int month)

public double sqrt(double d)

public boolean isOdd(int i);

public void printMsg(int times);

public double getLoanMonthlyPayment(double totalMoney, int loanYears, double interest);

public Char toUpperCase(char c);

public static void question1() {

Scanner scanner = new Scanner(Systemin);

double[] scores = new double[10];

int i = 0;

double totalScore = 0;

while (i < 10) {

double d = scannernextDouble();

scores[i] = d;

i++;

totalScore += d;

}

double avgScore = totalScore / 10;

Arrayssort(scores);

int index = ArraysbinarySearch(scores, avgScore);

Systemoutprintln("低于平均成绩的" + (index));

Systemoutprintln("高于平局成绩的" + (10 - index));

scannerclose();

}

public static void question2() {

double[] scores = { 100, 90, 80, 100, 70, 100, 90, 80, 100, 70 };

Arrayssort(scores);

double[] newScores = new double[8];

for (int i = 1; i < scoreslength - 2; i++) {

newScores[i - 1] = scores[i];

}

double totalScore = 0;

for (int i = 0; i < newScoreslength; i++) {

totalScore += newScores[i];

}

Systemoutprintln("得分是" + totalScore);

}

public static void question3() {

double[] d = new double[] { 1, 2, 3, 4, 5 };

double min = DoubleMAX_VALUE;

double max = DoubleMIN_VALUE;

for (int i = 0; i < dlength; i++) {

if (min > d[i]) {

min = d[i];

}

if (max < d[i]) {

max = d[i];

}

}

Systemoutprintln("最大值" + max);

Systemoutprintln("最小值" + min);

}

public static int average(int[] array) {

int total = 0;

for (int i = 0; i < arraylength; i++) {

total += array[i];

}

return total / arraylength;

}

public static double average(double[] array) {

double total = 0;

for (int i = 0; i < arraylength; i++) {

total += array[i];

}

return total / arraylength;

}

public boolean equals(int[] list1, int[] list2) {

if (list1 == null) {

return false;

}

if (list2 == null) {

return false;

}

if (list1length != list2length) {

return false;

}

for (int i = 0; i < list1length; i++) {

if (list1[i] != list2[i]) {

return false;

}

}

return true;

}

把这些数字都放在一个数组里面,比如叫做arr

然后Arrayssort(arr),具体函数有没有写对给忘了,你可以用eclipse的提醒功能找到这个东西

它的作用是从小到大排列一个数组

然后输出arr[0],arr[arrlength-1],就分别是最小和最大了

题目意思是问你要运行哪写步骤:

如果你输入的A,那么肯定捕获异常,因为要求输入的是整数

如果:你输入是A 会捕获InputMismatchException e

也就是会执行:

Systemoutprintln("----Flag 3 ----");

Systemoutprintln("输入数据格式错,要求是整型!");

Systemoutprintln("----Flag 4 ----");

接下来是:

Systemoutprintln("----Flag 5 ----");

Systemoutprintln("执行完毕!");

Systemoutprintln("----Flag 6 ----");

如果你输入的是3:

会执行下面这些:

打印一个C,不会越界,因为i-1 了, 3-1=2所以是C

Systemoutprintln("----Flag 2 ----");

Systemoutprintln("----Flag 5 ----");

Systemoutprintln("执行完毕!");

Systemoutprintln("----Flag 6 ----");

刚才修改了下输入3的后果,代码有点乱没仔细看,你还有一个数组存在

class Triangle {

double side1 = 10;

double side2 = 10;

double side3 = 10;

String mycolor;

boolean isFill;

Triangle(double side1,double side2,double side3,String mycolor,boolean isFill){

thisside1 = side1;

thisside2 = side2;

thisside3 = side3;

thismycolor = mycolor;

thisisFill = isFill;

}

private double getArea(){

return Mathtanh((((side1+side2+side3)/2)(((side1+side2+side3)/2)-side1)(((side1+side2+side3)/2)-side2)

(((side1+side2+side3)/2)-side3)));

}

private double getPerimeter(){

return side1+side2+side3;

}

public String toString(){

return "Area:"+getArea()+" "+"\n"+"Perimeter:"+getPerimeter()+"\n"+"color:"+" "+mycolor+"\n"+"isFill:"+"

"+isFill;

}

}

public class Test{

public static void main(String[] args) {

Triangle t = new Triangle(1,15,1,"yellow",true);

Systemoutprintln(ttoString());

}

}

import javaioBufferedReader;

import javaioInputStream;

import javaioInputStreamReader;

public class Test {

public static void main(String[] args) throws Exception {

String str = "请输入一个字符(如果想退出程序输入exit):";

String temp = "";

int num = 0;

InputStream in = Systemin;

InputStreamReader ir = new InputStreamReader(in);

while(true){

Systemoutprintln(str);

BufferedReader read = new BufferedReader(ir);

temp = readreadLine();

if("exit"equals(temp)){

Systemoutprintln("再见");break;

}

Systemoutprintln("输入的数据:"+temp);

char[] cs = temptoCharArray();

temp = "";

for(int i : cs){

if(i>=65&&i<=90){//大写变小写

num = num + 1;

i = i + 32;

}else if(i>=97&&i<=122){//小写变大写

num = num + 1;

i = i - 32;

}

temp = temp + (char)i;

}

Systemoutprintln("要修改的字符的个数:"+num);

Systemoutprintln("修改后的数据:"+temp);

}

}

}

Java生成CSV文件简单 *** 作实例

CSV是逗号分隔文件(Comma Separated Values)的首字母英文缩写,是一种用来存储数据的纯文本格式,通常用于电子表格或数据库软件。在 CSV文件中,数据“栏”以逗号分隔,可允许程序通过读取文件为数据重新创建正确的栏结构,并在每次遇到逗号时开始新的一栏。如:

123   1,张三,男2,李四,男3,小红,女   

Java生成CSV文件(创建与导出封装类)

package comyphompcommonutil;

import javaioBufferedWriter;

import javaioFile;

import javaioFileInputStream;

import javaioFileNotFoundException;

import javaioFileOutputStream;

import javaioIOException;

import javaioInputStream;

import javaioOutputStream;

import javaioOutputStreamWriter;

import javautilArrayList;

import javautilIterator;

import javautilLinkedHashMap;

import javautilList;

import javautilMap;

import javaxservlet>

import javaxservlet>

import orgapachecommonsbeanutilsBeanUtils;

import orgjunitTest;

/

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 (!fileexists()) {

filemkdir();

}

// 定义文件名格式并创建

csvFile = FilecreateTempFile(fileName, "csv",

new File(outPutPath));

// UTF-8使正确读取分隔符","

csvFileOutputStream = new BufferedWriter(new OutputStreamWriter(

new FileOutputStream(csvFile), "GBK"), 1024);

// 写入文件头部

for (Iterator propertyIterator = mapentrySet()iterator(); propertyIterator

hasNext();) {

javautilMapEntry propertyEntry = (javautilMapEntry) propertyIterator

next();

csvFileOutputStream

write("\"" + (String) propertyEntrygetValue() != null (String) propertyEntry

getValue() : "" + "\"");

if (propertyIteratorhasNext()) {

csvFileOutputStreamwrite(",");

}

}

csvFileOutputStreamnewLine();

// 写入文件内容

for (Iterator iterator = exportDataiterator(); iteratorhasNext();) {

Object row = (Object) iteratornext();

for (Iterator propertyIterator = mapentrySet()iterator(); propertyIterator

hasNext();) {

javautilMapEntry propertyEntry = (javautilMapEntry) propertyIterator

next();

/-------------------------------/ 

//以下部分根据不同业务做出相应的更改

StringBuilder sbContext = new StringBuilder("");

if (null != BeanUtilsgetProperty(row,(String) propertyEntrygetKey())) {

if("证件号码"equals(propertyEntrygetValue())){

//避免:身份z号码 ,读取时变换为科学记数 - 解决办法:加 \t(用Excel打开时,证件号码超过15位后会自动默认科学记数)

sbContextappend(BeanUtilsgetProperty(row,(String) propertyEntrygetKey()) + "\t");

}else{

sbContextappend(BeanUtilsgetProperty(row,(String) propertyEntrygetKey()));                         

}

}

csvFileOutputStreamwrite(sbContexttoString());

/-------------------------------/                 

if (propertyIteratorhasNext()) {

csvFileOutputStreamwrite(",");

}

}

if (iteratorhasNext()) {

csvFileOutputStreamnewLine();

}

}

csvFileOutputStreamflush();

} catch (Exception e) {

eprintStackTrace();

} finally {

try {

csvFileOutputStreamclose();

} catch (IOException e) {

eprintStackTrace();

}

}

return csvFile;

}

/

下载文件

@param response

@param csvFilePath

            文件路径

@param fileName

            文件名称

@throws IOException

/

public static void exportFile(>

>

throws IOException {

responsesetCharacterEncoding("UTF-8");

responsesetContentType("application/csv;charset=GBK");

responsesetHeader("Content-Disposition", "attachment; filename="

+ new String(fileNamegetBytes("GB2312"), "ISO8859-1"));

InputStream in = null;

try {

in = new FileInputStream(csvFilePath);

int len = 0;

byte[] buffer = new byte[1024];

OutputStream out = responsegetOutputStream();

while ((len = inread(buffer)) > 0) {

outwrite(buffer, 0, len);

}

} catch (FileNotFoundException e1) {

Systemoutprintln(e1);

} finally {

if (in != null) {

try {

inclose();

} catch (Exception e1) {

throw new RuntimeException(e1);

}

}

}

}

/

删除该目录filePath下的所有文件

@param filePath

            文件目录路径

/

public static void deleteFiles(String filePath) {

File file = new File(filePath);

if (fileexists()) {

File[] files = filelistFiles();

for (int i = 0; i < fileslength; 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 (fileexists()) {

File[] files = filelistFiles();

for (int i = 0; i < fileslength; 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 ArrayList<Map>();

Map row1 = new LinkedHashMap<String, String>();

row1put("1", "11");

row1put("2", "12");

row1put("3", "13");

row1put("4", "14");

exportDataadd(row1);

row1 = new LinkedHashMap<String, String>();

row1put("1", "21");

row1put("2", "22");

row1put("3", "23");

row1put("4", "24");

exportDataadd(row1);

LinkedHashMap map = new LinkedHashMap();

mapput("1", "第一列");

mapput("2", "第二列");

mapput("3", "第三列");

mapput("4", "第四列");

String path = "d:/export";

String fileName = "文件导出";

File file = CSVUtilcreateCSVFile(exportData, map, path, fileName);

String fileNameNew = filegetName();

String pathNew = filegetPath();

Systemoutprintln("文件名称:" + fileNameNew );

Systemoutprintln("文件路径:" + pathNew );

}

}

//注:BeanUtilsgetProperty(row,(String) propertyEntrygetKey()) + "\t" ,只为解决数字格式超过15位后,在Excel中打开展示科学记数问题。

import javaioBufferedReader;

import javaioIOException;

import javaioInputStreamReader;

public class TestCount {

public double count(double r)

{

return (4/3)MathPIMathpow(r, 3);

}

public static void main(String[] args) throws NumberFormatException, IOException

{

BufferedReader br=new BufferedReader(new InputStreamReader(Systemin));

TestCount t=new TestCount();

Systemoutprintln("请输入半径r:");

Double result=tcount(DoubleparseDouble(brreadLine()));

Systemoutprintln("圆球体积:"+result);

}

}

以上就是关于JAVA作业全部的内容,包括:JAVA作业、java作业题,都在这里了,希望你帮忙解答下、简单java程序作业等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/9284169.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-26
下一篇2023-04-26

发表评论

登录后才能评论

评论列表(0条)

    保存