Java Experiment Report 3
Computer Science and technology
May 6
Class Triangle
Problem Description
Extend a class Triangle from GeometricObjec and satisfy the follow conditions:
- Three privatedouble
member represent three sides and the default length is 1.0;
- One None-parameter construction function;
- One Three-parameter construction function;
- OnegetArea()
function return the area.And onegetPerimeter()
return the perimeter;
- OnetoString()
function return the information of three sides;
- Test.
Code part 1 GeometricObject
public class GeometricObject {
private String color ="white";
private boolean filled;
private java.util.Date dateCreated;
public GeometricObject(){
dateCreated = new java.util.Date();
System.out.println("call GeometicObject construction function;");
}
public GeometricObject(String color,boolean filled){
dateCreated = new java.util.Date();
this.color=color;
this.filled=filled;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color=color;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled=filled;
}
public java.util.Date getDateCreated()
{
return dateCreated;
}
@Override
public String toString() {
return "created on" + dateCreated+"\ncolor:"+color+" and filled:"+filled;
}
}
Code part 2 Triangle
public class Triangle extends GeometricObject {
private double a=1.0;
private double b=1.0;
private double c=1.0;
public Triangle(){ System.out.println("call Triangle construction function;");}
public Triangle(double a,double b,double c)
{ super("black",true);
this.a=a;this.b=b;this.c=c;}
public double getArea(){
double p=(a+b+c)/2.0;
double s=Math.sqrt(p*(p-a)*(p-b)*(p-c));
return s;
}
public double getPerimeter(){return a+b+c;}
@Override
public String toString() {
return super.toString()+ "\nTriangle:side1="+a+" side2="+b+" side3="+c;
}
}
Code part 3 test
public class test {
public static void main(String[] args) {
GeometricObject test1= new Triangle();
Triangle test2 =new Triangle(3,4,5);
test2.setColor("blue");
test2.setFilled(true);
System.out.println(test2.toString());
}
}
Result
Delete Repeat Numbers
Problem Description
Write a method to delete the repeat numbers in Arraylist.
Code Part
import java.util.ArrayList;
import java.util.Scanner;
public class deleteRepeatNumber {
public static void removeDuplicate(ArrayList<Integer> list) {
for(int i=1;i<list.size();i++){
for(int j=0;j<i;j++){
if(list.get(i)==list.get(j)) {
list.remove(i);
i--;
break;
}
}
}
}
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
Scanner input = new Scanner(System.in);
int n = input.nextInt();
for(int i=0;i<n;i++){
list.add(input.nextInt());
}
removeDuplicate(list);
for(int i=0;i<list.size();i++){
System.out.print(list.get(i)+" ");
}
}
}
Result
AnomalyCapture
Problem Description
Write a program to practice capture exception.
- Create a list filled with random number which size = 100;
- Remind user input a index ,and output the list’s value.If the input is not a interger then output"Input Mismatch."
If the input is out of bounds then output "Out of Bounds."
- Do not use if
statement.
Code Part
import java.util.InputMismatchException;
import java.util.Scanner;
public class AnomalyCapture {
public static void main(String[] args) {
int[] a = new int[100];
for(int i=0;i<100;i++){
a[i]=(int)(Math.random()*10);
}
while(true) {
System.out.print("please input a index range(0~99):");
Scanner input=new Scanner(System.in);
try {
int index=input.nextInt();
System.out.println("output:" + a[index]);
} catch (InputMismatchException ex) {
System.out.println("Try again.Input Mismatch.");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Try again.Out of Bounds.");
}
}
}
}
Result
WebCrawler
Problem Description
Capture Xmu homepage’s infomation which called “Quick Links” .
Code Part
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;
public class CatpureWeb {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
ArrayList<String> list2 = new ArrayList<>();
try{
String urlString = new String("https://www.xmu.edu.cn");
java.net.URL url = new java.net.URL(urlString);
Scanner input = new Scanner(url.openStream());
String s = " target=\"_blank\"><i class=\"icon icon-list-alt\"></i>";
String s2= "<li><a href=\"";
String s3="</a></li>";
int current = -1 ,current2=-1;
while(input.hasNext()){
String line = input.nextLine();
current = line.indexOf(s,current);
current2 = line.indexOf(s2,current2);
while(current>0){
int endIndex1=line.indexOf(s3);
int startindex=line.indexOf(s2);
if(endIndex1>0){
list.add(line.substring(current+s.length(),endIndex1));
list2.add(line.substring(startindex+s2.length(),current-1));
current = line.indexOf(s2,endIndex1);
}
else current=-1;
}
}
}
catch (MalformedURLException ex){
ex.printStackTrace();
}
catch (IOException ex){
System.out.println("Can not open !");
}
for(int i=0;i<list.size();i++){
System.out.print("["+(i+1)+"] ");
System.out.print(list.get(i)+" ");
System.out.println(list2.get(i));
}
}
}
Result
Some Thoughts
secret