gpt4 book ai didi

Java Programming for the Absolute Beginner 中列出的 Java 挑战

转载 作者:搜寻专家 更新时间:2023-11-01 02:29:54 25 4
gpt4 key购买 nike

这可能是一个初级问题。但是,我已经读完了 Java Programming for the Absolute Beginner 的第 5 章,并接近了挑战部分。我不太明白这个问题。

问题是:

"Create a Holder class so that it maintains a list of objects, possibly represented by Strings, which you can add to or take from by using its methods. Hint: try overriding the Vector class."

我看过 int [] [] convert --to--> Vector<Vector<Double>>页面并在最后一个回复中看到了一个简单的 Vector 示例,但我仍然难以理解这个问题。

我认为我的主要问题是了解如何覆盖 Holder 类。

这个问题的答案可能会帮助许多新的 Java 程序员理解 vector 和重写。

供您引用:
第二个问题是:

"Create a subclass of the Holder class. Use your imagination. Consider a Refrigerator class that holds food and keeps it cold, or a Wallet that holds money, perhaps. Try overriding methods defined in the Holder class. Try creating some overloaded methods as well.

这是一个完整的问题,我会在本章中添加一些实例,让你更好地理解我的问题出在哪里。我不想要编码答案(除非必要),只是用更基本的术语解释这个问题。

这些是本章中提供的示例:这些是我必须纳入答案的技术吗?

简单类定义:

public class SimpleCardDeck {
String[] cards = {“2C”, “3C”, “4C”, “5C”, “6C”, “7C”, “8C”,
“9C”, “10C”, “JC”, “QC”, “KC”, “AC”,
“2D”, “3D”, “4D”, “5D”, “6D”, “7D”, “8D”,
“9D”, “10D”, “JD”, “QD”, “KD”, “AD”,
“2H”, “3H”, “4H”, “5H”, “6H”, “7H”, “8H”,
“9H”, “10H”, “JH”, “QH”, “KH”, “AH”,
“2S”, “3S”, “4S”, “5S”, “6S”, “7S”, “8S”,
“9S”, “10S”, “JS”, “QS”, “KS”, “AS”};
public void list() {
for (int c=0; c < cards.length; c++) {
System.out.print(cards[c] + “ “);
}
}
}

创建和测试对象:

public class SimpleCardDeckTest {
public static void main(String args[]) {
SimpleCardDeck deck = new SimpleCardDeck();
System.out.println(deck.cards[0]);
System.out.println(deck.cards[10]);
System.out.println(deck.cards[51]);
deck.list();
}
}

一个类的例子:

public class Automobile {
public static final String DEFAULT_COLOR = “white”;
public String name;
public boolean running;
public String color;
public int numMiles;
public Automobile() {
this(false, DEFAULT_COLOR, 0);
}
public Automobile(boolean running, String color, int numMiles) {
this.running = running;
this.color = color;
this.numMiles = numMiles;
name = null;
}
public void start() {
if (running) {
System.out.println(“Can’t start, already running.”);
}
else {
running = true;
System.out.println(“The automobile has been started.”);
}
}
public void shutOff() {
if (!running) {
System.out.println(“Can’t shut off, not running.”);
}
else {
running = false;
System.out.println(“The automobile has been shut off.”);
}
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public void drive() {
if (running) {
numMiles += 10;
System.out.println(“You have driven 10 miles”);
}
else {
System.out.println(“You need to start the automobile first.”);
}
}
public int getNumMiles() {
return numMiles;
}
public String toString() {
String str;
str = “name = “ + name
+ “, running = “ + running
+ “, color = “ + color
+ “, numMiles = “ + numMiles;
return str;
}
}

类测试示例:

public class AutomobileTest {
public static void main(String args[]) {
Automobile auto1 = new Automobile();
System.out.println(“Auto 1: “ + auto1.toString());
Automobile auto2 = new Automobile(true, “green”, 37000);
auto2.name = “INGRID”;
System.out.println(“Auto 2: “ + auto2.toString());
System.out.println(“Driving Auto 1...”);
auto1.drive();
System.out.println(“Driving Auto 2...”);
auto2.drive();
System.out.println(“Starting Auto 1...”);
auto1.start();
System.out.println(“Starting Auto 2...”);
auto2.start();
System.out.println(“Giving Auto 1 a paint job...”);
auto1.setColor(“red”);
System.out.println(“Auto 1 is now “ + auto1.getColor());
System.out.println(“Renaming Auto 1...”);
auto1.name = “CHRISTINE”;
System.out.println(“Auto 1 is named “ + auto1.name);
System.out.println(“Shutting off Auto 2...”);
auto2.shutOff();
System.out.println(“Shutting off Auto 2 AGAIN...”);
auto2.shutOff();
System.out.println(“Auto 1: “ + auto1.toString());
System.out.println(“Auto 2: “ + auto2.toString());
}
}

汽车子类:

public class BigTruck extends Automobile {

protected boolean trailer;
public BigTruck() {
this(false, DEFAULT_COLOR, 0);
}
public BigTruck(boolean running, String color, int numMiles) {
super(running, color, numMiles);
trailer = false;
}
public void attachTrailer() {
if (trailer) {
System.out.println(“There is already a trailer attached.”);
}
else {
trailer = true;
System.out.println(“Attached a trailer.”);
}
}
public void detachTrailer() {
if (trailer) {
trailer = false;
System.out.println(“Detached the trailer.”);
}
else {
System.out.println(“There is no trailer attached.”);
}
}
public void haul() {
if (trailer) {
drive();
}
else {
System.out.println(“There is nothing to haul.”);
}
}
public String toString() {
String str = super.toString();
str += “, trailer = “ + trailer;
return str;
}
}

子类测试:

public class BigTruckTest {
public static void main(String args[]) {
BigTruck truck = new BigTruck();
System.out.println(truck);
System.out.println(“Starting...”);
truck.start();
System.out.println(“Driving...”);
truck.drive();
System.out.println(“Attaching Trailer...”);
truck.attachTrailer();
System.out.println(“Hauling...”);
truck.haul();
System.out.println(“Detaching trailer...”);
truck.detachTrailer();
System.out.println(“Shutting off...”);
truck.shutOff();
System.out.println(“Painting...”);
truck.setColor(“black”);
System.out.println(truck);
}
}

vector 维克多:

import java.util.Vector;
public class VectorVictor {
public static void main(String args[]) {
Vector v = new Vector(5, 5);
System.out.println(“size, capacity”);
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“Fuzzy”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“Wuzzy”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“was”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“a”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“bear”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“Fuzzy”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“Wuzzy”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“had”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“no”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“hair”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“Fuzzy”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“Wuzzy”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“wasn’t”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“fuzzy”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“was”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“he”));
System.out.println(v.size() + “, “ + v.capacity());
v.trimToSize();
System.out.println(v.size() + “, “ + v.capacity());
String[] str = new String[v.size()];
v.copyInto(str);
for (int s=0; s < str.length; s++) {
System.out.print(str[s] + “ “);
}
}
}

最佳答案

鉴于这本书已有 10 多年的历史,我觉得术语可能有点过时了。我怀疑他们希望您对 Vector 类进行子类化或创建一个类,该类是 Vector 的包装器(即:一个以 Vector 作为成员的类)。泛型使您书中使用的 Vector 类变得无关紧要。我相信 2004 年在 Java 中添加了泛型。参见:Java Generics Tutorial

我还强烈建议您挑选一本新的 Java 书籍来学习。自 2002 年以来,Java 编程语言发生了很大的变化,学习这种过时的语言版本并不能真正让自己受益匪浅。

我强烈建议您查看 Java 教程。它们是了解一般编程和 Java 语言的绝佳方式:Java Tutorials

关于Java Programming for the Absolute Beginner 中列出的 Java 挑战,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12081737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com