Lab1 OOP Review 1
1.1
java
package Lab1;
import java.util.Arrays;
import java.util.Scanner;
public class ex11 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int length = 5;
double[] test1 =new double[length];
System.out.println("the length of input is"+length);
for (int i = 0; i < 5; i++) {
test1[i] = sc.nextDouble();
}
reverseInPlace(test1);
System.out.println(Arrays.toString(test1));
}
public static void reverseInPlace(double[] arr){
for (int i = 0; i < arr.length/2; i++) {
double temp = arr[i];
arr[i] = arr[arr.length-1-i];
arr[arr.length-1-i] = temp;
}
}
}
1.2
java
package Lab1;
import java.util.Arrays;
import java.util.Scanner;
public class ex12 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int length = 5;
int[] test =new int[length];
System.out.println("the length of input is"+length);
for (int i = 0; i < 5; i++) {
test[i] = sc.nextInt();
}
if (isConsecutiveFour(test)){
System.out.println(Arrays.toString(test));
}else {
System.out.println("no");
}
}
public static boolean isConsecutiveFour(int[] values){
for (int i = 0; i < values.length-3; i++) {
if (values[i] == values[i + 1] && values[i] == values[i + 2] && values[i] == values[i + 3]){
return true;
}
}
return false;
}
}
1.3
java
package Lab1;
public class ex13_StopWatch {
long startTime;
long endTime;
public ex13_StopWatch(){
this.startTime= System.currentTimeMillis();
this.endTime= System.currentTimeMillis();
}
public ex13_StopWatch(int startTime, int endTime) {
this.startTime = startTime;
this.endTime = endTime;
}
public void start(){
this.startTime = System.currentTimeMillis();
}
public void end(){
this.endTime = System.currentTimeMillis();
}
public long getElapsedTime(){
return this.endTime- this.startTime;
}
}
1.4
太常见了就不写了
Lab2 OOP Review 2
Lab3 Abstract Classes and Interfaces
3.1
java
package Lab3;
import java.util.ArrayList;
public class ex31 {
public static void main(String[] args) {
ArrayList<Number> arr = new ArrayList<>();
arr.add(1);
arr.add(2);
arr.add(3);
arr.add(4);
shuffle(arr);
System.out.println(arr.toString());
}
public static void shuffle(ArrayList<Number> list){
for (int i = 0; i < list.size(); i++) {
int index = (int) (Math.random()*list.size());
Number temp = list.get(i);
list.set(i, list.get(index));
list.set(index,temp);
}
}
}
3.2
java
package Lab3;
public class ex32 {
public static void main(String[] args) {
ComparableCircle c1 = new ComparableCircle(2);
ComparableCircle c2 = new ComparableCircle(4);
ComparableCircle ans = Max.max(c1, c2);
System.out.println(ans.getradius());
}
}
class Circle {
private double radius;
public Circle(double radius){
this.radius = radius;
}
public double getradius(){
return radius;
}
}
class ComparableCircle extends Circle implements Comparable{
public ComparableCircle(double radius){
super(radius);
}
@Override
public int compareTo(ComparableCircle c) {
if (getradius() > c.getradius()){
return 1;
}
else if (getradius() < c.getradius()){
return -1;
}
else{
return 0;
}
}
}
interface Comparable {
public abstract int compareTo(ComparableCircle c);
}
class Max{
public static ComparableCircle max(ComparableCircle c1, ComparableCircle c2){
if (c1.compareTo(c2)>=0){
return c1;
}
else return c2;
}
}
3.3
3.4
接口中的字段必须是常量(即 static final
),而不是实例字段。因此,String name;
应该改为 String NAME = "Animal";
或者直接在类中使用 String getName();
方法来获取名称。
java
public interface ex34_Animal {
String NAME = "Animal"; // 常量
void makesound(); // 抽象方法
}
接口中不能包含具有实现的方法。接口中的方法默认是抽象的,即没有具体的实现。
在Java中,抽象类是不能被直接实例化的,即不能使用 new
关键字来创建抽象类的对象。因此,当你尝试在 main
方法中实例化 ex34_Myclass
类的对象时,会导致编译错误。
Lab4 Generics
4.1
java
package Lab4;
import java.util.ArrayList;
public class ex41 {
public static void main(String[] args) {
ArrayList<Number> arr = new ArrayList<>();
arr.add(1);
arr.add(2);
arr.add(3);
arr.add(4);
shuffle(arr);
System.out.println(arr.toString());
}
public static <E> void shuffle(ArrayList<E> list){
for (int i = 0; i < list.size(); i++) {
int index = (int) (Math.random()*list.size());
E temp = list.get(i);
list.set(i, list.get(index));
list.set(index,temp);
}
}
}
4.2
java
package Lab4;
import java.util.ArrayList;
//这个地方卡了半天属于是没有看清楚要求,之前弄成ArrayList<Number>了,然后compareable里面估计就没办法兼容了
public class ex42 {
public static void main(String[] args) {
ArrayList<Integer> arr = new ArrayList<>();
arr.add(1);
arr.add(2);
arr.add(3);
arr.add(4);
Number maxnum = ex42.<Integer>max(arr);
System.out.println(maxnum);
}
public static <E extends Comparable<E>> E max(ArrayList<E> list){
E larger = list.get(0);
for (int i = 0; i < list.size(); i++) {
if (list.get(i).compareTo(larger)>0){
larger = list.get(i);
}
}
return larger;
}
}
4.3
java
public class ex43 {
public class pair <E,F>{
public E first;
public F second;
public pair(E e, F i) {
}
}
pair<String,Integer> p1 = new pair<>("hello",1);
}
4.4
java
package Lab4;
import java.util.ArrayList;
import java.util.Arrays;
public class ex44 {
public static void main(String[] args) {
ArrayList<Integer> c = new ArrayList<>();
c.add(3);
c.add(4);
print(c); //这里报红是因为泛型没有继承性
}
public static void print(ArrayList<? extends Object> o){ //所以要用泛型的通配符解决这个问题
for (Object e : o){
System.out.println(e);
}
}
}
Lab5 Lists, Stacks, Queues, and Priority Queues
5.1
java
package Lab5;
import java.util.Iterator;
import java.util.LinkedList;
public class ex51 {
public static void main(String[] args) {
LinkedList<Integer> ll = new LinkedList<>();
int num = 100000;
for (int i = 0; i < num; i++) {
ll.add(i);
}
long starttimer = System.currentTimeMillis();
// //i 遍历
// for (int i = 0; i < num; i++) {
// ll.get(i);
// }
//iterator遍历
for (Integer i : ll) {
System.out.println(" ");
}
long stoptimer = System.currentTimeMillis();
System.out.println("time" + (stoptimer - starttimer));
}
}
5.2
java
package Lab5;
import java.util.Arrays;
import java.util.PriorityQueue;
public class ex52 {
public static void main(String[] args) {
PriorityQueue<String> queue1 = new PriorityQueue<String>(Arrays.asList(
new String[] {"George","Jim","John", "Blake", "Kevin", "Michael"}
));
PriorityQueue<String> queue2 = new PriorityQueue<String>(Arrays.asList(
new String[] {"George","Katie","Kevin", "Michelle", "Ryan"}
));
//union
System.out.println("The union of the two priority queues is "+queue1);
// difference
queue1.removeAll(queue2);
System.out.println("The difference of the two priority queues is "+queue1);
//intersection
queue1.retainAll(queue2);
System.out.println("The intersection of the two priority queues is "+queue1);
}
}
5.3
java
package Lab5;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Stack;
//查看是否有成组的符号对
public class ex53 {
public static void main(String[] args) {
String str1 = "(int) ("0.1")";
String str2 = "public void static main(String[] args){";
System.out.println(checkpairs(str1));
System.out.println(checkpairs(str2));
}
public static boolean checkpairs(String str){
Stack<Character> s = new Stack<>();
int length = str.length();
boolean checkans = false;
for (int i = 0; i < length; i++) {
Character c = str.charAt(i);
if(c=='{' || c=='['||c=='('){
s.push(c);
}
else if(c=='}' || c==']'||c==')'){
checkidentity(s,c);
}
}
if (s.isEmpty()){
checkans = true;
}
return checkans;
}
public static void checkidentity(Stack<Character>s, char c){
char lastc = s.peek();
if (ispair(lastc,c)){
s.pop();
}
else {
s.push(c);
}
}
public static boolean ispair(char c1, char c2){
return c1 == '{' && c2 == '}' || c1 == '[' && c2 == ']'|| c1 == '(' && c2 == ')';
}
}