Commit 603991f2 authored by 时良荣's avatar 时良荣

首次上传

parents
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="java-exercise" />
</profile>
</annotationProcessing>
</component>
</project>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="JavaDoc" enabled="true" level="WARNING" enabled_by_default="true">
<option name="TOP_LEVEL_CLASS_OPTIONS">
<value>
<option name="ACCESS_JAVADOC_REQUIRED_FOR" value="none" />
<option name="REQUIRED_TAGS" value="" />
</value>
</option>
<option name="INNER_CLASS_OPTIONS">
<value>
<option name="ACCESS_JAVADOC_REQUIRED_FOR" value="none" />
<option name="REQUIRED_TAGS" value="" />
</value>
</option>
<option name="METHOD_OPTIONS">
<value>
<option name="ACCESS_JAVADOC_REQUIRED_FOR" value="none" />
<option name="REQUIRED_TAGS" value="@return@param@throws or @exception" />
</value>
</option>
<option name="FIELD_OPTIONS">
<value>
<option name="ACCESS_JAVADOC_REQUIRED_FOR" value="none" />
<option name="REQUIRED_TAGS" value="" />
</value>
</option>
<option name="IGNORE_DEPRECATED" value="false" />
<option name="IGNORE_JAVADOC_PERIOD" value="true" />
<option name="IGNORE_DUPLICATED_THROWS" value="false" />
<option name="IGNORE_POINT_TO_ITSELF" value="false" />
<option name="myAdditionalJavadocTags" value="date" />
</inspection_tool>
</profile>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4" />
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>exercise</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
public class HelloWorld {
public static void main(String[] args) {
System.out.println("hello world!");
}
}
package com.exercise.fifth;
import java.util.ArrayList;
/**
* 作业题目1:
* 编写代码,使之导致java堆溢出、虚拟机栈溢出、方法区溢出、和本地内存溢出;
* @author elliot
*/
public class ExerciseFirst {
public static void main(String[] args){
// heapOverflow(); // 堆溢出
// stackOverflow(); // 栈溢出
// noHeapOverflow(); // 方法区溢出
// (new JavaVMStackOOM()).stackLeakByThread(); // 本地内存溢出
}
/**
* 堆溢出
* java.lang.OutOfMemoryError: Java heap space
*/
public static void heapOverflow(){
ArrayList list = new ArrayList();
int i = 1;
while (true){
list.add(new ExerciseFirst());
i++;
}
}
/**
* 栈溢出
* java.lang.StackOverflowError
*/
public static void stackOverflow(){
new ExerciseFirst().stackTest();
}
private void stackTest(){
stackTest();
}
/**
* 方法区溢出
*/
public static void noHeapOverflow(){
ArrayList list = new ArrayList();
int strCount = 0;
while (true) {
list.add(String.valueOf(strCount++).intern());
}
}
}
/**
* 本地内存溢出
*/
class JavaVMStackOOM {
private void dontStop() {
while (true) {
}
}
public void stackLeakByThread() {
while (true) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
dontStop();
}
});
thread.start();
}
}
}
package com.exercise.fifth;
import jdk.nashorn.internal.parser.JSONParser;
/**
* 作业题目2:
* 创建一个实体类,覆写toString()方法使之输出json格式字符串;
* @author elliot
*/
public class ExerciseSecond {
public static void main(String[] args){
new MyString2().toString("sss");
}
}
class MyString {
public void toString(String s){
System.out.println("1111");
System.out.println(s);
}
}
class MyString2 extends MyString{
@Override
public void toString(String s){
String json_s = JSONParser.quote(s);
System.out.println(json_s);
}
}
package com.exercise.fifth;
import java.util.HashSet;
import java.util.Objects;
/**
* 作业题目3:
* 创建一个带id属性的实体类,覆写hashCode和equals方法;将实体类放入集合中,使相同id值得对象不重复;
* @author elliot
*/
public class ExerciseThird {
public static void main(String[] args){
HashSet map = new HashSet();
map.add(new MyClass(1));
map.add(new MyClass(2));
map.add(new MyClass(1));
System.out.println(map.size());
}
}
class MyClass{
public int id;
public MyClass(int id){
this.id = id;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyClass myClass = (MyClass) o;
return id == myClass.id;
}
@Override
public int hashCode() {
return Objects.hash(id);
}
}
package com.exercise.first;
/**
* 写个工具类,实现二进制、十进制、十六进制互转
* @author elliot
*/
public class ExerciseFifth {
public static void main(String[] args){
String binary = "10110100";
// 2 - 10
int decimal = binaryToDecimal(binary);
// 10 - 16
String hexadecimal = decimalToHexadecimal(decimal);
// 16 - 2
String firstBinary = hexadecimalToBinary(hexadecimal);
// 2 - 16
String newHexadecimal = binaryToHexadecimal(firstBinary);
// 10 - 2
String secondBinary = decimalToBinary(decimal);
// 16 - 10
int newDecimal = hexadecimalToDecimal(newHexadecimal);
}
/**
* 二进制转十进制
* @param binary
* @return
*/
public static int binaryToDecimal(String binary){
int decimal;
decimal = Integer.parseInt(binary, 2);
System.out.println(binary + " 对应的十进制的值为:" + decimal);
return decimal;
}
/**
* 二进制转十六进制
* @param binary
* @return
*/
public static String binaryToHexadecimal(String binary){
int decimal = binaryToDecimal(binary);
String hexadecimal;
hexadecimal = decimalToHexadecimal(decimal);
System.out.println(binary + " 对应的十六进制的值为:" + hexadecimal);
return hexadecimal;
}
/**
* 10进制转16进制
* @param decimal
* @return
*/
public static String decimalToHexadecimal(int decimal){
String hexadecimal;
hexadecimal = Integer.toHexString(decimal).toUpperCase();
System.out.println(decimal + " 对应的十六进制的值为:" + hexadecimal);
return hexadecimal;
}
/**
* 10进制转2进制
* @param decimal
* @return
*/
public static String decimalToBinary(int decimal){
String binaryString;
binaryString = Integer.toBinaryString(decimal);
System.out.println(decimal + " 对应的二进制的值为:" + binaryString);
return binaryString;
}
/**
* 16进制转10进制
* @param hexa
* @return
*/
public static int hexadecimalToDecimal(String hexa){
int decimal;
decimal = Integer.parseInt(hexa, 16);
System.out.println(hexa + " 对应的十进制的值为:" + decimal);
return decimal;
}
/**
* 16进制转2进制
* @param hexa
* @return
*/
public static String hexadecimalToBinary(String hexa){
int decimal = hexadecimalToDecimal(hexa);
String binaryString;
binaryString = Integer.toBinaryString(decimal);
System.out.println(hexa + " 对应的二进制的值为:" + binaryString);
return binaryString;
}
}
package com.exercise.first;
/**
* byte、int 与 二进制 互转,先将byte的127、128、-128、-128转二进制,再将这些二进制转int
* @author elliot
*/
public class ExerciseFirst {
/**
* 入口方法
* @param args 入口参数
*/
public static void main(String[] args) {
byte[] byteData = new byte[4];
byteData[0] = 127;
byteData[1] = (byte)128;
byteData[2] = -128;
byteData[3] = -127;
for (byte byteDatum : byteData) {
byteToBinaryToInt(byteDatum);
}
}
/**
* 二进制、int互转
* @param byteValue byte参数
*/
public static void byteToBinaryToInt(byte byteValue) {
String aBinary = byteToBinary(byteValue);
System.out.println(byteValue + " 的二进制为: " + aBinary);
int bInt = binaryToInt(aBinary);
System.out.println(aBinary + " 转int为:" + bInt);
}
/**
* 转二进制
* @param b byte参数
* @return 二进制
*/
public static String byteToBinary(byte b) {
String aBinary;
aBinary = Integer.toBinaryString((b & 0xFF) + 0x100).substring(1);
return aBinary;
}
/**
* 转int
* @param aBinary 二进制
* @return int值
*/
public static int binaryToInt(String aBinary) {
int bInt;
bInt = Integer.parseInt(aBinary, 2);
return bInt;
}
}
package com.exercise.first;
/**
* 写个工具类,实现ASCII和char互转
* @author elliot
*/
public class ExerciseFourth {
/**
* 入口方法
* @param args 参数
*/
public static void main(String[] args){
char aChar = 'H';
int aInt = charToAsc(aChar);
asciiToChar(aInt);
}
/**
* char 转对应的 ascii 值
* @param charStr
* @return
*/
public static int charToAsc(char charStr){
int charAsc;
charAsc = (int) charStr;
System.out.println(charStr + " 对应的ASCII为:" + charAsc);
return charAsc;
}
/**
* ascii 转换 对应的 char
* @param ascii
* @return
*/
public static char asciiToChar (int ascii){
char ch1;
ch1 = (char)ascii;
System.out.println(ascii + " 对应的char值为:" + ch1);
return ch1;
}
}
package com.exercise.first;
/**
* 阅读Integer内部类IntegerCache的源码
* @author elliot
*/
public class ExerciseSecond {
public static void main(String[] args) {
Integer h = 1;
Integer i = 1;
Integer j = 128;
Integer k = 128;
int a = valueOf(h);
int b = valueOf(i);
int c = valueOf(j);
int d = valueOf(k);
System.out.println(h == i);
System.out.println(j == k);
System.out.println(a == b);
System.out.println(c == d);
}
/**
* int对象类
* @param i int值
* @return
*/
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high) {
return IntegerCache.cache[i + (-IntegerCache.low)];
}
return new Integer(i);
}
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
int i = Integer.parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++) {
cache[k] = new Integer(j++);
}
// range [-128, 127] must be interned (JLS7 5.1.7)
assert high >= 127;
}
private IntegerCache() {}
}
}
package com.exercise.first;
import java.nio.charset.StandardCharsets;
/**
* 写个工具类,实现类型转换,String 转 (int、long、float、double、byte[]),(byte[]、int) 转 String
* @author elliot
*/
public class ExerciseThird {
/**
* 文件入口
* @param args 入口参数
*/
public static void main(String[] args) {
try {
String str = "456789324";
stringToInt(str);
stringToLong(str);
stringToFloat(str);
stringToDouble(str);
} catch (NumberFormatException e) {
System.out.println(e.getMessage());
}
}
/**
* string 转 int
* @param str
* @return
*/
public static int stringToInt(String str){
int strInt;
strInt = Integer.parseInt(str);
System.out.println(str + " 转成int为:" + strInt);
return strInt;
}
/**
* string 转 Long
* @param str
* @return
*/
public static long stringToLong(String str){
long strLong;
strLong = Long.parseLong(str);
System.out.println(str + " 转成long为:" + strLong);
return strLong;
}
/**
* string 转 float
* @param str
* @return
*/
public static float stringToFloat(String str){
float strFloat;
strFloat = Float.parseFloat(str);
System.out.println(str + " 转成float为:" + strFloat);
return strFloat;
}
/**
* string 转 double
* @param str
* @return
*/
public static double stringToDouble(String str){
double strDouble;
strDouble = Double.parseDouble(str);
System.out.println(str + " 转成double为:" + strDouble);
return strDouble;
}
/**
* string 转 byte[]
* @param str
* @return
*/
public static byte[] stringToBytes(String str){
byte[] byteArr = str.getBytes(StandardCharsets.UTF_8);
return byteArr;
}
/**
* byte[] 转 string
* @param bytes
* @return
*/
public static String byteArrToStringTo(byte[] bytes){
String string = new String(bytes);
return string;
}
/**
* int 转 string
* @param intNum
* @return String
*/
public static String intToString (int intNum){
return intNum + "";
}
}
package com.exercise.fourth;
import java.util.Scanner;
/**
* 作业题目4:
* 请定义一个登录接口的业务处理类,并编写一个login方法,来模拟处理如下登录异常情况:
* 1001-用户名或密码为空
* 1002-用户名不存在
* 1003-密码错误
* 1004-此帐号已锁定
* @author elliot
*/
public class ExerciseFifth {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入用户名");
String username = scanner.next();
System.out.println("请输入密码");
String pwd = scanner.next();
Login.userLogin(username, pwd);
}
}
class Login{
public static void userLogin(String userName, String pwd){
try {
System.out.println("用户登录中......");
if(userName.isEmpty() || pwd.isEmpty()){
throw new LoginException(1001, "用户名或密码为空");
}
if(!userName.equals("111111")){
throw new LoginException(1002, "用户名不存在");
}
if(!pwd.equals("123456")){
throw new LoginException(1003, "密码错误");
}
if(userName.equals("aaaa")){
throw new LoginException(1004, "此帐号已锁定");
}
}catch (LoginException exception){
System.out.println(exception.getMessage());
}
}
}
class LoginException extends RuntimeException{
private int errCode;
private String errMsg;
public LoginException(int errCode, String errMsg){
super(errMsg);
this.errCode = errCode;
this.errMsg = errMsg;
}
}
package com.exercise.fourth;
/**
* 作业题目1:
* 请写一段示例代码,演示如何在一个方法中捕获多个异常类型。
* @author elliot
*/
public class ExerciseFirst {
public static void main(String[] args){
try {
// do something
System.out.println("Hello world");
}catch (NullPointerException e){
System.out.println("NullPointerException do something");
}catch (ClassCastException e){
System.out.println("ClassCastException do something");
} finally {
System.out.println("finally do something");
}
}
}
package com.exercise.fourth;
/**
* 作业题目4:
* throw和throws的区别是什么?请写一段程序加以说明。
* 答:
* 1、throw 是语句抛出一个异常,一般是在代码块的内部。在方法参数列表后,throws后可以跟着多个异常名,表示抛出的异常用逗号隔开
* 2、throw 创建的是一个异常对象,throws表示向调用该类的位置抛出异常,不在该类解决
* 3、throw 确定了发生哪种异常才可以使用,可能发生哪种异常
* @author elliot
*/
public class ExerciseFourth {
public static void main(String[] args) throws RuntimeException,Exception{
try {
throw new RuntimeException("程序运行时异常");
}catch (RuntimeException e){
System.out.println(e.getMessage());
}
}
}
package com.exercise.fourth;
/**
* 作业题目2:
* 捕获异常代码块中,只有try/finally语句而没有catch语句是否可以,请编写一段函数加以验证。
* 答:可以
* @author elliot
*/
public class ExerciseSecond {
public static void main(String[] args){
try {
// do something
System.out.println("Hello world");
} finally {
System.out.println("finally do something");
}
}
}
package com.exercise.fourth;
/**
* 作业题目3:
* 对于finally语句段中的代码一定会执行吗?请编写一段程序进行验证。
* 答:不一定执行。
* 系统退出、线程中断、宕机等情况下不会执行
* @author elliot
*/
public class ExerciseThird {
public static void main(String[] args){
try {
// do something
System.out.println("Hello world");
System.exit(0);
} catch (RuntimeException e){
System.out.println("RuntimeException");
} finally {
System.out.println("finally do something");
}
}
}
package com.exercise.second;
/**
* 输出9*9乘法表
* @author elliot
*/
public class ExerciseFifth {
public static void main(String[] args) {
for(int i=1;i<=9;i++){
for(int j=1;j<=i;j++){
System.out.print(j+"*"+i+"="+i*j+'\t');
}
System.out.println();
}
}
}
package com.exercise.second;
/**
* 写一段会员打折的程序代码,规则如下:
* 1级会员消费满100打9折;
* 2级会员消费满100打8折,满200打7.5折
* 3级会员消费满100打7折,满200打6.5折,满300打6折;
* 非会员消费满500打9折;
* @author elliot
*/
public class ExerciseFirst {
private static final float AMOUNT_LIMIT_100 = 100f;
private static final float AMOUNT_LIMIT_200 = 200f;
private static final float AMOUNT_LIMIT_300 = 300f;
private static final float AMOUNT_LIMIT_500 = 500f;
public static void main(String[] args)
{
int level = 3;
float amount = 200f;
float discount = getMemberDiscountByLevelAndAmount(level, amount);
System.out.println(discount);
}
/**
* 根据会员等级和购买金额获取会员折扣
* @param level 会员等级
* @param amount 购买金额
* @return 会员折扣
*/
public static float getMemberDiscountByLevelAndAmount(int level, float amount)
{
float discount = 10f;
switch (level) {
case 1:
if (amount >= AMOUNT_LIMIT_100) {
discount = 9f;
}
break;
case 2:
if (amount >= AMOUNT_LIMIT_100 && amount < AMOUNT_LIMIT_200) {
discount = 8f;
}
if (amount >= 200) {
discount = 7.5f;
}
break;
case 3:
if (amount >= AMOUNT_LIMIT_100 && amount < AMOUNT_LIMIT_200) {
discount = 7f;
}
if (amount >= AMOUNT_LIMIT_200 && amount < AMOUNT_LIMIT_300) {
discount = 6.5f;
}
if (amount >= AMOUNT_LIMIT_300) {
discount = 6f;
}
break;
default:
if (amount >= AMOUNT_LIMIT_500) {
discount = 9f;
}
break;
}
return discount;
}
}
package com.exercise.second;
/**
* 判断101-200之间有多少个素数,并输出所有素数。
* 素数:一个大于1的自然数,除了1和它自身外,不能被其他自然数整除的数叫做素数
* @author elliot
*/
public class ExerciseFourth {
public static void main(String[] args) {
int count = 0;
for (int min = 101; min <= 200; min++) {
if (isPrime(min)) {
count ++;
System.out.println("素数 -- " + min);
}
}
System.out.println("素数的总个数为 -- " + count);
}
/**
* 是否素数
* @param number 待检查的数字
* @return boolean
*/
private static boolean isPrime(int number) {
double sqrt = Math.sqrt(number);
if (number < 2) {
return false;
}
if (number == 2 || number == 3) {
return true;
}
// 先判断是否为偶数,若偶数就直接结束程序
if (number % 2 == 0) {
return false;
}
for (int i = 3; i <= sqrt; i+=2) {
if (number % i == 0) {
return false;
}
}
return true;
}
}
package com.exercise.second;
import java.util.Random;
/**
* 写一段程序代码,利用Random类生成1个1000以内的随机数字,猜出这个数字,考虑尽可能的降低时间复杂度。生成随机数字代码如下:
* Random random = new Random();
* int num = random.nextInt(1000);
* @author elliot
*/
public class ExerciseSecond {
/**
* 最大猜测次数
*/
private static final int FIXED_COUNT = 10;
public static void main(String[] args) {
int min = 0;
int max = 1000;
Random random = new Random();
int fixedValue = random.nextInt(max);
int randomValue;
for (int i = 1; ; i++) {
randomValue = (max + min) / 2;
System.out.println("最小值 -- " + min + "--最大值 -- " + max + "--猜测值 -- " + randomValue + "--随机值 -- " + fixedValue);
if (randomValue < fixedValue) {
min = randomValue;
}
if (randomValue > fixedValue) {
max = randomValue;
}
if (i > FIXED_COUNT) {
System.out.println("猜了"+ i +"次了,还是没有猜中该随机数 -- " + fixedValue);
break;
}
if (randomValue == fixedValue) {
System.out.println("第" + i + "次猜中了,该随机数为:" + fixedValue);
break;
}
}
}
}
package com.exercise.second;
/**
* 写一段死循环程序代码,30s后跳出循环。
* @author elliot
*/
public class ExerciseThird {
public static void main(String[] args) {
int seconds = 30;
long start = System.nanoTime();
long end = start + (seconds) * 1000 * 1000000L;
System.out.println("进程开始时间是:" + start);
for (int i = 0;;i++){
if(System.nanoTime() >= end) {
long realEnd = System.nanoTime();
long a = (realEnd - start) / 1000000L;
System.out.println("进程结束时间是:" + realEnd + ",共进行了" + i + "次循环" + ",使用了" + a + "ms");
break;
}
}
}
}
package com.exercise.sixth;
/**
* 作业题目5:
* 字符串可以直接用加号进行拼接,但是也有几种不同的情况. 以下不同情况的拼接,分别创建了几个对象?
* 1) String str = "hello" + "world"; // 1个
* 2)String str1 = "hello"; String str2 = str1 + "world"; // 3个
* 3)String str1 = new String("hello"); String str2 = str1 + "world"; // 4个
* @author elliot
*/
public class ExerciseFifth {
public static void main(String[] args){
}
}
package com.exercise.sixth;
/**
* 作业题目1:
* 统计一个字符串里面另一个字符串出现的次数,
* 例如统计"monkey"在"I am monkey1024.monkey like banana.little monkey is smart."中出现的次数.
* @author elliot
*/
public class ExerciseFirst {
public static void main(String[] args){
String mainStr = "I am monkey1024.monkey like banana.little monkey is smart.";
String childStr = "monkey";
System.out.println("monkey出现的次数是:" + hasSubStringCount(mainStr, childStr));
}
/**
* 父字符串 中包含 子字符串的次数
* @param father 父字符串
* @param child 子字符串
* @return int
*/
public static int hasSubStringCount(String father, String child){
int count = 0;
while (father.indexOf(child) != -1) {
count++;
father = father.substring(father.indexOf(child) + child.length());
}
return count;
}
}
package com.exercise.sixth;
/**
* 作业题目4:
* String s = "113@ ere qqq yyui" 请输出所有子串
* 113
* ere
* qqq
* yyui
* @author elliot
*/
public class ExerciseFourth {
public static void main(String[] args){
String target = "113@ ere qqq yyui";
System.out.println("目标字符串:" + target);
String[] strList = target.split(" ");
for (int i = 0; i < strList.length; i++) {
System.out.println(strList[i].replace("@", " "));
}
}
}
package com.exercise.sixth;
/**
* 作业题目2:
* 编写程序将 “jdk” 全部变为大写,然后截取子串”DK”
* @author elliot
*/
public class ExerciseSecond {
public static void main(String[] args){
String target = "jdk";
System.out.println("目标字符串:" + target);
String upperTarget = target.toUpperCase();
System.out.println("目标字符串转大写:" + upperTarget);
String subUpperTarget = upperTarget.substring(1, 3);
System.out.println("目标字符串转大写后再截取后两位:" + subUpperTarget);
}
}
package com.exercise.sixth;
import java.util.ArrayList;
/**
* 作业题目3:
* "abcd23abcd34bcd"中,判断该字符串中是否包含"bc"子串。如果包含,求子串的所有出现位置。
* @author elliot
*/
public class ExerciseThird {
public static void main(String[] args){
String target = "abcd23abcd34bcd";
System.out.println("目标字符串:" + target);
String childStr = "bc";
Boolean isContains = target.contains(childStr);
System.out.println("字符串中是否包含'bc'子串:" + (isContains ? "是" : "否"));
if(isContains){
ArrayList map = subStringIndex(target, childStr);
String s = childStr + "出现的位置有:" + map;
System.out.println(s);
}
}
/**
* 父字符串 中包含 子字符串的位置
* @param father 父字符串
* @param child 子字符串
* @return int
*/
public static ArrayList subStringIndex(String father, String child){
ArrayList map = new ArrayList();
int temp = 0;
while (father.indexOf(child) != -1) {
map.add(father.indexOf(child) + temp);
temp += father.indexOf(child) + child.length();
father = father.substring(father.indexOf(child) + child.length());
}
return map;
}
}
package com.exercise.third;
/**
* 1. 求不同图形的周长
* Graph类包含一个求周长的抽象方法
* •圆
* •三角形
* @author elliot
*/
public class ExerciseFirst {
public static void main(String[] args){
// 计算一个圆形的周长
Double[] args1 = {0.01};
Circular circular = new Circular(args1);
System.out.println("这个圆的周长是:" + circular.calcGirth());
// 计算一个三角形形的周长
Double[] args2 = {0.01};
Triangle triangle = new Triangle(args2);
System.out.println("这个三角形形的周长是:" + triangle.calcGirth());
}
}
// 定义图形接口类,接口类中包含一个计算周长的方法
interface Graph {
public double calcGirth();
}
// 定义图形"圆",实现计算周长方法
class Circular implements Graph{
Double[] args;
public Circular(Double[] args){
this.args = args;
}
@Override
public double calcGirth(){
// todo calc 在这里用参数args计算出"圆"的周长
return 0.01D;
}
}
// 定义图形"三角形",实现计算周长方法
class Triangle implements Graph{
Double[] args;
public Triangle(Double[] args){
this.args = args;
}
// todo calc 在这里用参数args计算出"三角形"的周长
@Override
public double calcGirth(){
return 0.01D;
}
}
package com.exercise.third;
/**
* 4. 结合 static 和 final 关键字,利用接口做参数,写个计算器,能完成【加减乘除】运算
* (1)定义一个接口Compute含有一个方法int computer(int n,int m);
* (2)设计四个类分别实现此接口,完成【加减乘除】运算
* (3)设计一个类UseCompute,含有方法:public void useCom(Compute com, int one, int two)
* 此方法要求能够:
* 1.用传递过来的对象调用computer方法完成运算
* 2.输出运算的结果
* (4)设计一个测试类,调用UseCompute中的方法useCom来完成【加减乘除】运算
* @author elliot
*/
public class ExerciseFourth {
public static void main(String[] args){
UseCompute compute = new UseCompute();
Add add = new Add();
compute.useCom(add, 1, 2);
}
}
class UseCompute {
public void useCom(Compute com, int one,int two){
System.out.println(com.computer(one, two));
}
}
interface Compute{
/**
*
* @param n
* @param m
* @return
*/
public int computer(int n, int m);
}
/**
* 加
*/
class Add implements Compute{
@Override
public int computer(int n, int m){
return n + m;
}
}
/**
* 减
*/
class Subtraction implements Compute{
@Override
public int computer(int n, int m){
return n - m;
}
}
/**
* 乘
*/
class Mul implements Compute{
@Override
public int computer(int n, int m){
return n * m;
}
}
/**
* 除法
*/
class Div implements Compute{
@Override
public int computer(int n, int m){
return n / m;
}
}
package com.exercise.third;
import java.util.Scanner;
/**
* 2. 编写程序实现饮料购买:
* 编写程序,接收用户输入的信息,选择购买的饮料。
* 可供选择的饮料有:咖啡、矿泉水和可乐。
* 其中,购买咖啡时可以选择:加糖、加奶还是什么都不加。
* 购买可乐时可以选择:买可口可乐还是百事可乐
* @author elliot
*/
public class ExerciseSecond {
// 测试:购买咖啡,加奶
public static void main(String[] args){
System.out.println("请输入选择的饮料:咖啡 / 可乐");
Scanner scanner = new Scanner(System.in);
String name = scanner.next();
System.out.println("购买咖啡时可以选择:1、加糖、2、加奶3、什么都不加。购买可乐时可以选择:1、可口可乐 2、百事可乐");
int select = scanner.nextInt();
buyDrink(name, select);
}
// 去买饮料
public static void buyDrink(String name, int select){
switch (name){
case Coffee.name:
new Coffee(select).buy();
break;
case Coke.name:
new Coke(select).buy();
break;
default:
System.out.println("请选择您要买的饮料");
break;
}
}
}
/**
* 定义购买接口,需要选择喜好,并实现购买
*/
interface Buy{
/**
* 喜好、口味
*/
String FLAVOR = "";
/**
* 购买动作
*/
public void buy();
}
/**
* 抽象的料
*/
abstract class Drink{
public static final String name = "";
}
/**
* 咖啡
*/
class Coffee extends Drink implements Buy{
public static final String name = "咖啡";
String FLAVOR = "";
/**
* 咖啡可以选择的口味
*/
public static final String FLAVOR_SUGAR = "加糖";
public static final String FLAVOR_MILK = "加奶";
public static final String FLAVOR_NOTHING = "什么都不加";
public Coffee(int select){
switch (select){
case 1: // 可口可乐
this.FLAVOR = FLAVOR_SUGAR;
break;
case 2: // 加奶
this.FLAVOR = FLAVOR_MILK;
break;
case 3: // 什么都不加
default: // 默认什么都不加
this.FLAVOR = FLAVOR_NOTHING;
break;
}
}
// 买
@Override
public void buy(){
System.out.println("购买成功,您购买的是:" + this.FLAVOR + "的" + name);
}
}
/**
* 可乐
*/
class Coke extends Drink implements Buy{
public static final String name = "可乐";
String FLAVOR = "";
/**
* 咖啡可以选择的口味
*/
public static final String FLAVOR_COCO = "可口可乐";
public static final String FLAVOR_BAISHI = "百事可乐";
public Coke(int select){
switch (select){
case 1: // 可口可乐
this.FLAVOR = FLAVOR_COCO;
break;
case 2: // 百事可乐
default: // 默认百事可乐
this.FLAVOR = FLAVOR_BAISHI;
break;
}
}
// 买
@Override
public void buy(){
System.out.println("购买成功,您购买的" + name + "是:" + this.FLAVOR);
}
}
\ No newline at end of file
package com.exercise.third;
/**
* 3.多选题,
* public class Tetromino {
* protected int move(int x) {
* return 0;
* }
* }
* class T extends Tetromino {
* <插入代码>
* }
* 在<插入代码>处填入选项中的代码,使T类没有编译错误的是()
* A.public int move(int x) { return 0; }
* B.private int move(int x) { return 0; }
* C.private int move(long x) { return 0; }
* D.protected long move(int x) { return 0; }
* @author elliot
*/
public class ExerciseThird {
public static void main(String[] args) {
move(3);
System.out.println("没有编译错误的是 ( C )");
}
protected static int move(int x) {
return 0;
}
}
class Test extends ExerciseThird{
private int move(long x) { return 0; }
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment