JAVA期末大作业1.圆柱体:import java.util.*;public class cylinder {public static void main(String[] args) {double radius,height;final double PI=3.14;double area,volume;Scanner s=new Scanner(System.in);radius=s.nextDouble();height=s.nextDouble();area=2*PI*radius*radius+2*PI*radius*height;volume=PI*radius*radius*height;System.out.println("area="+area);System.out.println("volume="+volume);}}结果:2.heap:import java.util.*;public class heap {public static boolean isYearHeap(int x){if(x%4==0||x%400==0){return true;}else{return false;}}public static void main(String args[]){int i;Scanner s=new Scanner(System.in);i=s.nextInt();if(isYearHeap(i)){System.out.println("Heap");}else{System.out.println("Not a heap year");}}}结果:3.符号:import java.util.*;public class test {public static void main(String[] args) {int a=60;/*60=0011 1100*/int b=13;/*13=0000 1101*/int c=0;c=a&b;/*12=0000 1100*/System.out.println("a&b="+c);c=a|b;System.out.println("a|b="+c);c=a^b;System.out.println("a^b="+c);c=~a;System.out.println("~a="+c);c=a<<2;System.out.println("a<<2="+c);c=a>>2;System.out.println("a>>2="+c);c=a>>>2;System.out.println("a>>>2="+c);}}结果:4.if语句:import java.util.*;public class area {public static void main(String[] args) { double x,y;Scanner s=new Scanner(System.in);x=s.nextDouble();y=s.nextDouble();if((x*y>1&&x*y<4)||(x*y<-1&&x*y>-4)){ System.out.println("inside");}else{System.out.println("outside");}}}结果:5.grade:import java.util.*;import javax.swing.JOptionPane;public class grade {public static void main(String[] args) {String h=JOptionPane.showInputDialog("Input a letter:");char grade=h.charAt(0);switch(grade){case'a':System.out.println("85~100");break;case'b':System.out.println("70~84");break;case'c':System.out.println("60~69");break;case'd':System.out.println("<60");break;default:System.out.println("error");}}}结果:6.package com.tutorialspoint;import ng.*;public class BooleanDemo {public static void main(String[] args) {// create a Boolean object bBoolean b;// assign value to bb = new Boolean(true);// create a boolean primitive type boolboolean bool;// assign primitive value of b to boolbool = b.booleanValue();String str = "Primitive value of Boolean object " + b + " is " + bool; // print bool valueSystem.out.println( str );}}7.窗口:import javax.swing.*;public class first {public static void main(String args[]){JFrame first=new JFrame();first.setSize(300,400);first.setTitle("我的窗口");first.setVisible(true);}}结果:8.for循环:public class TestFor {public static void main(String[]args){ int i,j,k,l;for(i=1;i<=5;i++){k=5;for(j=0;j<9;j++,k--){if(i-k>=0){if(k>0)System.out.print(k);else{l=Math.abs(k)+2;if(l<=i)System.out.print(l);elseSystem.out.print(" ");}}elseSystem.out.print(" ");}System.out.println("");}}}结果:9.import java.util.Arrays;public class TestArray {public static void main(String[] args) { int a [] = {1,3,2,6,5,17,11,7}; System.out.println("排序之前数组:");for(int i = 0 ;i<a.length;i++){System.out.print(a[i]+"\t");}Arrays.sort(a);System.out.println("\n"+"排序后的数组:"); for(int i = 0 ;i<a.length;i++){System.out.print(a[i]+"\t");}}}10.数组;public class TestArray {public static void main(String[] args){double[] value={74.5,56.8,60.7,90.2,70.7};double mean,stdDev,sum=0.0,sumSq=0.0;for(int i=0;i<value.length;i++){sum+=value[i];sumSq+=value[i]*value[i];}mean=sum/value.length;System.out.println(mean);stdDev=Math.sqrt(sumSq/value.length-mean*mean);System.out.println(stdDev);}}结果;11.Arraylist:import java.util.ArrayList;import java.util.List;public class ArraylistTest {public static void main(String[]args){int i,sum=0;double Ave;List<Integer> List= new ArrayList<Integer>();for(i=0;i<10;i++){List.add(i);}for(i=0;i<10;i++){sum+=List.get(i);}Ave=sum/10.0;System.out.println(Ave);}}结果:12.import java.util.Arrays;public class TestArray {public static void main(String[] args) {int a [] = new int [6] ;int b [] = new int [6];Arrays.fill(a, 4);System.out.println("fill(a, val) 的使用;");for(int i = 0 ;i<a.length;i++){System.out.print(a[i]+"\t");}System.out.println("\n"+"fill(a, fromIndex, toIndex, val)的使用:"); Arrays.fill(b, 2, 4, 5);for(int i = 0 ;i<b.length;i++){System.out.print(b[i]+"\t");}}}13.public class Test {public static void stringReplace (String text) {text = text.replace('j' , 'i');}public static void bufferReplace (StringBuffer text) {text = text.append("C");}public static void main (String args[]) {String textString = new String ("java");StringBuffer textBuffer = new StringBuffer ("java");stringReplace (textString);bufferReplace (textBuffer);System.out.println (textString + textBuffer);}}结果:14.Rectangle:public class Rectangle {private int Length,Width;public Rectangle(){Length=1;Width=1;}public Rectangle(int Length,int Width) {this.Length=Length;this.Width=Width;}public int getArea(){return Length*Width;}public int getLength(){return Length;}public int getWidth(){return Width;}}结果:15.Person:public class Person {private String name;private int age;private double height;public Person(){name="WangWu";age=60;height=1.67;}public Person(String n,int s,double h){name=n;age=s;height=h;}public void printNeatly(){System.out.println("person name is "+name);System.out.println("person age is "+age);System.out.println("person height is "+height);}}public class TestPerson {public static void main(String args[]){Person s1=new Person("ZhangSan",23,1.73);s1.printNeatly();Person s2=new Person();s2.printNeatly();}}结果:16.public class Person {static int number=0;public Person(){number++;}public int getNumberOfObjects(){return number;}}public class Main {public static void main(String[] args){Person p1=new Person();System.out.println(p1.getNumberOfObjects());Person p2=new Person();System.out.println(p2.getNumberOfObjects());}}结果:17.Swing:import javax.swing.*;public class ComparingStrings {public static void main(String[] args) {String str1="Elephant",str2="eLePhant";int i;if(str1.equals(str2)){System.out.println("They are equal");}else{System.out.println("They are not equal");}if(str1.equalsIgnoreCase(str2)){System.out.println("Equal,but not for case");}else{System.out.println("They are not equal");}i=pareTo(str2);if(i==0)System.out.println(str1+"equals"+str2);else if(i>0)System.out.println(str2+"precedes"+str1);elseSystem.out.println(str1+"precedes"+str2);}}结果:18.Buffer:import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class printStdIn {public static void main(String[] args) {try{BufferedReader br=new BufferedReader(new InputStreamReader(System.in));String input;while((input=br.readLine())!=null){System.out.println(input);}}catch(IOException io){io.printStackTrace();}}}结果:19.Bar:public class Main {static Bar bar;static{bar=new Bar();}public static void main(String[]args){System.out.println(bar.toString());System.out.println(bar);}}class Bar {public Bar(){}public String toString(){return "Bar";}}结果:20.public class Test {int x,y;public static void main(String[] args) { Test test1=new Test();test1.x=1;test1.y=1;Test test2=new Test();test2.x=1;test2.y=1;if(test1.equals(test2))System.out.println("true");elseSystem.out.println("false");}public boolean equals(Test t){return (this.x==t.x)&&(this.y==t.y);}}结果:21.For增强:public class TestFor {public static void main(String[]args){int [] number={0,1,2,3,4,5,6,7,8,9};int sum=0;for(int item:number){sum=sum+item;}System.out.println(sum);}}结果:22.文件的操作:import java.io.File;public class DeleteFileExanmple {public static void main(String[] args) {try{File file=new File("c:\\test.txt");if(file.delete()){System.out.println(file.getName()+"is deleted!");}else{System.out.println("Delete operation is failed.");}}catch(Exception e){System.out.println("not sucessfully delete");}}}结果:。