新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
你好,按照你的要求,代码如下:
创新互联2013年开创至今,是专业互联网技术服务公司,拥有项目做网站、成都网站制作网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元鼓楼做网站,已为上家服务,为鼓楼各地企业和个人服务,联系电话:18982081108
-------------------------------------------------------------------------------
1.给定一个User类,要求编写一个用户管理类UserManager
-------------------------------------------------------------------------------
import java.util.HashSet;
public class UserManager {
HashSetUser set = new HashSetUser();
public void add(User user) {
set.add(user);
}
public void del(User user) {
set.remove(user);
}
public boolean isExist(User user) {
return set.contains(user);
}
public void print(User user) {
System.out.println(user.toString());
}
}
class User {
String name;
public String toString() {
return "姓名: " + name;
}
}
-------------------------------------------------------------------------------
2.给定一个Goods类,要求编写一个物品管理类GoodsManager
-------------------------------------------------------------------------------
import java.util.ArrayList;
public class GoodsManager {
ArrayListGoods list = new ArrayListGoods();
public void add(Goods user) {
list.add(user);
}
public void del(Goods user) {
list.remove(user);
}
public Goods select(int id) {
for (Goods goods : list) {
if (goods.id == id) {
return goods;
}
}
return null;
}
public void print() {
for (Goods goods : list) {
System.out.println(goods.toString());
}
}
}
class Goods {
int id;
public String toString() {
return "条形码编号: " + id;
}
}
Book.java
package com.books;
public abstract class Book {
private int page;
private double price;
private double zheKou;
public abstract void printPrice();
public Book(int page, double price, double zheKou) {
super();
this.page = page;
this.price = price;
this.zheKou = zheKou;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public double getZheKou() {
return zheKou;
}
public void setZheKou(double zheKou) {
this.zheKou = zheKou;
}
}
JiaoCai.java
package com.books;
public class JiaoCai extends Book{
public JiaoCai(int page, double price, double zheKou) {
super(page, price, zheKou);
}
@Override
public void printPrice() {
System.out.println(getPage() * 0.05 * getZheKou() * getPrice());
}
}
KeJi.java
package com.books;
public class KeJi extends Book {
public KeJi(int page, double price, double zheKou) {
super(page, price, zheKou);
}
@Override
public void printPrice() {
System.out.println(getPage() * 0.1 * getZheKou() * getPrice());
}
}
WenYi.java
package com.books;
public class WenYi extends Book{
public WenYi(int page, double price, double zheKou) {
super(page, price, zheKou);
}
@Override
public void printPrice() {
System.out.println(getPage() * 0.08 * getZheKou() * getPrice());
}
}
测试类。Test.java
package com.books;
public class Test {
public static void main(String[] args) {
int page = 530;
double zheKou = 0.7;
double price = 100;
Book book = new KeJi(page, price, zheKou);
book.printPrice();
book = new WenYi(page, price, zheKou);
book.printPrice();
book = new JiaoCai(page, price, zheKou);
book.printPrice();
}
}
有什么不懂的加我QQ,详解
这个题目,考查你对数组结构、操作的了解
数组在内存中,是一组“长度固定的(创建以后就不可修改)、地址相邻的内存空间”,比如三个元素的数组
String[] array = {"1","2","3"};其内存结构
--------
| 1 |
--------
--------
| 2 |
--------
--------
| 3 |
--------
如果我要在第2个位置插入数据,则需要:
1、修改数组长度
2、将2、3向下移一个位置,空出一个给“ddd”
下面是实现代码
public static void main(String[] args) {
String[] array = { "1", "2", "3" };
array = insert(array, "ddd", 2);
for (int i = 0; i array.length; i++) {
System.out.print(array[i] + " ");
}
}
/**
* 在数组array的第index个位置,播放元素value,并返回该数组
*/
public static String[] insert(String[] array, String value, int index) {
//因为数组是“固定长度”,所以,必须要使用一个新的数组
String[] newArray = new String[array.length + 1];
int count = 0;
//将旧数组+插值得到新数组
for (int i = 0; i array.length; i++) {
if ((i + 1) == index) {
newArray[count++] = "ddd";
}
newArray[count++] = array[i];
}
return newArray;
}