7-2 判断闰年
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
int y=input.nextInt();
if(y%400==0)System.out.println("yes");
else if(y%100==0)System.out.println("no");
else if(y%4==0)System.out.println("yes");
else System.out.println("no");
}
}
7-5 分解质因数
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
for (int i = a; i <= b; i++) {
if (isPrime(i))continue;
else prime(i);
}
}
public static boolean isPrime(int n) {
boolean ret=true;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n%i==0){
ret=false;
break;
}
}
if (ret) System.out.println(n+"="+n);
return ret;
}
public static void prime(int n){
System.out.print(n+"=");
for (int i = 2; i <= n; i++) {
while (n%i==0&&n!=i){
System.out.print(i+"*");
n/=i;
}
if (i==n){
System.out.println(i);
}
}
}
}
7-6 统计最大数出现次数
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a;
int max=-1;
int count=1;
do {
a=sc.nextInt();
if (max
max = a;
count=1;
}
else if (max==a)count++;
}while (a!=0);
System.out.println("The largest number is "+max);
System.out.println("The occurrence count of the largest number is "+count);
}
}
7-10 jmu-Java-03面向对象基础-04-形状-继承
import java.util.Scanner;
abstract class Shape {
double PI = 3.14;
public abstract double getPerimeter();
public abstract double getArea();
}
class Rectangle extends Shape {
int wide, len;
Rectangle(int a, int b) {
wide = a;
len = b;
}
@Override
public double getPerimeter() {
// TODO Auto-generated method stub
return 2 * (wide + len);
}
@Override
public double getArea() {
// TODO Auto-generated method stub
return wide * len;
}
public String toString(){
return "[width=" + wide + ", length=" + len + "]";
}
}
class Circle extends Shape {
int radius;
Circle(int _radius) {
radius = _radius;
}
@Override
public double getPerimeter() {
// TODO Auto-generated method stub
return radius * 2 * PI;
}
@Override
public double getArea() {
// TODO Auto-generated method stub
return radius * radius * PI;
}
public String toString(){
return "[radius=" + radius + "]";
}
}
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
in.nextLine();
Shape A[] = new Shape[n];
int k = 0, j = 0;
double sumAllArea = 0, sumAllPerimeter = 0;
for (int i = 0; i < n; i++) {
String S = in.next();
if (S.equals("rect")) {
int wide = in.nextInt(), len = in.nextInt();
in.nextLine();
A[i] = new Rectangle(wide, len);
} else if (S.equals("cir")) {
int radius = in.nextInt();
in.nextLine();
A[i] = new Circle(radius);
}
sumAllArea += A[i].getArea();
sumAllPerimeter += A[i].getPerimeter();
}
System.out.println(sumAllPerimeter);
System.out.println(sumAllArea);
System.out.print("[");
for (int i = 0; i < n; i++) {
if(i != 0)
System.out.print(", ");
if (A[i] instanceof Rectangle) {
System.out.print("Rectangle ");
System.out.print(A[i].toString());
}
else {
System.out.print("Circle ");
System.out.print(A[i].toString());
}
}
System.out.println("]");
for(int i = 0;i < n;i++) {
if(A[i] instanceof Rectangle) {
System.out.println("class Rectangle,class Shape");
}else {
System.out.println("class Circle,class Shape");
}
}
in.close();
}
}
7-11 jmu-Java-03面向对象-06-继承覆盖综合练习-Person、Student、Employee、Company (15 分)
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws InterruptedException {
Scanner sc=new Scanner(System.in);
List
String name;
int age;
boolean gender;
String stuNo;
String clazz;
String companyName;
Company company;
double salary;
while(true) {
String t=sc.next();
if(t.equals("s")) {
name=sc.next();
age=sc.nextInt();
gender=sc.nextBoolean();
stuNo=sc.next();
clazz=sc.next();
if(name==null||stuNo==null||clazz==null) {
continue;
}
personList.add(new Student(name, age, gender, stuNo, clazz));
}else if(t.equals("e")){
name=sc.next();
age=sc.nextInt();
gender=sc.nextBoolean();
salary=sc.nextDouble();
companyName=sc.next();
company=new Company(companyName);
if(name==null) {
continue;
}
if(companyName==null) {
companyName="null";
}
personList.add(new Employee(name, age, gender, company, salary));
}else{
break;
}
}
Collections.sort(personList, new Name_AgeComparator());
for(int i=0;i System.out.println(personList.get(i).toString()); } String str=sc.next(); while(true) { if(str.equals("return")||str.equals("exit")) { break; }else { List List boolean flag1=true; boolean flag2=true; for(int i=0;i if(personList.get(i).toString().indexOf("Student")>=0) { if(stuList.size()==0) { stuList.add(personList.get(i)); } for(int j=0;j if(personList.get(i).equals(stuList.get(j))){ flag1=false; } } if(flag1) { stuList.add(personList.get(i)); } flag1=true; }else { if(empList.size()==0) { empList.add(personList.get(i)); } for(int j=0;j if(personList.get(i).equals(empList.get(j))){ flag2=false; } } if(flag2) { empList.add(personList.get(i)); } flag2=true; } } System.out.println("stuList"); for(int i=0;i System.out.println(stuList.get(i).toString()); } System.out.println("empList"); for(int i=0;i System.out.println(empList.get(i).toString()); } break; } } } static class Name_AgeComparator implements Comparator @Override public int compare(Person o1, Person o2) { if(o1.name.compareTo(o2.name)==0) { if(o1.age==o2.age) { return 0; }else if(o1.age return -1; }else { return 1; } }else { return(o1.name.compareTo(o2.name)); } } } } abstract class Person{ String name; int age; boolean gender; public Person(String name, int age, boolean gender) { super(); this.name = name; this.age = age; this.gender = gender; } @Override public String toString() { return this.name+'-'+String.valueOf(this.age)+'-'+String.valueOf(this.gender); } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Person other = (Person) obj; if (age != other.age) return false; if (gender != other.gender) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } } class Student extends Person{ String stuNo; String clazz; public Student(String name, int age, boolean gender, String stuNo, String clazz) { super(name,age,gender); this.clazz=clazz; this.stuNo=stuNo; } @Override public String toString() { return"Student:"+ super.toString()+'-'+this.stuNo+'-'+this.clazz; } @Override public boolean equals(Object obj) { if (!super.equals(obj)) return false; if (this == obj) return true; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (clazz == null) { if (other.clazz != null) return false; } else if (!clazz.equals(other.clazz)) return false; if (stuNo == null) { if (other.stuNo != null) return false; } else if (!stuNo.equals(other.stuNo)) return false; return true; } } class Company{ String name; public Company(){ } public Company(String name) { this.name = name; } @Override public String toString() { return name; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Company other = (Company) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } } class Employee extends Person{ Company company; double salary; public Employee(String name, int age, boolean gender, Company company, double salary) { super(name, age, gender); this.company = company; this.salary = salary; } @Override public String toString() { return "Employee:"+ super.toString()+'-'+company+'-'+salary; } @Override public boolean equals(Object obj) { if (!super.equals(obj)) return false; if (this == obj) return true; if (getClass() != obj.getClass()) return false; Employee other = (Employee) obj; if (company == null) { if (other.company != null) return false; } else if (!company.equals(other.company)) return false; DecimalFormat df = new DecimalFormat("#.#"); if (!df.format(salary) .equals( df.format(other.salary))) return false; return true; } } 7-15 USB接口的定义 interface USB{ void work(); void stop(); } class Mouse implements USB{ @Override public void work() { System.out.println("我点点点"); } @Override public void stop() { System.out.println("我不能点了"); } } class Upan implements USB{ @Override public void work() { System.out.println("我存存存"); } @Override public void stop() { System.out.println("我走了"); } } public class Main { public static void main(String[] args) { // TODO Auto-generated method stub USB usb1=new Mouse(); usb1.work(); usb1.stop(); USB usbs[]=new USB[2]; usbs[0]=new Upan(); usbs[1]=new Mouse(); for(int i=0;i usbs[i].work(); usbs[i].stop(); } } } 7-16 jmu-Java-03面向对象基础-01-构造函数与toString import java.util.Scanner; class Person{ private String name = null; private int age = 0; private boolean gender = false; private int id = 0; public Person() { System.out.println("This is constructor"); System.out.println(name+","+age+","+gender+","+id); System.out.println("Person [name="+name+", age="+age+", gender="+gender+", id="+id+"]"); } public Person(String n, int a, boolean g) { this.name = n; this.age = a; this.gender = g; } public String toString() { System.out.println("Person [name="+this.name+", age="+this.age+", gender="+this.gender+", id="+0+"]"); return name; } } public class Main { public static void main(String[] args) { // TODO Auto-generated method stub @SuppressWarnings("resource") Scanner reader = new Scanner(System.in); int number = reader.nextInt(); Person[] per = new Person[number]; for(int i=0; i String name = reader.next(); int age = reader.nextInt(); boolean genter = reader.nextBoolean(); per[i] = new Person(name,age,genter); } for(int x=per.length-1; x>=0;x--){ per[x].toString(); } per.toString(); @SuppressWarnings("unused") Person s = new Person(); } } 7-17 定义商品类,封装成员变量,输出对象 import java.util.Scanner; class stuff{ private String number; private String name; private double price; public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } stuff(String number, String name,double price ) { this.name = name; this.number = number; this.price = price; } public String toString(){ return getNumber()+","+getName()+","+getPrice(); } } public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String number = sc.next(); String name = sc.next(); double price = sc.nextDouble(); stuff a = new stuff(number, name, price); System.out.println(a.toString()); } } 7-18 定义类与创建对象 public class Main { public static void main(String[] args) { Person p1=new Person("lili", 19); Person p2=new Person("lucy", 20); System.out.println("this person is "+p1.name+",her age is "+p1.age); System.out.println("this person is "+p2.name+",her age is "+p2.age); } } class Person { String name; int age; public Person(String name, int age) { this.name = name; this.age = age; } } 7-14 教师类 import java.util.Scanner; class Teacher{ int no; String name; String seminary; int age; public Teacher(){ } public Teacher(int no, String name,int age, String seminary) { this.no = no; this.name = name; this.seminary = seminary; this.age = age; } public int getNo() { return no; } public void setNo(int no) { this.no = no; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSeminary() { return seminary; } public void setSeminary(String seminary) { this.seminary = seminary; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public boolean equals(Object obj){ Teacher t=(Teacher) obj; if(this.no==t.no) return true; return false; } public String toString(){ return "no: "+this.no+", name:"+this.name+", age: "+this.age+", seminary: "+seminary; } } public class Main { public static void main(String[] args) { Scanner s=new Scanner(System.in); Teacher t=new Teacher(s.nextInt(),s.next(),s.nextInt(),s.next()); Teacher t1=new Teacher(s.nextInt(),s.next(),s.nextInt(),s.next()); System.out.println(t.toString()); System.out.println(t1.toString()); System.out.println(t.equals(t1)); } } 6-1 jmu-Java-06异常-finally System.out.println("resource open success"); }catch(Exception e){ System.out.println(e); } try{ resource.close(); System.out.println("resource release success"); }catch(RuntimeException e){ System.out.println(e); } 6-2 jmu-Java-06异常-多种类型异常的捕获 catch(NumberFormatException e){ System.out.println("number format exception"); System.out.println(e); }catch(IllegalArgumentException e){ System.out.println("illegal argument exception"); System.out.println(e); }catch(Exception e){ System.out.println("other exception"); System.out.println(e); } 6-3 接口与多态 class Circle implements Shap{ Double r; final double PI=3.14; public Circle(double parseDouble) { super(); this.r = parseDouble; } @Override public double f() { if(r<=0) return 0; return 2*PI*r; } } class Rect implements Shap{ Double a,b; public Rect(double parseDouble, double parseDouble2) { super(); this.a = parseDouble; this.b = parseDouble2; } @Override public double f() { if(a<=0||b<=0) return 0; return 2*(a+b); } } class Tri implements Shap{ Double a,b,c; public Tri(double parseDouble, double parseDouble2, double parseDouble3) { super(); this.a = parseDouble; this.b = parseDouble2; this.c = parseDouble3; } @Override public double f() { if(a+b return a+b+c; } } 5-1 要求:根据Main类中main方法中的代码,设计满足要求的Student(学生)类:1)包含属性:int no(学号)、String name(姓名);2)满足Main类中main方法代码的说明要求。 Main类中main方法代码的说明:1)首先,从键盘接收形如“3 cuizhenyu 2 tiangang 1 dingchangqing 4 zhangfeng”的字符串,该字符串中包含了4个学生的学号和姓名(各学生以及学生的学号和姓名之间都用一个空格分隔,姓名中只包含英文字母),然后将该字符串内容中的前3个学生的学号及其姓名放到到Student数组stus中;2)将stus中的3个Student放入到HashSet stuSet中(注意:如果学生的学号相同,则认为是相同对象,不放入stuSet中);3)将第4个学生对象放入到stuSet中,如果第4个学生对象的学号与stuSet中已有学生对象的学号相同则不能放入。然后,打印出当前stuSet中学生对象的个数;4)用Arrays.sort方法对数组stus按照学生姓名的字母顺序排序(先比较首字母,首字母相同的比较第二个字母,以此类推),输出排序后的stus中3个学生对象的内容,每个学生对象的输出格式为“no=XX&name=YY”。 class Student implements Comparable private int no; private String name; public Student(int no, String name) { this.no = no; this.name = name; } public int getNo() { return no; } public void setNo(int no) { this.no = no; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString() { return "no=" + no + "&name=" + name; } public int compareTo(Student o) { if(this.name.compareTo(o.name)<0) { return -1; } else if(this.name.compareTo(o.name)>0)return 1; return 0; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + no; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (no != other.no) return false; return true; } } 5-2 class base { int x, y, z, w; public base(int a, int b) { x = a; y = b; } public base(int a, int b, int c, int d) { 空一:this(a,b); 空二:new base 5-3 根据输出结果,完成填空。 super(x); System.out.println(); 5-4 定义一个Person类,该类包括age,name两个数据成员和eat(),work()两个成员方法,并实现对两个数据成员的Getter方法。然后通过测试程序,实现相应对象的 *** 作。程序输出结果如下 private String name return name "会生活" new Person p.eat() 5-5 有如下父类和子类的定义,请根据要求填写代码。 super(a); this.a = a*10; super.a this.a 选择: 2-1 以下二维数组的定义正确的是( ) B. int a[][]=new int[3][] 2-2 下面那种类型不属于Java的基本数据类型? D. String 下面的数据声明及赋值哪一个是正确的? D. int i = 10; 2-4下列标识符(名字)命名原则中,符合规范的是___。 常量完全大写 2-5在JAVA中,给定代码片段如下所示,则编译运行后,输出结果是()。 for (int i = 0; i < 10; i++) { if (i == 10 - i) { break; } if (i % 3 != 0) { continue; } System.out.print(i + " "); } B. 0 3 2-6 以下选项中没有语法错误的是( ) 。 C. int j=0; for(int k=0; j + k !=10; j++,k++) { System.out.println(“ j is “+ j + “k is”+ k); } 2-7 以下代码段将创建几个对象? String s1="bc"; String s2="bc"; D. 1 2-8 关于垃圾收集的哪些叙述是对的。 B. 垃圾收集将检查并释放不再使用的内存。 2-9 你怎样强制对一个对象立即进行垃圾收集? E. 垃圾收集是不能被强迫立即执行 2-10 设有定义:String s=”World”;,下列语句错误的是( )。 D. String str=s.append(); 2-11 以下代码输出( )。 public static void main(String[] args) { String[] tokens = "Welcome to Java".split("o"); for (int i = 0; i < tokens.length; i++) { System.out.print(tokens[i] + " "); } } C. Welc me t Java 2-12 请选择下面程序正确的输出结果( ) public class Main{ public static void main(String args[ ]){ String a = new String("A"); String b = new String("B"); mb_operate(a,b); System.out.println(a + "." + b); } static void mb_operate(String x,String y){ x.concat(y); y=x; } } A. A.B 2-13 Java中,要对一个类实现for( : )形式的遍历,则该类必须实现下列哪一个接口? D. Iterable 2-14 在Java中,( )类可用于创建链表数据结构的对象 A. linkedList 2-15 现在有一个方法:public static int info(int x,double y),下面那个方法是对本方法的正确重载? C. public static int info(int x,int y); 2-16 在Java中,一个类可同时定义许多同名的方法,这些方法的形式参数个数、类型或顺序各不相同,传回的值也可以不相同。这种面向对象程序的特性称为( )。 C. 重载 2-17 假设类A有如下定义,且a是A类的一个实例,则必定错误的选项是( )。 class A { int i; static String s; void method1() { } static void method2() { } } C. A.method1(); 2-18 下面关于缺省构造方法的描述中正确的是( ) A. 当类中没有定义任何构造方法时,Java编译器将为这个类创建缺省构造方法 2-19 下述哪条关于构造方法的说法,不符合Java语法的规定( )。 2-20 以下代码的输出结果为( )。 public class Pass{ static int j = 20; public void amethod(int x){ x = x*2; j = j*2; } public static void main(String args[]){ int i = 10; Pass p = new Pass(); p.amethod(i); System.out.println(i+" and "+j); } } C. 10 and 40 2-21一个*.java文件中可以包含多少个public类? A. 最多1个 2-22 对于构造方法,下列叙述不正确的是( )。 B. 构造方法的返回类型只能是void型,即在方法名前加void 2-23 以下程序运行结果是 public class Test { public int div(int a, int b) { try { return a / b; }catch(Exception e){ System.out.println(“Exception”); }catch(NullPointerException e){ System.out.println(“ArithmeticException”); } catch (ArithmeticException e) { System.out.println(“ArithmeticException”); } finally { System.out.println(“finally”); } return 0; } public static void main(String[] args) { Test demo = new Test(); System.out.println(“商是:” + demo.div(9, 0)); } } D. 编译报错 2-24 对以下程序进行编译、运行结果是 abstract class Minebase { abstract void amethod(); static int i; } public class Mine extends Minebase{ public static void main(String argv[]){ int[] ar = new int[5]; for(i = 0;i < ar.length;i++) System.out.println(ar[i]); } } C. 编译出错。 2-25 下列程序的运行结果是( )。 public class Test { public static void main(String[] args) { try { System.out.println("Welcome to Java"); int i = 0; int y = 2 / i; System.out.println("Welcome to HTML"); } finally { System.out.println("The finally clause is executed"); } } } B. Welcome to Java,下一行是 The finally clause is executed , 然后是错误信息. 2-26 下面哪个标识符不符合Java定义要求? D. 100Book 2-27 下面( )不是Java的关键字。 C. sizeof 2-28 int型public成员变量MAX_LENGTH,该值保持为常数100,则定义这个变量的语句是( )。 D. public final int MAX_LENGTH = 100 2-29 Java 语言的特点不包括( ) (2分) C. 多重继承 2-30 编译Java源文件和解释执行Java字节码文件的指令分别是什么? D. javac.exe和java.exe 2-31 在Java中,以下()类的对象是以键-值的方式存储对象。 C. HashMap 2-32 要想在集合中保存没有重复的元素并且按照一定的顺序排序,可以使用以下()集合。 D. TreeSet 2-33 getCustomerInfo()方法如下,try中可以捕获三种类型的异常,如果在该方法运行中产生了一个IOException,将会输出什么结果( )。 public void getCustomerInfo() { try { // do something that may cause an Exception } catch (java.io.FileNotFoundException ex){ System.out.print("FileNotFoundException!"); } catch (java.io.IOException ex){ System.out.print("IOException!"); } catch (java.lang.Exception ex){ System.out.print("Exception!"); } } A. IOException! 2-34 下面代码运行结果是 public class Demo{ public int add(int a,int b){ try{ return a+b; }catch(Exception e){ System.out.println(“catch 语句块”); }finally{ System.out.println(“finally 语句块”); } return 0; } public static void main(String[] args){ Demo demo = new Demo(); System.out.println(“和是:”+demo.add(9,34)); } } B. finally语句块 和是:43 2-35 下列程序的错误是( ) public class Test { public static void main (String[] args) { try { System.out.println("Welcome to Java"); } } } B. 有一个try块但没有catch块或finally块。 2-36 已知下列代码,如果方法oneMethod()运行异常,则下列哪个语句肯定不会被输出? public void example(){ try { oneMethod(); System.out.println("condition1"); }catch(ArrayIndexOutOfBoundsException e) { System.out.println("condition2"); }catch(Exception e) { System.out.println("condition3"); }finally{ System.out.println("condition4"); } A. condition1 2-37 将以下哪种方法插入行6是不合法的。( ) 。 1.public class Test1 { 2. public float aMethod(float a,float b) throws 3. IOException { } 4. } 5.public class Test2 extends Test1{ 6. 7.} A. float aMethod(float a,float b){ } 2-38 下面的概念,哪个不是关于对象的多态性的体现。 B. 方法的继承 2-39 多态的表现形式有 A. 重写 2-40 class Person { public void printValue(int i, int j) {//... } public void printValue(int i){//... } } public class Teacher extends Person { public void printValue() {//... } public void printValue(int i) {//...} public static void main(String args[]){ Person t = new Teacher(); t.printValue(10); } D. 行7 2-41 如下代码的输出是( )。 public class Test { public static void main(String[] args) { new Person().printPerson(); new Student().printPerson(); } } class Student extends Person { private String getInfo() { return "Student"; } } class Person { private String getInfo() { return "Person"; } public void printPerson() { System.out.println(getInfo()); } } A. Person Person 2-42 已知类的继承关系如下: class Employee; class Manager extends Employee; class Director extends Employee; 则以下语句哪个能通过编译? ( )。 A. Employee e = new Manager(); 2-43 设类B是类C的父类,下列声明对象x1的语句中不正确的是? D. C x1=new B( ); 欢迎分享,转载请注明来源:内存溢出
第10行语句将调用哪行语句?
评论列表(0条)