java程序类方法的使用_第1页
java程序类方法的使用_第2页
java程序类方法的使用_第3页
java程序类方法的使用_第4页
java程序类方法的使用_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

The Second Experiment Java Object-Oriented programmingQ1. Design a class named Point3D, which is the subclass of Point2D discussed in the class. Besides the coordinates variables, the methods that support coordinates setting/getting, distance calculation and coordinates shifting should be provided.Java codes:package oop_java;public class point2D protected double x;protected double y;void Point2D(double x2, double y2) x = 0;y = 0;point2D(double x,double y) this.x = x;this.y = y;/These are two constructors and they are used to initialize. public double Distance() return Math.sqrt(x*x+y*y);package oop_java;public class point3D extends point2D/The point3d have inherited point2D.private double z;point3D(double x, double y,double z) super(x, y);/Point3D invoked the constructor of point2D by using “super”.this.z=z;/The variable “z” need to be initialized in point3D.public void setxy(double x,double y,double z)this.x=x;this.y=y;this.z=z;/This is the method of setting.public double getx()return x;public double gety()return y;public double getz()return z;/These are methods of getting.public double Distance() return Math.sqrt(x*x+y*y+z*z);/The method of Distance can get the result of the distance of two points.public double Distance(double a,double b,double c)return Math.sqrt(a-x)*(a-x)+(b-y)*(b-y)+(c-z)*(c-z);/This is the overloading of Distance.We can get the distance of (x,y,z) and (a,b,c).public void symmetry(double h,double i,double j) x=-h; y=-i; z=-j;/It is the method to get the coordinates after symmetry by (0.0). public void shift(double d,double e,double f)x=x+d;y=y+e;z=z+f;package oop_java;public class usepoint public static void main(String args) point2D p1 = new point2D(2.3,4.1);System.out.println(the distance between p1 and (0.0) is + + p1.Distance();point3D p2=new point3D(3.2,2.1,5.2);/We can define a new object by using “new”.System.out.println(the distance between p2 and (0.0) is + + p2.Distance(); point3D p3=new point3D(2.1,3.4,1.9); System.out.println(the distance between p2 and p3 is + + p2.Distance(2.1,3.4,1.9); point3D p4=new point3D(2.0,3.0,4.0); p4.symmetry(2.0,3.0,4.0); System.out.println(the coordinates after symmetry is + +(+p4.getx()+,+p4.gety()+,+p4.getz()+);/The method symmetry doesnt have the result,so we can get the result by using getting ways. point3D p5=new point3D(2.1,3.5,8.4); p5.shift(2.0,2.0,2.0); System.out.println(the coordinates after shifting is + +(+p5.getx()+,+p5.gety()+,+p5.getz()+);Screen shots: Fig 1.1 Q2. Design a class that provides the methods below:2.1 check whether an integer is an even.2.2 check whether a value is positive.2.3 return the times that a specified substring appears in a string.2.4 sort an array by bubble and selection algorithms.2.5 Left shift an array by K bits, i.e., left shift the array 2,4,6,8 by 2 bits, the array becomes 6,8,2,4.2.6 right shift an array by K bits, just like check whether an integer is a palindrome.2.8 split a double value into its integral part and fractional part.Java codes:/this class includes many methods.package oop_java;public class some_methods int x=2;public void even(int x) if(x%2=0) System.out.println(x+ +is an even); else System.out.println(x+ +is not an even); /The even method is used to check whether a number is an even. public void positive(int y) if (y0) System.out.println(y+ +is an positive); else System.out.println(y+ +is not an positive); /The methods positive is used to check whether a number is positive. public int times(String s,String sub) int n=0;/n is the total number of all the times that the substring appears in the string S. int position=0; while (s.indexOf(sub,position)=0)/The method indexof returns the position where the sub string appears first. n+; position=s.indexOf(sub,position)+sub.length(); /Set the position to where the position of the sub string and add the length of the sub string. return n; /The method bubblesort is used to sort the array. public void bubblesort() int A=2,3,28,14,23,8,76,44; for (int i = 0; i 8; i+) for (int j = i; j Aj) int t = Ai; Ai = Aj; Aj = t; /Compare each two numbers and set the smaller on the front .Traverse the whole array. for (int a= 0;a 8; a+) System.out.print(Aa + ,);/Output the array. System.out.println(); /It is another way to sort the array. public void selectionsort() int B=12,3,22,4,55,18,39,6; for(int i=0;i8;i+) int min = i; for(int j=i+1;j8;j+) if(BjBmin) min = j; /Find the smallest number and put it in the first position.And find the smallest number in the rest number,and so on. int t= Bi; Bi = Bmin; Bmin=t; System.out.println(selection sort); for(int a=0;aB.length;a+) System.out.print(Ba+ ); /Output the array. System.out.println(); /This method is used to check whether a number is a palindrome.If it is a palindrome,return true. public boolean palindrome(int number) String num=String.valueOf(number);/The method of valueof is used to translate the string to number. return new StringBuffer(num).reverse().toString().equalsIgnoreCase(num); /If the flip of the number equals to the number itself,it is a palindrome. public void spilt( String s1) String str=234.45; String ss=str.split(.); for(String s:ss) System.out.println(s); /The method spilt is used to separate a double number into integral part and fractional part. public void shift() x=this.x;/The x is the number that the array will shift. int a=new int 5; a=new int2,4,6,8,10; for(int i=0;i5;i+) System.out.print(ai+);/Output the known array. System.out.println(); System.out.println(shift to left.); for(int i=0;i5;i+) if(i+1+x5) System.out.print(ai+x+1+);/First,output the last two numbers. else System.out.print(ax-4+i+);/Then output the first 5-x numbers. System.out.println(); System.out.println(shift to right.); for(int i=0;i5;i+) if(i+x5) System.out.print(ai+x+);/First output the numbers except the first 2 numbers. else System.out.print(ai-x-1+);/Then output the first 2 numbers. package oop_java;public class usesomemethod /Invoke all the methods in the main method.public static void main(String args) some_methods A=new some_methods();/Produce a instance by the class.A.even(131);A.positive(-32);String s=aababbabab;String sub=ab;System.out.println(the sub string appears+ +A.times(s,sub)+ times);A.bubblesort(); A.selectionsort(); System.out.println(1234321 is a palindrome is + A.palindrome(1234321); String s1=234.45; A.spilt(s1); A.shift(); Screen shots: Fig 2.1Q3. Design a class named student, and use List, ArrayList and Iterator to traverse all the students records.Java codes:package oop_java;public class student /Define all the variables.private String id;private String name;private int age;private String major;/Define all the setters and getters.public String getId() return id;/The getters have no parameter.public void setId(String id) this.id = id;/The setters have parameters.public String getName() return name;public void setName(String name) = name;public int getAge() return age;public void setAge(int age) this.age = age;public String getMajor() return major;public void setMajor(String major) this.major = major;package oop_java;import java.util.*;public class usestudent public static void main(String args) List v = new ArrayList();/Define a arraylist class to implement the interface of list.boolean done = true;Scanner sc = new Scanner(System.in);/Define a instance to input the record.System.out.println(please input students records:);while(done)student stu = new student();System.out.print(ID:);stu.setId(sc.next();System.out.print(Name:);stu.setName(sc.next();System.out.print(Age:);stu.setAge(sc.nextInt();System.out.print(Major:);stu.setMajor(sc.next();v.add(stu);System.out.print(Need add another student?(Y/N)?);if(sc.next().equals(N)done=false;/We use a condition to control the input.If the condition is met,We will continue to input the record.sc.close();System.out.println(students number: + v.size();/Output the size of the record.System.out.println(all students record by Iterator:);Iterator it = v.iterator();/Produce a iterator by the interface of Iterator.while(it.hasNext() /Check whether there is a record in the list by “it,hasNext”.student sr = it.next();System.out.println(ID: + sr.getId();System.out.println(Name: +sr.getName();System.out.println(Age: +sr.getAge();System.out.println(Major: +sr.getMajor();Screen shots: Fig 3.1Q4. Design a class named AddressBook, includes name, cell phone number, company and gender, and their setters/getters, use List, ArrayList and Iterator to:4.1 insert a new record.Screen shots: Fig 4.14.2 delete all the records named Jack.Screen shots: Fig 4.24.3 update the cell phone number toif the records name is Mary.Screen shots: Fig 4.34.4 output all the records which cell phone numbers take 139 as the prefix and names take M as the prefix.Screen shots: Fig 4.4Java codes:package oop_java;public class AddressBook /Define four variables.private String name;private String phonenum ; private String company;private String gender;/These are setters and getters.public String getName() return name;public void setName(String name) = name;public String getphonenum() return phonenum;public void setphonenum(String phonenum) this.phonenum =phonenum;public String getgender() return gender;public void setgender(String gender) this.gender = gender;public String getcompany() return company;public void setcompany(String company) pany = company;package oop_java;/Import all the packages you will use.import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.Scanner;public class useAddress public static void main(String args) List v = new ArrayList();/Define a arraylist class to implement the interface of list.Scanner sc = new Scanner(System.in);System.out.println(please input a new records:);AddressBook ab = new AddressBook();System.out.print(name);ab.setName(sc.next();System.out.print(phonenum);ab.setphonenum(sc.next();System.out.print(company);ab.setcompany(sc.next();System.out.print(gender);ab.setgender(sc.next();v.add(ab);sc.close();System.out.println(all the record by Iterator:);Iterator it = v.iterator();/Produce a iterator by the interface of Iterator. while(it.hasNext() AddressBook s = it.next();System.out.println(name + s.getName();System.out.println(phonenum +s.getphonenum();System.out.println(company +s.getcompany();System.out.println(gender +s.getgender();it = v.iterator();/Use iterator to traverse the array.while(it.hasNext() if(it.next().getName().equals(Jack)it.remove();/When the name is Jack,delete its record.Iteratora= v.iterator();a=v.iterator();while(a.hasNext() if(a.next().getName().equals(Mary)ab.setphonenum; ab.getphonenum();/When the name is Mary,invoke the setter and set her phone number to 1388888888. Iteratorb= v.iterator(); b=v.iterator(); while(b.hasNext() if(b.next().getName().startsWith(M, 1) &b.next().getphonenum().startsWith(139)/When name starts with M and the phone number starts with 139,output all his or her record. for(int i=0;iv.size();i+)System.out.println(name +v.get(i).getName();System.out.println(phonenum +v.get(i).getphonenum();System.out.println(company +v.get(i).getcompany();System.out.println(gender +v.get(i).getgender(); System.out.println(all the record by for:);for(int i=0;iv.size();i+)System.out.println(name +v.get(i).getName();System.out.println(phonenum +v.get(i).getphonenum();System.out.println(company +v.get(i).getcompany();System.out.println(gender +v.get(i).getgender();/Output the result by for cycle.Q5. Design an interface named Shape, which has a variable named shapeName, and two methods named perimeter and area, design two classes named rectangle and ellipse respectively to implement this interface.Java codes:package oop_java;public interface Shape final static String retangle=retangle;final static String ellipse =ellipse;public abstract void setShapeName(String shapeName);public abstract double ComputePerimeter();public abstract double ComputeArea();/Define a interface,and it includes three abstract methods.package oop_java;public class rectangle implements Shape/Define a rectangle class to implement the interface.private double x,y;private String ShapeName;rectangle(double x,double y)this.setSides(x,y);this.setShapeName(rectangle);private void setSides(double x, double y) if(x=0|y=0)System.out.println(the input is wrong); return;/The two sides of rectangle is positive.this.x=x;this.y=y;public double ComputePerimeter()return 2*(x+y);/Compute the perimeter of rectangle.public double ComputeArea()return x*y;/Compute the area of rectangle.public void setShapeName(String shapeName) this.ShapeName=rectangle;/Set its name.package oop_java

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论