java练习1

Contributor:石安安 Type:代码 Date time:2018-09-07 10:26:37 Favorite:17 Score:0
返回上页 Report
请选择举报理由:




Collection Modify the typo
package lab1;
import static java.lang.Math.*;
import java.util.*;
public class L1 {
public static void main(String[] args){
int j=0,i=0;
Scanner in = new Scanner(System.in);
System.out.print("请输入三角形行数:");
int n = in.nextInt();
for(j=1;j<(n+1);j++){
for(i=0;i<j;i++){
System.out.print("*");
}
System.out.print("\n");
}
}
}
package lab1;
import static java.lang.Math.*;
import java.util.Scanner;
public class L2 {
public static void main(String[] args){
double deierta=0,x1=0,x2=0,y=0;
Scanner in = new Scanner(System.in);
System.out.print("请输入a:");
double a = in.nextDouble();
System.out.print("请输入b:");
double b = in.nextDouble();
System.out.print("请输入c:");
double c = in.nextDouble();
deierta=b*b-4*a*c;
y=sqrt(deierta);
if(deierta>0){
System.out.print("有2个实数根:");
x1=((-b)+y)/(2*a);
x2=((-b)-y)/(2*a);
System.out.println("x1="+x1+",x2="+x2);
}else if(deierta==0){
System.out.print("有2个相同实数根:");
x1=(-b)/(2*a);
System.out.println("x="+x1);
}else if(deierta<0){
System.out.print("没有实数根");
}
}
}
package LAB2;
import java.math.*;
public class L1 {
public static void main(String[] args)
{
// TODO code application logic here
final int MAX_PER_CLASS = 40;
System.out.println(MAX_PER_CLASS);
float a= (float) 18.888;
int na = (int) a;
System.out.print(na);
BigInteger m =new BigInteger("12345678901234567890");
BigInteger d;
d = m.multiply(BigInteger.valueOf(123));
System.out.print(d);
}
}
package LAB2;
import java.lang.String;
public class L2 {
public static void main(String[] args)
{
String strlm = " I am a student. I like Java Language! ";
System.out.println(strlm.length());
System.out.println("第一个字符:"+strlm.substring(0,1)+"\n
最后一个字符:"+strlm.substring(strlm.length()-1,strlm.length()));
System.out.println(strlm.indexOf("student"));
String strlm1=strlm.trim();
System.out.println("去除首尾空格:"+strlm1);
System.out.println(strlm1.substring(2, 5));
String[] strlm2 = strlm1.split(" ");
for(String ch:strlm2)
{
System.out.println(ch);
}
StringBuilder builder = new StringBuilder();
System.out.println(builder.append(strlm1.replace(" ", "")));
System.out.println(strlm1.toUpperCase());
}
}
package LAB2;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;
public class L3 {
public static void main(String[] args) throws FileNotFoundException{
{
Scanner in = new Scanner(System.in);
System.out.print("在此输入");
int[] a = new int[5];
for(int i=0;i<5;i++){
a[i]=in.nextInt();
}
Arrays.sort(a);
PrintWriter out = new PrintWriter("e:\\myfile.txt");
for(int element:a){
System.out.print(element+" ");
out.flush();
}
}
}
}
package LAB3;
import java.util.Scanner;
public class Circle {
private double radius;
public static final double PI=3.14159;
//无参构造函数
public Circle() {
this.radius=0;
}
//有参构造函数
public Circle(double r) {
this.radius=r;
}
public double setRadius(double r) {
this.radius=r;
return radius;
}
public double getRadius() {
return radius;
}
public double area() {
double r=this.radius;
double area=r*r*PI;
return area;
}
public double peri() {
double r=this.radius;
double peri=2*r*PI;
return peri;
}
public static void main(String[] args)
{
System.out.println("请输入圆的半径");
Scanner cr=new Scanner(System.in);
double r=cr.nextDouble();
Circle cir1=new Circle();
cir1.setRadius(r);
System.out.println("无参构造方法:"+"圆的半径是:"+cir1.getRadius());
System.out.println("圆的面积是:"+cir1.area());
System.out.println("圆的周长是:"+cir1.peri());
Circle cir2=new Circle(r);
System.out.println("有参构造方法:"+"圆的半径是:"+cir2.getRadius());
System.out.println("圆的面积是:"+cir2.area());
System.out.println("圆的周长是:"+cir2.peri());
}
}
package LAB3;
public class Course {
private String classNumber;
private String className;
private double classGrade;
//无参构造方法
public Course() {
classNumber="";
className="";
classGrade=0;
}
public Course(String cn1,String cn2,double cg) {
classNumber=cn1;
className=cn2;
classGrade=cg;
}
public String getNumber() {
return classNumber;
}
public String getName() {
return className;
}
public double getGrade() {
return classGrade;
}
public double getHour() {
double hour=3*classGrade;
return hour;
}
}
package LAB3;
import java.time.*;
import java.util.Date;
public class L1 {
public static void main(String[] args)
{
Date today = new Date();
String t = today.toString();
System.out.println(t);
LocalDate newYear = LocalDate.now();
int year = newYear.getYear();
int month = newYear.getMonthValue();
System.out.println(year+" "+month);
}
}
package LAB3;
import java.util.Scanner;
public class TestCourse {
public static void main(String[] args) {
System.out.println("请输入课程编号、名称与学分");
Scanner sc=new Scanner(System.in);
Course c1=new Course(sc.next(),sc.next(),sc.nextDouble());
System.out.println("课程编号:"+c1.getNumber());
System.out.println("课程名称:"+c1.getName());
System.out.println("课程学分:"+c1.getGrade());
System.out.println("课程学时:"+c1.getHour());
}
}
package shianan.lab4;
public class Student {
private static int total;
private String snum;
private String sname;
private int sage;
{
snum="s000";
sname="我是谁";
sage=22;
total=40;
}
public Student() {
snum="";
sname="";
sage=0;
}
public Student(String number,String name,int age) {
snum=number;
sname=name;
sage=age;
}
public void changeValue(int age) {age = 111;}
public void changeValue(Student s){s. sage=222;}
public String getSnum(){
return snum;
}
public String getSname() {
return sname;
}
public int getSage() {
return sage;
}
public static int setTotal(int t) {
total=t;
return total;
}
public static int getTotal() {
return total;
}
public static void main(String[] args) {
Student st1=new Student("s001","孙悟空",500);
st1.setTotal(40);
System.out.println("number="+st1.getSnum()+",name="+st1.getSname()+
",age="+st1.sage+",班级总人数:"+st1.getTotal());
Student st2=new Student("s002","唐僧",50);
st2.setTotal(35);
System.out.println("number="+st2.snum+",name="+st2.sname+
",age="+st2.sage+",班级总人数:"+st2.getTotal());
System.out.println("number="+st1.getSnum()+",name="+
st1.getSname()+",age="+st1.sage+",班级总人数:"+st1.getTotal());
System.out.println("班级总人数:"+Student.getTotal());
int newAge=20;
st1.changeValue(newAge);
System.out.println("结果:"+newAge);
st1.changeValue(st2);
System.out.println("结果:"+st2.sage);
Student st3=new Student();
System.out.println("number="+st3.snum+",name="+st3.sname+
",age="+st3.sage+",班级总人数:"+st3.getTotal());
System.out.println("得出结果:与初始化块里的内容不相同");
}
}
package shianan.lab5;
public class Cub extends Rect{
private double height;
public Cub(){
super();
this.height=0;
}
public Cub(double l,double w,double h){
super(l,w);
this.height=h;
}
public void set(double length,double width,double height){
super.set(length, width);
this.height=height;
}
public double getHeight(){
return height;
}
public double peri(){
double peri=2*super.peri()+4*this.height;
return peri;
}
public double area(){
double area=2*(this.height*super.getLength()+this.height*super.getWidth()+super.area());
return area;
}
public double vol(){
double vol=super.area()*this.height;
return vol;
}
}
package shianan.lab5;
public class Rect {
private double length;
private double width;
public Rect(){
this.length=0;
this.width=0;
}
public Rect(double l,double w){
this.length=l;
this.width=w;
}
public void set(double length,double width){
this.length=length;
this.width=width;
}
public double getLength(){
return length;
}
public double getWidth(){
return width;
}
public double area(){
double area=this.length*this.width;
return area;
}
public double peri(){
double peri=2*(this.length+this.width);
return peri;
}
}
package shianan.lab5;
import java.util.Scanner;
public class Test {
public static void main(String[] args){
System.out.println("请按顺序输入长宽高");
Scanner in =new Scanner(System.in);
double l=in.nextDouble();
double w=in.nextDouble();
double h=in.nextDouble();
Cub cub1=new Cub();
cub1.set(l,w,h);
System.out.println("表面积="+cub1.area()+",体积="+cub1.vol());
Cub cub2=new Cub(l,w,h);
System.out.println("表面积="+cub2.area()+",体积="+cub2.vol());
System.out.println("结果一样");
System.out.println("长方体周长="+cub2.peri());
Rect rc1=new Rect(l,w);
System.out.println("新长方形面积="+rc1.area()+",周长="+rc1.peri());
}
}
package shianan.lab6;
public class Cub extends Rect{
private double height;
public Cub(){
super();
this.height=0;
}
public Cub(double l,double w,double h){
super(l,w);
this.height=h;
}
public void set(double length,double width,double height){
super.set(length, width);
this.height=height;
}
public double getHeight(){
return height;
}
public double peri(){
double peri=2*super.peri()+4*this.height;
return peri;
}
public double area(){
double area=2*(this.height*super.getLength()+this.height*super.getWidth()+super.area());
return area;
}
public double vol(){
double vol=super.area()*this.height;
return vol;
}
}
package shianan.lab6;
public class Rect extends Shape{
private double length;
private double width;
public Rect(){
this.length=0;
this.width=0;
}
public Rect(double l,double w){
this.length=l;
this.width=w;
}
public void set(double length,double width){
this.length=length;
this.width=width;
}
public double getLength(){
return length;
}
public double getWidth(){
return width;
}
public double area(){
double area=this.length*this.width;
return area;
}
public double peri(){
double peri=2*(this.length+this.width);
return peri;
}
}
package shianan.lab6;
public abstract class Shape {
public abstract double area();
public abstract double peri();
}
package shianan.lab6;
import java.util.Scanner;
public class Test {
public static void main(String[] args){
System.out.println("请输入数字,1是求长方形,2是求长方体");
Scanner in =new Scanner(System.in);
int i = in.nextInt();
Shape shp;
shp = new Rect();
shp = new Cub();
while(i>0&&i<3){
if(i==1){
System.out.println("请输入长和宽");
Scanner in1 =new Scanner(System.in);
double l=in.nextDouble();
double w=in.nextDouble();
shp=new Rect(l,w);
System.out.println("周长="+shp.peri()+"面积="+shp.area());
i=i+2;
}else if(i==2){
System.out.println("请依次输入长、宽、高");
Scanner in2 =new Scanner(System.in);
double l=in.nextDouble();
double w=in.nextDouble();
double h=in.nextDouble();
shp = new Cub(l,w,h);
System.out.println("周长="+shp.peri()+"面积="+shp.area());
i=i+2;
}
}
}
}
package shianan.lab7;
public class Rect implements ShapeCalc,Cloneable{
private double length;
private double width;
public Rect(){
this.length=0;
this.width=0;
}
public Rect(double l,double w){
this.length=l;
this.width=w;
}
public void set(double length,double width){
this.length=length;
this.width=width;
}
public double getLength(){
return length;
}
public double getWidth(){
return width;
}
public double area(){
double area=this.length*this.width;
return area;
}
public double peri(){
double peri=2*(this.length+this.width);
return peri;
}
public Rect clone() throws CloneNotSupportedException{
return (Rect)super.clone();
}
}
package shianan.lab7;
public interface ShapeCalc {
double area();
double peri();
}
package shianan.lab7;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
System.out.println("请输入长和宽:");
Scanner in =new Scanner(System.in);
double l=in.nextDouble();
double w=in.nextDouble();
ShapeCalc rc = new Rect(l,w);
Rect newrc = null ;
try {
newrc = ((Rect) rc).clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
System.out.println("周长="+rc.peri()+"面积="+rc.area());
System.out.println("周长="+newrc.peri()+"面积="+newrc.area());
}
}
package shianan.lab8;
public class project {
public static void main(String[] args){
String output[] ={ "The ","quick ","brown ","fox ","jumps ", "over ","the ","lazy ","dog."};
int i= 0;
while(i<12){
try{
System.out.print(output[i++]);
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println();
System.out.println("下标越界异常处理!");
System.out.println(e.toString());
break;
}finally{ System.out.println("不管怎样都要执行的语句!"); }
}
System.out.println("haha..."); }
}
package shianan.lab8;
public class project1 {
public static void main(String[] args){
int i= 100;
int j=0;
while(j<10){
try{
System.out.println(i/j++);
}catch(ArithmeticException e){
System.out.println();
System.out.println("除数为0异常处理!");
System.out.println(e.toString());
break;
}finally{ System.out.println("不管怎样都要执行的语句!");
}
}
}
}
package shianan.lab8;
import java.util.Scanner;
public class project2 extends Exception{
public project2(String string) {
// TODO 自动生成的构造函数存根
}
static int Multiply(int n,int m) throws project2{
int re;
re =n*m;
if(re>1000) throw new project2();
return re;
}
public static void main(String[] args){
System.out.println("请输入2个数");
Scanner in =new Scanner(System.in);
int n=in.nextInt();
int m=in.nextInt();
try {
System.out.println(Multiply(n,m));
} catch (project2 e) {
System.out.println();
System.out.println("结果re="+n*m+"超过了"+1000);
}
}
}
package shianan.lab9;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
public class CollectionTest {
public CollectionTest(){
ArrayList<String> list = new ArrayList<String>();
list.add("James");
list.add("Tom");
list.add("Steven");
list.add("Alice");
for(int count=0;count<list.size();count++)
System.out.print(list.get(count)+" ");
System.out.println();
System.out.println("迭代器输出:");
Iterator iter=list.iterator();
while(iter.hasNext())
{
String element = (String) iter.next();
if(element=="Tom"){
iter.remove();
}else{
System.out.println(element +" ");
}
}
Collections.sort(list);
System.out.println("sort排序后用for each输出:");
for(String a : list){
System.out.println(a);
}
}
public static void main(String[] args){
CollectionTest c1 = new CollectionTest();
System.out.println("此时Tom已被删除");
}
}
package shianan.lab9;
import java.util.*;
public class Student {
private String snum;
private String sname;
private int sage;
public Student() {
snum="";
sname="";
sage=0;
}
public Student(String number,String name,int age) {
snum=number;
sname=name;
sage=age;
}
public String toString() {
return "学生学号:"+snum+"姓名:"+sname+"年龄:"+sage;
}
public static void main(String[] args) {
Map<String,Student> staff = new HashMap();
Student st1=new Student("15H002","孙悟空",2000);
Student st2=new Student("15H001","唐僧",40);
Student st3=new Student("15H003","猪八戒",1000);
staff.put("15H002", st1);
staff.put("15H001", st2);
staff.put("15H003", st3);
System.out.println("判断是否有15H003和15H004学生:");
Student s1 = staff.get("15H003");
if(s1!=null) {
System.out.println(s1);
}else {
System.out.println("不存在这个学号的学生");
}
Student s2 =staff.get("15H004");
if(s2!=null) {
System.out.println(s2);
}else {
System.out.println("不存在这个学号的学生");
}
System.out.println();
for(Map.Entry<String, Student> entry : staff.entrySet())
{
String key = entry.getKey();
Student value = entry.getValue();
System.out.println(key+" "+value);
}
staff.remove("15H001");
System.out.println();
System.out.println("删除唐僧后的学生信息:");
for(Map.Entry<String, Student> entry : staff.entrySet())
{
String key = entry.getKey();
Student value = entry.getValue();
System.out.println(key+" "+value);
}
Object[] values = staff.values().toArray();
System.out.println(values.toString());
}
}
声明:以上文章均为用户自行添加,仅供打字交流使用,不代表本站观点,本站不承担任何法律责任,特此声明!如果有侵犯到您的权利,请及时联系我们删除。
Hot degree:
Difficulty:
quality:
Description: the system according to the heat, the difficulty, the quality of automatic certification, the certification of the article will be involved in typing!

This paper typing ranking TOP20

登录后可见

用户更多文章推荐