面相对象第七次作业
题目1:在作业5的基础上,再创建一个柱体类,包含矩形对象、高和体积等三个成员变量,一个构造方法进行成员变量初始化,和计算体积、换底两个功能方法,在主类中输入长、宽、高,计算柱体体积,输入新的长、宽、高,创建新的矩形对象,并利用换底方法换底,再次计算柱体体积。
package rectanglecall;
public class Square{
private int length,width;
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
}
package rectanglecall;
public class Rectangle {
private int length,width;
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
}
package rectanglecall;
public class compare {
Square square;
public compare(){
square.setLength(21);
square.setWidth(2);
}
}
package rectanglecall;
import java.util.Scanner;
public class compare0 {
Rectangle rectangle;
public compare0(){
rectangle.setLength(20);
rectangle.setWidth(4);
}
}
package rectanglecall;
public class finall {
static Rectangle rectangle;
static Square square;
static double size;
static int high;
public finall(){
high=4;
}
public static void main(String[] args) {
size=rectangle.getLength() * rectangle.getWidth()*high;
System.out.println(size);
size=square.getLength() * square.getWidth()*high;
System.out.println(size);
}
}
题目2:设计名为MyInteger的类,它包括: int型数据域value 一个构造方法,当指定int值时,创建MyInteger对象 数据域value的访问器和修改器 isEven( )和isOdd( )方法,如果当前对象是偶数或奇数,返回true 类方法isPrime(MyInteger i),判断指定的值是否为素数,返回true 在主类中创建MyInteger对象,验证MyInteger类中各方法。
package myinteger;
/**
*
* @author shinan
*/
public class MyInteger {
/**
* @param args the command line arguments
*/
int value;
public MyInteger(int value){
this.value=value;
}
public int getValue(){
return value;
}
public boolean isEven(){
if(value%2==0)
return true;
else
return false;
}
public boolean isOdd(){
if(value%2!=0)
return true;
else
return false;
}
public boolean isPrime(){
for(int x=2;x<value-1;x++){
if(value%x==0)
return false;
}
return true;
}
public boolean isEven(int value){
if(value%2==0)
return true;
else
return false;
}
public boolean isOdd(int value){
if(value%2!=0)
return true;
else
return false;
}
public boolean isPrime(int value){
for(int x=2;x<value-1;x++){
if(value%x==0)
return false;
}
return true;
}
public boolean isEven(MyInteger myinteger ){
if(myinteger.getValue()%2==0)
return true;
else
return false;
}
public boolean isOdd(MyInteger myinteger){
if(myinteger.getValue()%2!=0)
return true;
else
return false;
}
public boolean isPrime(MyInteger myinteger){
for(int x=2;x<myinteger.getValue()-1;x++){
if(myinteger.getValue()%x==0)
return false;
}
return true;
}
public boolean equals(int value){
return true;
}
public boolean equals(MyInteger myinteger){
return this.value == myinteger.getValue();
}
public static int parseInt(char []c){
return Integer.valueOf(new String(c));
}
public static int parseInt(String s){
return Integer.valueOf(s);
}
public static void main(String[] args) {
// TODO code application logic here
String s = "123";
char[] a ={'1','2'};
MyInteger i1 = new MyInteger(3);
MyInteger i2 =new MyInteger(4);
System.out.println(i1.isEven());
System.out.println(i1.isOdd());
System.out.println(i1.isPrime());
System.out.println(i1.isPrime(i2));
System.out.println(i1.isEven(i2));
System.out.println(i1.isOdd(i
2));
System.out.println(i1.equals(5));
System.out.println(i1.equals(i2));
System.out.println(i1.parseInt(s));
System.out.println(i1.parseInt(a));
}
}