Java语言程序设计与数据结构(基础篇)课后练习题 第十三章(一)

本文详细解析了Java面试中的基础题,包括冒泡排序示例和抽象类GeometricObject的实现。同时提供了一线大厂面试题集锦,涵盖基础到高级、框架、分布式等技术点,以及配套的学习资源和项目源码。
摘要由CSDN通过智能技术生成

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!

}

13.3

=================================================================

package dishisanzhang;

import java.util.ArrayList;

import java.util.Arrays;

public class dishisanzhang {

public static void main(String[] args) {

// TODO Auto-generated method stub

Integer[] n = {1, 5, 6, 4, 3, 2};

ArrayList list = new ArrayList<>(Arrays.asList(n));

sort(list);

System.out.println(list);

}

public static void sort(ArrayList list){

Number tmp; //下面是一个冒泡排序

for(int i=0;i<list.size()-1;i++)

for(int j=0;j<list.size()-i-1;j++)

if((int)(list.get(j+1))<(int)(list.get(j))){

tmp = list.get(j);

list.set(j, j+1);

list.set(j+1, tmp);

}

}

}

13.4

=================================================================

看清单!

13.5

=================================================================

package dishisanzhang;

import java.util.Date;

public abstract class GeometricObject implements Comparable< GeometricObject >{

private String color;

private boolean filled;

private Date dateCreated;

protected GeometricObject(){

dateCreated = new Date();

}

protected GeometricObject(String color,boolean filled){

this.color = color;

this.filled = filled;

dateCreated = new Date();

}

public String getColor(){

return color;

}

public void setColor(String color){

this.color = color;

}

public boolean isFilled(){

return filled;

}

public void setFilled(boolean filled){

this.filled = filled;

}

public Date getDateCreated(){

return dateCreated;

}

@Override

public String toString(){

return “Create on “+dateCreated+”\nColor: “+color+”\n and Filled”+filled;

}

@Override

public int compareTo(GeometricObject o){

if (getArea() < o.getArea())

{

return -1;

}

else if (getArea() > o.getArea())

{

return 1;

}

else

return 0;

}

public static GeometricObject max(GeometricObject o1,GeometricObject o2){

if (o1.compareTo(o2) > 0)

{

return o1;

}

else

return o2;

}

public abstract double getArea();

public abstract double getPerimeter();

}

package dishisanzhang;

public class Rectangle extends GeometricObject {

private double width;

private double height;

public Rectangle() {

}

public Rectangle(double width, double height) {

this(width, height, “white”, false);

}

public Rectangle(double width, double height, String color, boolean filled) {

super(color, filled);

this.width = width;

this.height = height;

}

public double getWidth() {

return width;

}

public void setWidth(double width) {

this.width = width;

}

public double getHeight() {

return height;

}

public void setHeight(double height) {

this.height = height;

}

@Override

public double getArea() {

return width * height;

}

@Override

public double getPerimeter() {

return 2 * (width + height);

}

@Override

public String toString() {

return "\nRectangle Width: " + getWidth() + " and Height: " + getHeight();

}

}

package dishisanzhang;

public class Circle extends GeometricObject {

private double radius;

public Circle() {

}

public Circle(double radius) {

this(radius, “white”, false);

}

public Circle(double radius, String color, boolean filled) {

super(color, filled);

this.radius = radius;

}

public double getRadius() {

return radius;

}

@Override

public double getArea() {

return radius * radius * Math.PI;

}

@Override

public double getPerimeter() {

return 2 * radius * Math.PI;

}

@Override

public String toString() {

return "\nCircle Radius : " + getRadius();

}

}

package dishisanzhang;

public class dishisanzhang {

public static void main(String[] args) {

// TODO Auto-generated method stub

Rectangle r1 = new Rectangle(1,2);

Rectangle r2 = new Rectangle(2,3);

System.out.println("The Max: "+GeometricObject.max(r1, r2));

Circle c1 = new Circle(2);

Circle c2 = new Circle(3);

System.out.println("The Max: "+GeometricObject.max(c1, c2));

}

}

13.6

=================================================================

package dishisanzhang;

public class Circle {

private double radius = 1.0;

public Circle() {

radius = 1.0;

}

public Circle(double radius) {

this.radius = radius;

}

public double getArea() {

return radius * radius * Math.PI;

}

}

package dishisanzhang;

public class ComparableCircle extends Circle implements Comparable< ComparableCircle > {

public ComparableCircle() {

super();

}

public ComparableCircle(double radius) {

super(radius);

}

@Override

public int compareTo(ComparableCircle o) {

if (this.getArea() > o.getArea()) {

return 1;

} else if (this.getArea() < o.getArea()) {

return -1;

} else

return 0;

}

}

package dishisanzhang;

public class dishisanzhang {

public static void main(String[] args) {

// TODO Auto-generated method stub

ComparableCircle c1 = new ComparableCircle(2.0);

ComparableCircle c2 = new ComparableCircle(3.0);

if (c1.compareTo(c2) > 0)

System.out.println(“c1 > c2”);

else if (c1.compareTo(c2) == 0)

System.out.println(“c1 = c2”);

else

System.out.println(“c1 < c2”);

}

}

13.7

=================================================================

package dishisanzhang;

import java.util.Date;

public abstract class GeometricObject {

private String color;

private boolean filled;

private Date dateCreated;

protected GeometricObject(){

dateCreated = new Date();

}

protected GeometricObject(String color,boolean filled){

this.color = color;

this.filled = filled;

dateCreated = new Date();

}

public String getColor(){

return color;

}

public void setColor(String color){

this.color = color;

}

public boolean isFilled(){

return filled;

}

public void setFilled(boolean filled){

this.filled = filled;

}

@Override

public String toString(){

return "Created on “+dateCreated+”\nColor: “+color+”\nFilled: "+filled;

}

public abstract double getArea();

public abstract double getPerimeter();

}

package dishisanzhang;

interface Colorable {

public abstract void howToColor();

}

package dishisanzhang;

public class Square extends GeometricObject implements Colorable{

private double width;

private double height;

public Square(){

}

public Square(double width,double height){

this(width,height,“white”,false);

}

public Square(double width, double height, String color, boolean filled) {

super(color, filled);

this.width = width;

this.height = height;

}

@Override

public double getArea() {

return width * height;

}

@Override

public double getPerimeter() {

return 2 * (width + height);

}

@Override

public void howToColor(){

System.out.println(“Color all four sides”);

}

@Override

public String toString(){

if(super.isFilled())

howToColor();

return super.toString();

}

}

package dishisanzhang;

最后

看完上述知识点如果你深感Java基础不够扎实,或者刷题刷的不够、知识不全面

小编专门为你量身定制了一套<Java一线大厂高岗面试题解析合集:JAVA基础-中级-高级面试+SSM框架+分布式+性能调优+微服务+并发编程+网络+设计模式+数据结构与算法>

image

针对知识面不够,也莫慌!还有一整套的<Java核心进阶手册>,可以瞬间查漏补缺

image

全都是一丢一丢的收集整理纯手打出来的

更有纯手绘的各大知识体系大纲,可供梳理:Java筑基、MySQL、Redis、并发编程、Spring、分布式高性能架构知识、微服务架构知识、开源框架知识点等等的xmind手绘图~

image

image
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!

  • (width + height);

}

@Override

public void howToColor(){

System.out.println(“Color all four sides”);

}

@Override

public String toString(){

if(super.isFilled())

howToColor();

return super.toString();

}

}

package dishisanzhang;

最后

看完上述知识点如果你深感Java基础不够扎实,或者刷题刷的不够、知识不全面

小编专门为你量身定制了一套<Java一线大厂高岗面试题解析合集:JAVA基础-中级-高级面试+SSM框架+分布式+性能调优+微服务+并发编程+网络+设计模式+数据结构与算法>

[外链图片转存中…(img-GqJlXIXS-1714693336410)]

针对知识面不够,也莫慌!还有一整套的<Java核心进阶手册>,可以瞬间查漏补缺

[外链图片转存中…(img-w8wDEnp3-1714693336411)]

全都是一丢一丢的收集整理纯手打出来的

更有纯手绘的各大知识体系大纲,可供梳理:Java筑基、MySQL、Redis、并发编程、Spring、分布式高性能架构知识、微服务架构知识、开源框架知识点等等的xmind手绘图~

[外链图片转存中…(img-NdOz0xan-1714693336411)]

[外链图片转存中…(img-veZGlky1-1714693336411)]
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!

  • 10
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值