System.out.println(Float.parseFloat(str2)); } }
17.编写程序,将long型数据987654转换为字符串,将十进制数365转换为十六进制数表示的字符串。 参考代码: public class Ex17 {
public static void main(String[] args) { long num=987654L; int i=365;
System.out.println(“Long类型转换为String:“+String.valueOf(num)); HexI=DtoX(i);
System.out.println(HexI); }
//转换函数
public static String DtoX(int d) {
String x=”\ if(d<16){ x=change(d); } else{ int c; int s=0; int n=d; while(n>=16){ s++; n=n/16; }
String [] m=new String[s]; int i=0; do{ c=d/16;
//判断是否大于10,如果大于10,则转换为A-F的格式 m[i++]=change(d); d=c;
}while(c>=16);
String x=change(d);
for(int j=m.length-1;j>=0;j–){ x+=m[j]; } } return x; }
//判断是否为10-15之间的数,如果是则进行转换 public static String change(int d){ String x=”\ switch(d){ case 10: x=”A”; break; case 11: x=”B”; break; case 12: x=”C”; break; case 13: x=”D”; break; case 14: x=”E”; break; case 15: x=”F”; break; default:
x=String.valueOf(d); } return x; } }
18.编写一个程序,接收以克为单位的一包茶叶的单位重量、卖出的包数和每克的价格,计算并显示出销售的总额。其中三个数据一行输入,数据间用“-”分隔。比如:输入“3-100-2.1”,
表示每包的重量为3克,共卖出100包,每克的价格为2.1元。此时的销售总额为630元。 参考代码:
import java.util.Scanner; public class Ex18 {
public static void main(String[] args) { Scanner scan=new Scanner(System.in);
System.out.println(“请依次输入重量、包数、价格,并以-分隔:“); String strIn=scan.nextLine(); Scanner sc=new Scanner(strIn); sc.useDelimiter(“-”); //设置分隔符 int num=sc.nextInt(); int bag=sc.nextInt(); float price=sc.nextFloat(); double total=price*num*bag;
System.out.println(“销售总额为:“+total); } }
19.编写一个泛型方法,能够返回一个int类型数组的最大值和最小值、String类型数组的最大值和最小值(按字典排序)。 参考代码: class Pair
public Pair() { min = null; max = null; }
public Pair(T min, T max) { this.min = min; this.max = max; } public T getMin() { return min; } public T getMax() { return max; }
public void setMin(T newValue) { min = newValue; } public void setMax(T newValue) { max = newValue; } }
class ArrayAlg {
public static
T min = a[0];T max = a[0]; for (int i = 1; i < a.length; i++) {
if (min.compareTo(a[i]) > 0) {min = a[i];} if (max.compareTo(a[i]) < 0) { max = a[i];} }
return new Pair
public class Ex19 {
public static void main(String[] args) { //测试整型数组
Integer[] arrI={1,2,3,4,5,6};
Pair
System.out.println(“整型数组的最小值:“+p1.getMin().intValue()); System.out.println(“整型数组的最大值:“+p1.getMax().intValue()); //测试字符串数组 String[
]
words
={“able”,”word”,”excel”,”course”,”java”,”c#”};
Pair
p2=ArrayAlg.minmax(words);
System.out.println(“字符串数组的最小值:“+p2.getMin()); System.out.println(“字符串数组的最大值:“+p2.getMax()); } }
20.编写一个泛型方法,接受对象数组和集合作为参数,将数组中的对象加入集合中。并编写代码测试该方法。 参考代码: import java.util.*; public class Ex20 {
static
public static void main(String[] args) { Integer[] ia = new Integer[100];
Collection
21.试编写一个List类型的对象只能存储通讯录(存储同学的姓名和联系方式),并输出通讯
录的列表到控制台。 参考代码: import java.util.*; class Student{ private String name; private String phone;
public Student(String name, String phone) { this.name = name; this.phone = phone; }
public String toString() { return name+”:”+phone; } }
public class Ex21 {
public static void main(String[] args) {
Student st1=new Student(“John”,”23214″); Student st2=new Student(“Alice”,”4563″); List 22.设计一个程序,基于泛型Map实现10个英文单词的汉语翻译,即通过单词得到它的中文含义。 参考代码: import java.util.*; public class Ex22 { public static void main(String[] args) { String[] eng={“Apple”,”Orange”,”Green”}; String[] chs={“苹果“,”桔子“,”绿色“}; Map System.out.println(test+”翻译:“+map.get(test)); }