565656

(1)定义Point(点)、Circle(圆形)、Square(正方形)类。点信息包括x,y坐标。圆信息包括圆心坐标和半径。正方形信息包括中心坐标和边长。用关联(Point对象作为Circle和Square的成员)和继承(Circle和Square分别继承Point)两种方法实现并测试。

public class Test {

public static void main(String[] args) {
	// TODO Auto-generated method stub
Circle1 c1=new Circle1(1,2,3);
Square1 s1=new Square1(1,2,3);
Circle2 c2=new Circle2(1,2,3);
Square2 s2=new Square2(1,2,3);
}

}

class Point{
double x,y;
Point(double x,double y){
this.x=x;
this.y=y;
}
}
class Circle1 extends Point{
double r;
Circle1(double x,double y,double r){
super(x,y);
this.r=r;
}
}
class Square1 extends Point{
double a;
Square1(double x,double y,double a){
super(x,y);
this.a=a;
}

}

class Circle2{
Point point;
double r;
Circle2(double x,double y,double r){
this.point=new Point(x,y);
this.r=r;
}
}
class Square2{
Point point;
double a;
Square2(double x,double y,double a){
this.point=new Point(x,y);
this.a=a;
}

}
(2)
定义抽象类Shape(形状),包含形状名字属性及相应构造方法。
定义两个接口,一个接口IArea定义求面积的方法,一个接口IVolume定义求体积的方法。
定义Shape类的子类矩形(Rectangle)类,实现IArea接口。
定义长方体类同时实现IArea(求表面积)和IVolume接口。
在main方法中测试。
public class Test {
public static void main(String[] args) {
Rectangle rec1=new Rectangle(“长方形”,1,2);
Cuboid cub1=new Cuboid(1,2,3);
System.out.println(rec1.area());
System.out.println(cub1.area());
System.out.println(cub1.volume());
}
}
abstract class Shape{
String name;

public Shape(String name) {
	this.name = name;
}

}
interface IArea {

double area();// 求面积

}

interface IVolume {
double volume();// 求体积
}

class Rectangle extends Shape implements IArea{
int len,wid;

public Rectangle(String name, int len, int wid) {
	super(name);
	this.len = len;
	this.wid = wid;
}

@Override
public double area() {
	// TODO Auto-generated method stub
	return this.len*this.wid;
}

}
class Cuboid implements IArea,IVolume{
int len,wid,hei;

public Cuboid(int len, int wid, int hei) {
	super();
	this.len = len;
	this.wid = wid;
	this.hei = hei;
}

@Override
public double volume() {
	// TODO Auto-generated method stub
	return this.len*this.wid*hei;
}

@Override
public double area() {
	// TODO Auto-generated method stub
	return 2*(len*wid+len*hei+wid*hei);
}

}

3、日期类:
public class Test {
public static void main(String[] args) {
Date date1=new Date(2001,3,15);
System.out.println(date1+":是一年中的第"+date1.days()+“天”);
Date date2=new Date(2018,10,1);
System.out.println(date2+":是一年中的第"+date2.days()+“天”);
}
}

class Date{
int year,month,day;

public Date(int year, int month, int day) {		
	this.year = year;
	this.month = month;
	this.day = day;
}

public Date() {
	this(1900, 1, 1);
}
public boolean isLeapYear(){
	return year%4==0&&year%100!=0||year%400==0;
}
public int days(){
	int[] day={0,31,28,31,30,31,30,31,31,30,31,30,31};
	int sum=this.day;
	for(int i=1;i<month;i++){
		sum+=day[i];
	}
	if(month>2&&isLeapYear()){
		sum++;
	}
	return sum;
}
public String toString(){
	return year+"年"+month+"月"+day+"日";
}

}

2补全代码:

输入1-12间的整数(代表月份),输出该月份的天数。

如果用户输入的不是整数或输入的整数不在1到12之间,给出“NumberFormat Error”或“the num is not bween 1 and 12”的提示.

import java.util.Scanner;
public class Test{
public static void main(String[] args){
System.out.println(“输入月份数”);
Scanner sc=new Scanner(System.in);
String m=sc.next();
int month=Integer.parseInt(m);//把字符串转为整数,这句会发生异常
。。。
}
}

import java.util.Date;
public class Test {
public static void main(String[] args) {
Date currentDate = new Date();
System.out.println(“currentDate=” + currentDate);
System.out.println(“自1970年1月1日起经历的毫秒数:” + currentDate.getTime());
Date newDate = new Date(100000000);
System.out.println(“newDate=” + newDate);
System.out.println(currentDate.before(newDate));
System.out.println(currentDate.after(newDate));
}
}

3、约瑟夫问题

描述

约瑟夫问题:有n只猴子,按顺时针方向围成一圈选大王(编号从1到n),从第1号开始报数,一直数到m,数到m的猴子退出圈外,剩下的猴子再接着从1开始报数。就这样,直到圈内只剩下一只猴子时,这个猴子就是猴王,编程求输入n,m后,输出最后猴王的编号。

输入

每行是用空格分开的两个整数,第一个是 n, 第二个是 m ( 0 < m,n <=300)。最后一行是:

0 0

输出

对于每行输入数据(最后一行除外),输出数据也是一行,即最后猴王的编号

样例

输入:

6 2
12 4
8 3
0 0
输出:

5
1
7
分析:可以用List对象list1表示n个猴子围成的圈,报数相当于list1.add(list1.remove(0));出圈相当于list1.remove(0);

代码:

import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
int n = sc.nextInt();
int m = sc.nextInt();
if (n == 0 && m == 0) {
break;
}
List list1 = new LinkedList();
for (int i = 1; i <= n; i++) {
list1.add(i);
}
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < m - 1; j++) {
list1.add(list1.remove(0));
}
list1.remove(0);
}
System.out.println(list1.get(0));
}

}

}
4、字符串排序

描述

先输入你要输入的字符串的个数。然后换行输入该组字符串。每个字符串以回车结束,每个字符串少于一百个字符。如果在输入过程中输入的一个字符串为“stop”,也结束输入。
然后将这输入的该组字符串按每个字符串的长度,由小到大排序,按排序结果输出字符串。

输入

字符串的个数,以及该组字符串。每个字符串以‘\n’结束。如果输入字符串为“stop”,也结束输入.

输出

将输入的所有字符串按长度由小到大排序输出(如果有“stop”,不输出“stop”)。

样例

输入

5
sky is grey
cold
very cold
stop
3
it is good enough to be proud of
good
it is quite good
输出

cold
very cold
sky is grey
good
it is quite good
it is good enough to be proud of
分析:字符串按长度排序。

代码:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()) {
int n = sc.nextInt();
sc.nextLine();
List list1 = new ArrayList<>();
for (int i = 0; i < n; i++) {
String s = sc.nextLine();
if (“stop”.equals(s)) {
break;
} else {
list1.add(s);
}
}
Collections.sort(list1, (a, b) -> a.length() - b.length());
for (String ss : list1) {
System.out.println(ss);
}
}

}

}

1程序改错,在提交文件中写清楚题号、出错原因及改正方法
(1)class A{
public static void main(String[] args) {
f();
}
void f(){
}
}
静态方法不能直接调用实例方法。
改正方法:f()前加static,
或通过对象调用f():A a=new A();a.f();
(2)class A{
void f(){
}
}
class B extends A{
int f(){
return 1;
}
}
子类继承父类方法void f(),自己定义方法int f(),形参相同,重载错误,因为调用时无法区分。
改正方法:int f()中加个参数或改个方法名。
(3)
class A{
A(int i){
}
}
class B extends A{
}
子类B要调用父类A的无参构造方法,父类A没有。
改正方法:A加一个无参构造方法。
(4)
class A{
A(int i){
}
A(){
A(1);
}
}
构造方法之间调用不用方法名,用this。
改正:把A(1)改为this(1)
(5)
class A{
A(int i){
}
A(){
System.out.println();
this(1);
}
}
this(1);要放在第1句。
(6)
class A{
int i;
static void f(){
this.i=10;
}
}
静态方法不能访问实例成员。改正:f()前static去掉。
(7)
class A{
void f(int i){
}
String f(int k){
}

}
重载错误。
(8)哪些正确,哪句编译报错,哪句运行报错。
public class Test {
public static void main(String[] args) {
A v1=new A(); 正确
B v2=new B();正确
B v3=new A();编译报错
A v4=new B();正确,自动转换
B v5=(B)v1;编译正确,运行报错,类转换错误,因为v1本就不是B。
B v6=(B)v4;正确,v4本就是B,只是披上了A的外衣。
}
}
class A{
}
class B extends A{
}

(9)public class Test {
public static void main(String[] args) {
A a = new A();
}
}
abstract class A {
A() { }
}
抽象类可以有构造方法,但不能直接创建对象。

(10)
class A{
abstract void f();
}
普通类中不能有抽象方法。改正:class A前加abstract

(11)
abstract class A{
abstract void f();
}
class B extends A{

}
继承抽象类的普通类,要实现抽象类的抽象方法。

(12)
interface A{
int a;
}
接口中常量a(public static final可以省略)要初始化。int a=123;

(13)
interface A{
void f(){
}
}
接口中不允许有普通方法。改正方法:f()改成抽象的,或前面加default或加static。

(14)
interface A{
}
class B extends A{
}
extends改为implements

(15)
interface A{
void f();
}
class B implements A{
}
B要实现A中的抽象方法f()

(16)
interface A{
A(){
}
}
接口不能有构造方法
2下面两题是独立的。
(1)定义Point(点)、Circle(圆形)、Square(正方形)类。点信息包括x,y坐标。圆信息包括圆心坐标和半径。正方形信息包括中心坐标和边长。用关联(Point对象作为Circle和Square的成员)和继承(Circle和Square分别继承Point)两种方法实现并测试。

public class Test {

public static void main(String[] args) {
	// TODO Auto-generated method stub
Circle1 c1=new Circle1(1,2,3);
Square1 s1=new Square1(1,2,3);
Circle2 c2=new Circle2(1,2,3);
Square2 s2=new Square2(1,2,3);
}

}

class Point{
double x,y;
Point(double x,double y){
this.x=x;
this.y=y;
}
}
class Circle1 extends Point{
double r;
Circle1(double x,double y,double r){
super(x,y);
this.r=r;
}
}
class Square1 extends Point{
double a;
Square1(double x,double y,double a){
super(x,y);
this.a=a;
}

}

class Circle2{
Point point;
double r;
Circle2(double x,double y,double r){
this.point=new Point(x,y);
this.r=r;
}
}
class Square2{
Point point;
double a;
Square2(double x,double y,double a){
this.point=new Point(x,y);
this.a=a;
}

}
(2)
定义抽象类Shape(形状),包含形状名字属性及相应构造方法。
定义两个接口,一个接口IArea定义求面积的方法,一个接口IVolume定义求体积的方法。
定义Shape类的子类矩形(Rectangle)类,实现IArea接口。
定义长方体类同时实现IArea(求表面积)和IVolume接口。
在main方法中测试。
public class Test {
public static void main(String[] args) {
Rectangle rec1=new Rectangle(“长方形”,1,2);
Cuboid cub1=new Cuboid(1,2,3);
System.out.println(rec1.area());
System.out.println(cub1.area());
System.out.println(cub1.volume());
}
}
abstract class Shape{
String name;

public Shape(String name) {
	this.name = name;
}

}
interface IArea {

double area();// 求面积

}

interface IVolume {
double volume();// 求体积
}

class Rectangle extends Shape implements IArea{
int len,wid;

public Rectangle(String name, int len, int wid) {
	super(name);
	this.len = len;
	this.wid = wid;
}

@Override
public double area() {
	// TODO Auto-generated method stub
	return this.len*this.wid;
}

}
class Cuboid implements IArea,IVolume{
int len,wid,hei;

public Cuboid(int len, int wid, int hei) {
	super();
	this.len = len;
	this.wid = wid;
	this.hei = hei;
}

@Override
public double volume() {
	// TODO Auto-generated method stub
	return this.len*this.wid*hei;
}

@Override
public double area() {
	// TODO Auto-generated method stub
	return 2*(len*wid+len*hei+wid*hei);
}

}

1运行下面代码会出现什么异常,在提交文件中写清题号及异常类型
(1)
public class Test {
public static void main(String[] args) {
int a=10/0;
}
}
ArithmeticException:算数异常
(2)
public class Test {
public static void main(String[] args) {
int[] a=new int[3];
System.out.println(a[3]);
}
}
ArrayIndexOutOfBoundsException:数组下标越界
(3)
public class Test {
public static void main(String[] args) {
B b=(B)new A();
}
}
class A{
}
class B extends A{

}
ClassCastException:类转换异常
(4)
import java.io.File;
import java.io.FileInputStream;
public class Test {
public static void main(String[] args) throws Exception{
File file1=new File(“c:/aaa.txt”);//创建文件对象
FileInputStream fis=new FileInputStream(file1);//创建上述文件的输入流对象,为读文件做准备
}
}
FileNotFoundException:文件找不到异常
(5)
public class Test {
public static void main(String[] args) {
int[] a=new int[-2];
}
}
NegativeArraySizeException:数组个数为负数异常
(6)
public class Test {
public static void main(String[] args) {
A[] s=new A[10];
s[0].f();
}
}
class A{
void f(){
}
}
NullPointerException:空指针异常
(7)
public class Test {

public static  void main(String[] args)  {
String s="abc";
int a=Integer.parseInt(s);//把s装换为整数	  
}

}
NumberFormatException:数值格式转换异常
(8)
public class Test {

public static  void main(String[] args)  {
String s="abc";
System.out.println(s.charAt(3));//输出字符串从左数的第4个字符
}

}
StringIndexOutOfBoundsException:字符串下标越界
(9)
public class Test {
public static void main(String[] args) {
Class.forName(“aa.bb.cc.Test”);//加载包aa.bb.cc下的类Test
}
}
ClassNotFoundException:类找不到异常
(10)
import java.util.Scanner;
public class Test {
public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
}

}
运行时输入一个非整数。
java.util.InputMismatchException:输入格式不匹配
2补全代码:
补充如下代码:输入1-12间的整数(代表月份),输出该月份的天数,
如果用户输入的不是整数或输入的整数不在1到12之间,给出“NumberFormat Error”或“the num is not bween 1 and 12”的提示.

import java.util.Scanner;
public class Test{
public static void main(String[] args){
System.out.println(“输入月份数”);
Scanner sc=new Scanner(System.in);
String m=sc.next();
int month=Integer.parseInt(m);//把字符串转为整数,这句会发生异常
。。。
}
}
import java.util.Scanner;
public class Test{
public static void main(String[] args){
System.out.println(“输入月份数”);
Scanner sc=new Scanner(System.in);
String m=sc.next();
try {
int month=Integer.parseInt(m);//把字符串转为整数,这句会发生异常
if(month>=1 && month<=12) {
String[] ms= {“31”,“28 or 29”,“31”,“30”,“31”,“30”,“31”,“31”,“30”,“31”,“30”,“31”};
System.out.println(ms[month-1]);
}
else {
System.out.println(“month is not between 1 and 12”);
}

	} catch (NumberFormatException e) {
		// TODO Auto-generated catch block
		System.out.println("Number Format Error");
	}
	
     
}

}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值