1.九九乘法表
package jiujiu;
/**
- 九九乘法表
- @author Administrator
/
public class Demo1 {
public static void main(String[] args) {
for(int i=1;i<10;i++){
for(int j=1;j<=i;j++){
System.out.print(j+""+i+"="+j*i+"\t");
}System.out.println();
}
}
}
输出结果
11=1
12=2 22=4
13=3 23=6 33=9
14=4 24=8 34=12 44=16
15=5 25=10 35=15 45=20 55=25
16=6 26=12 36=18 46=24 56=30 66=36
17=7 27=14 37=21 47=28 57=35 67=42 77=49
18=8 28=16 38=24 48=32 58=40 68=48 78=56 88=64
19=9 29=18 39=27 49=36 59=45 69=54 79=63 89=72 9*9=81
2.定义方法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);
}
}
输出结果
1+2=3
3.请写一个方法打印数组的内容,实现遍历数组,要求在main方法中调用。
public class Print {
public static void main(String[] args) {
Print p=new Print();
int[] a={1,2,3,4,5};
p.print(a);
}
public void print(int[] a){
System.out.print("[");
for(int i=0;i<=3;i++){
System.out.print(a[i]+",");
if(i==3){
System.out.print(a[4]);
}
}System.out.println("]");
}
}
输出结果
[1,2,3,4,5]
4.请将消费者在商城购物这个场景抽象出类,并编写一个客户端类,实现“小明在欧尚买了一件T恤”这样一个购物行为。
package shop;
//衣服类
public class Clothes {
private String brand;//品牌
private int count; //数量
public Clothes(){
}
public Clothes(String brand, int count) {
super();
this.brand = brand;
this.count = count;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
//超市类
public class Mall {
private String name;
public Mall(){
}
public Mall(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
//顾客类
public class People {
private String name;//名字
public People(){
}
public People(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/*
* 方法
*/
public void buy(Mall m, Clothes c) {
System.out.println(this.name + "在" + m.getName() + "买了"
+ c.getCount() + "件" + c.getBrand());
}
}
public class Test {
public static void main(String[] args) {
Clothes c=new Clothes();
c.setBrand(“T恤”);
c.setCount(1);
People p=new People();
p.setName(“小明”);
Mall m=new Mall();
m.setName(“欧尚”);
p.buy(m, c);
}
}
输出结果
小明在欧尚买了1件T恤
5.定义长度为5的物品List集合,按照价格从高到低排序,查看排序结果
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;
// }
}
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);
// }
}
}
输出结果
-----原始集合内的排列-----
名称:本子, 价格:5.0, 颜色:白色
名称:电脑, 价格:4999.0, 颜色:黑色
名称:桌子, 价格:500.0, 颜色:褐色
名称:椅子, 价格:50.0, 颜色:蓝色
名称:杯子, 价格:20.0, 颜色:透明的
-----价格:从高到低-----
名称:电脑, 价格:4999.0, 颜色:黑色
名称:桌子, 价格:500.0, 颜色:褐色
名称:椅子, 价格:50.0, 颜色:蓝色
名称:杯子, 价格:20.0, 颜色:透明的
名称:本子, 价格:5.0, 颜色:白色