1.实现在控制台输出九九乘法表
/**
- 九九乘法表
- @author Administrator
/
public class nineTable {
public static void main(String[] args) {
int sum = 0;
//注意从1开始
for (int i = 1; i < 10; i++) {
for (int j = 1; j <= i; j++) {
sum = i * j;
System.out.print(j+ "" + i + “=” + sum +"\t");
}
System.out.println();
}
}
}
2.写出打印数组的内容,实现遍历数组,要求在main方法中使用
/**
- 输出数组
- @author Administrator
*/
public class PrintArr {
public static void main(String[] args) {
int[] arr = {1,2,3,4,5,6};
PrintArr print = new PrintArr();
print.print(arr);
}
public void print(int[] arr){
System.out.print("[");
for (int i = 0; i < arr.length-1; i++) {
System.out.print(arr[i]+",");
//判断输出最后一个数字时不打印逗号
if(i == arr.length-2){
System.out.print(arr[arr.length-1]);
}
}
System.out.print("]");
}
}
3.定义方法sum,要求实现两个数之和的运算,要求在main方法中使用
/**
- 求和方法
- @author Administrator
*/
public class Sum {
public static void main(String[] args) {
Sum sum = new Sum();
int i = 1;
int j = 2;
sum.sum(i , j);
}
public void sum(int i, int j) {
int sum = 0;
sum = i + j;
System.out.println(i +"+"+ j+"的和为"+sum);
}
}
将消费者在商城购物抽象出类,编写客户类,实现小明在欧尚买了体检T恤这样一个购物行为
package com.dodole.shop.character;
/**
- 衣服类
- @author Administrator
/
public class Clothes {
/*
* 衣服类型
*/
private String type;
/**
* 数量
*/
private int count;
/**
* @return the type
*/
public String getType() {
return type;
}
/**
* @param type the type to set
*/
public void setType(String type) {
this.type = type;
}
/**
* @return the count
*/
public int getCount() {
return count;
}
/**
* @param count the count to set
*/
public void setCount(int count) {
this.count = count;
}
}
package com.dodole.shop.character;
/**
- 人个体类
- @author Administrator
*/
public class Person {
private String name;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* 购买方法
* @param sm
* @param c
*/
public void buy(SuperMall sm, Clothes c) {
System.out.println(this.name + "在" + sm.getName() + "买了"
+ c.getCount() + "件" + c.getType());
}
}
package com.dodole.shop.character;
/**
- 购物地点
- @author Administrator
*/
public class SuperMall {
private String name;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
}
package com.dodole.shop.enitiy;
/**
- 第四题,输出一句话
/
import com.dodole.shop.character.;
public class ClientShop {
public static void main(String[] args) {
Person p = new Person();
p.setName(“小明”);
SuperMall sm = new SuperMall();
sm.setName("欧尚");
Clothes c = new Clothes();
c.setType("T恤");
c.setCount(1);
p.buy(sm, c);
}
}
5.定义长度为5的List的集合,按照价格从高到低排序,查看排序的结果
package com.dodoke.list;
/**
- 排序
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ClientList {
public static void main(String[] args) {
// 创建对象
Item book = new Item(“本子”, 5, “白色”);
Item computer = new Item(“电脑”, 4999, “黑色”);
Item desk = new Item(“桌子”, 500, “褐色”);
Item chair = new Item(“椅子”, 50, “蓝色”);
Item cup = new Item(“杯子”, 20, “透明的”);
// 创建集合
List<Item> items = new ArrayList<Item>();
// 把对象添加到集合中
items.add(book);
items.add(computer);
items.add(desk);
items.add(chair);
items.add(cup);
System.out.println("-----原始集合内的排列-----");
// 遍历集合输出
for (Item item : items) {
System.out.println(item);
}
System.out.println();
System.out.println("-----价格:从高到低-----");
// 按照价格由高到低排序输出
//排序方式一:实现comparable接口的compareTo方法;
Collections.sort(items);
// 遍历输出
for (Item item : items) {
System.out.println(item);
}
System.out.println();
System.out.println("-----价格:从低到高-----");
// 按照价格由低到高排序
/*
* 排序方式二:实现comparator接口的compare方法
* 优点:可以不改变原来累的继承等内容,重新建一个类,写比较方法
*/
Collections.sort(items, new ItemComparator());
// 遍历输出
for (Item item : items) {
System.out.println(item);
}
}
}
package com.dodoke.list;
import java.util.Comparator;
/**
-
物品类
-
@author Administrator 实现了Comparable接口的方法compareTo;
/
public class Item implements Comparable {
/*- 名称
*/
private String name;
private double price;
private String color;
// 存取方法
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;
}public String getColor() {
return color;
}public void setColor(String color) {
this.color = color;
}/**
- 构造方法
*/
public Item(String name, double price, String color) {
super();
this.name = name;
this.price = price;
this.color = color;
}
@Override
public String toString() {
return “名称:” + this.name + “, 价格:” + this.price + “, 颜色:” + this.color;
}/**
- 方式一:实现comparable接口的方法; 排序(从大到小,可修改)
*/
@Override
public int compareTo(Item o) {
if (this.price > o.price) {
// 返回值为负数时,不需要交换,代表从大到小排序
return -1;// 也可以这样写 return this.price-o.price
}
if (this.price < o.price) {
return 1;
}
return 0;
}
}
- 名称
/**
- 方式二:实现comparator接口;
- 可以实现多个接口,用,分隔
- @author Administrator
/
class ItemComparator implements Comparator {
/*- 实现compare抽象方法 这里是从小到大排序,可修改
*/
@Override
public int compare(Item o1, Item o2) {
if (o1.getPrice() > o2.getPrice()) {
// 返回值为正数时,表示需要交换,表示从小到大排序
return 1;
} else if (o1.getPrice() < o2.getPrice()) {
// 返回值为负数是,表示不需要交换
return -1;
}
return 0;
}
- 实现compare抽象方法 这里是从小到大排序,可修改
}
2019.3.24.21.43