Java语言程序设计与数据结构(基础篇)课后练习题 第十二章(四)

12.24

import java.io.File;
import java.io.PrintWriter;

public class dishierzhang {

public static void main(String[] args) throws Exception {
	String str1;
	StringBuilder s1 = new StringBuilder();
	File file = new File(args[0]);
	String f = "FristName";
	String l = "LastName";
	try (PrintWriter p = new PrintWriter(file);) {
		for (int i = 0; i < 1000; i++) {
			s1.append(f + i + l + i);
			str1 = getGradeName((int) (Math.random() * 2 + 1));
			s1.append(" " + str1);
			s1.append(" " + getSalary(str1));
			p.println(s1.toString());
			s1.delete(0, s1.length());
		}
	}
}

public static String getGradeName(int i) {
	String s = null;
	if (i < 1 || i > 3) {
		System.out.println("Wrong!");
		System.exit(1);
	}
	switch (i) {
	case 1:
		return s = "assistant";
	case 2:
		return s = "associate";
	case 3:
		return s = "full";
	}
	return s;
}

public static String getSalary(String s) {
	String s1 = null;
	double salary = 0;
	if (!(s.equalsIgnoreCase("assistant") || s.equalsIgnoreCase("associate") || s.equalsIgnoreCase("full"))) {
		System.out.println("Uage: getSalary Error");
		System.exit(2);
	}
	if (s.equalsIgnoreCase("assistant")) {
		salary = Math.random() * 30000 + 50000;
	} else if (s.equalsIgnoreCase("associate")) {
		salary = Math.random() * 50000 + 60000;
	} else if (s.equalsIgnoreCase("full")) {
		salary = Math.random() * 55000 + 75000;
	}
	s1 = String.valueOf(salary);
	System.out.println(s1);
	int index = s1.indexOf('.');
	s1 = s1.substring(0, index + 3);
	return s1;
}

}

12.25

import java.net.URL;
import java.util.Scanner;

public class dishierzhang {

public static void main(String[] args) throws Exception {
	double salaryAssistant = 0;
	int countAssistant = 0;
	double salaryAssociate = 0;
	int countAssociate = 0;
	double salaryFull = 0;
	int countFull = 0;
	double SalaryQuantity = 0;
	URL url = new URL("http://liveexample.pearsoncmg.com/data/Salary.txt");
	String s = null;
	try (Scanner input = new Scanner(url.openStream());) {
		while (input.hasNext()) {
			s = input.nextLine();
			String[] s1 = s.split(" ");
			SalaryQuantity = Double.valueOf(s1[s1.length - 1]);
			if (s1[s1.length - 2].equalsIgnoreCase("assistant")) {
				salaryAssistant += SalaryQuantity;
				countAssistant++;
			} else if (s1[s1.length - 2].equalsIgnoreCase("associate")) {
				salaryAssociate += SalaryQuantity;
				countAssociate++;
			} else if (s1[s1.length - 2].equalsIgnoreCase("full")) {
				salaryFull += SalaryQuantity;
				countFull++;
			}
			s = null;
		}
	}
	System.out.println("Count: " + countAssistant + ", Salary: " + salaryAssistant + ", Aver: "
			+ salaryAssistant / countAssistant);
	System.out.println("Count: " + countAssociate + ", Salary: " + salaryAssociate + ", Aver: "
			+ salaryAssociate / countAssociate);
	System.out.println("Count: " + countFull + ", Salary: " + salaryFull + ", Aver: " + salaryFull / countFull);
}

}

12.26

import java.io.File;
import java.util.Scanner;

public class dishierzhang {

public static void main(String[] args) throws Exception {
	Scanner input = new Scanner(System.in);
	System.out.print("Enter a directory: ");
	String s = input.next();
	File file = new File(s);
	if (file.mkdir()) {
		System.out.println("Directory created successfully");
	} else {
		System.out.println("Directory already exists");
	}
}

}

12.27

12.28

import java.io.File;
import java.util.Scanner;

public class dishierzhang {

public static void main(String[] args) throws Exception {
	Scanner input = new Scanner(System.in);
	System.out.print("Enter a file path:");
	File file = new File(path(input.next()));
	renameFile(file);
}

public static String path(String path) {
	return path.replaceAll("\\\\", "/");
}

public static void renameFile(File file) {
	if (getNewName(file).equals("gg")) {
		if (file.isFile()) {
			return;
		} else {
			directory(file);
		}
	} else if (file.isDirectory()) {
		directory(file);
		file(file);

	} else if (file.isFile()) {
		file(file);
	}
}

public static File file(File file) {
	File newFile = new File(getNewName(file));
	System.out.println(newFile.getAbsolutePath() + " " + file.renameTo(newFile));
	return newFile;
}

public static void directory(File file) {
	File[] subFile = file.listFiles();
	System.out.println(file.getAbsolutePath());
	for (File everyFile : subFile) {
		renameFile(everyFile);
	}
}

public static String getNewName(File file) {
	String name = path(file.getAbsolutePath());
	String newName = null;
	if (!name.contains("Exercise")) {
		return "gg!";
	}

	String tail = name.substring(name.lastIndexOf("Exercise"), name.length());
	if (tail.contains(".")) {
		String[] countNum = tail.split("[ercise_.]");
		if (countNum[countNum.length - 3].length() == 1) {
			countNum[countNum.length - 3] = "0" + countNum[countNum.length - 3];
		}
		newName = name.substring(0, name.lastIndexOf("Exercise")) + "Exercise" + countNum[countNum.length - 3] + "_"
				+ countNum[countNum.length - 2] + "." + countNum[countNum.length - 1];
	} else {
		String[] countNum = tail.split("[ercise_]");
		if (countNum[countNum.length - 2].length() == 1) {
			countNum[countNum.length - 2] = "0" + countNum[countNum.length - 2];
		}
		newName = name.substring(0, name.lastIndexOf("Exercise")) + "Exercise" + countNum[countNum.length - 2] + "_"
				+ countNum[countNum.length - 1];

	}
	return newName;
}

}

12.29

12.30

import java.io.File;
import java.util.Scanner;

public class dishierzhang {

public static void main(String[] args) throws Exception {
	int[] alphabet = new int[26];
	Scanner input = new Scanner(System.in);
	System.out.print("Enter a filename: ");
	String s = input.next();
	File file = new File(s);
	if(!file.exists()){
		System.out.println("Not Exist!");
		System.exit(1);
	}
	Scanner output = new Scanner(file);
	while(output.hasNextLine()){
		String l = output.nextLine().toUpperCase();
		for(int i=0;i<l.length();i++)
			if(Character.isLetter(l.charAt(i)))
				alphabet[l.charAt(i)-'A']++;
	}
	for(int i=0;i<26;i++)
		System.out.println("Number of "+(char)(i+'A')+"s: "+alphabet[i]);
}

}

12.31

这个题感觉有点问题,中间split分裂后的数组s1[1]和s1[2]特别奇怪。。。。。差点给我搞吐了,不过还是做出来了,搞定!

import java.net.URL;
import java.util.Scanner;

public class dishierzhang {

public static void main(String[] args) throws Exception {
	Scanner input = new Scanner(System.in);
	System.out.print("Enter the year: ");
	String year = input.next();
	System.out.print("Enter the gender: ");
	String gender = input.next(); //M是男,F是女。
	System.out.print("Enter the name: ");
	String name = input.next();
	URL url = new URL("http://liveexample.pearsoncmg.com/data/babynamesranking" + year + ".txt");
	String s = null;
	try (Scanner input1 = new Scanner(url.openStream());) {
		while (input1.hasNextLine()) {
			s = input1.nextLine();
			String[] s1 = s.split(" ");
			if (gender.equals("M")) {
				if (s1[1].matches("(.*)" + name + "(.*)")) {
					System.out.print(name + " is ranked #" + s1[0] + " in vear " + year);
					System.exit(0);
				}
			} else if (gender.equals("F")) {
				if (s1[2].matches("(.*)" + name + "(.*)")) {
					System.out.print(name + " is ranked #" + s1[0] + " in vear " + year);
					System.exit(0);
				}
			} else {
				System.out.println("Wrong,gender!");
				System.exit(1);
			}
			s = null;
		}
	}
	System.out.println("The name " + name + " is not ranked in vear " + year);

}

}

12.32

照着上面的代码,自己改改吧。

12.33

import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;

public class dishierzhang {

public static void main(String[] args) throws Exception {
	Scanner input = new Scanner(System.in);
	System.out.print("Enter a urlString : ");
	String urlString = input.nextLine();
	System.out.print("Enter a word: ");
	String word = input.next();
	input.nextLine();

	check(word, urlString);
}

public static void check(String word, String s) {
	ArrayList<String> listOfPendingURLs = new ArrayList<>();
	ArrayList<String> listOfTraversedURLs = new ArrayList<>();

	listOfPendingURLs.add(s);
	boolean b = true;
	while (b) {
		String urlString = listOfPendingURLs.remove(0);
		if (!listOfTraversedURLs.contains(urlString)) {
			listOfTraversedURLs.add(urlString);
			if (getWord(word, urlString)) {
				System.out.println(urlString);
				System.exit(1);
			}

			for (String everyURL : getSubURLs(urlString)) {
				if (!listOfTraversedURLs.contains(everyURL)) {
					listOfPendingURLs.add(everyURL);
				}
			}
		}

		if (listOfPendingURLs.size() == 0) {
			System.out.println("Not find the " + word);
			b = false;
		}
	}
}

public static boolean getWord(String word, String urlString) {
	try {
		Scanner input = new Scanner(new URL(urlString).openStream());
		while (input.hasNextLine()) {
			String everyLine = input.nextLine();
			if (everyLine.contains(word)) {
				return true;
			}
		}
	} catch (Exception e) {
		System.out.println(e.getMessage());
	}

	return false;
}

public static ArrayList<String> getSubURLs(String urlString) {
	ArrayList<String> list = new ArrayList<>();

	try {
		URL url = new URL(urlString);
		Scanner input = new Scanner(url.openStream());
		int i = 0;
		while (input.hasNext()) {
			String line = input.nextLine();
			i = line.indexOf("http:");
			while (i > 0) {
				int endIndex = line.indexOf("\"", i);
				if (endIndex > 0) {
					list.add(line.substring(i, endIndex));
					i = line.indexOf("http:", endIndex);
				} else
					i = -1;
			}
		}
	} catch (Exception e) {
		System.out.println(e.getMessage());
	}

	return list;
}

}

第十二章习题 完

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xupengboo

你的鼓励将是我创作最大的动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值