P255Java练习
题目4,编写类A03,实现数组的赋值功能copyArr,输入旧数组,返回一个新数组,元素和旧数组一样
public class Tets255Homework04 {
public static void main (String[] args) {
A03 t=new A03();
int [] a={1,2,3,4,5};
int [] a1=t.copyArr(a);
for(int i=0;i<a1.length;i++){
System.out.println(a1[i]);
}
}
}
class A03{
public int [] copyArr(int[] arr){
//在堆里开空间
int [] arr1=new int[arr.length];
for(int i=0;i<arr.length;i++){
arr1[i]=arr[i];
}
return arr1;
}
}
题目5定义一个圆类Circle,定义属性半径,提供显示圆周长的功能和方法,提示显示圆面积的方法
public class Tets255Homework05 {
public static void main (String[] args) {
Circle c=new Circle (10);
System.out.println(c.getCirfer());
System.out.println(c.getCirar());
}
}
class Circle{
int radius ;
public Circle(int radius){//构造器
this.radius =radius;
}
public double getCirfer(){
return 2*radius*3.14;
}
public double getCirar(){
return Math.PI*radius*radius;
}
}
题目6,创建一个Cale计算类,在其中两个定义两个变量表示两个操作数,定义四个方法实现求和,差,乘,除(若除数为要提示),并创建两个对象分别进行测试
public class Tets255Homework04 {
public static void main (String[] args) {
Cale p1 = new Cale(9.0,0.0);
System.out.println(p1.sum());
System.out.println(p1.minus());
System.out.println(p1.nul());
Double divRes =p1.div();
if(divRes !=null){
System.out.println(divRes);
}
}
}
class Cale{
double ao;
double bo;
public Cale(double ao,double bo){//构造器
this.ao=ao;
this.bo=bo;
}
public double sum(){
return ao+bo;
}
public double minus(){
return ao-bo;
}
public double nul(){
return ao*bo;
}
public Double div(){//包
if(bo ==0){
System.out.println("bo不能为0");
return null;
}else{
return ao/bo;
}
}
}
题目7设计一个Dog类,属性name,age,color,输出show()方法,显示其信息,并创建对象进行测试
public class Tets255Homework04 {
public static void main (String[] args) {
Dog dog1=new Dog(1,"aa","blue");
dog1.show();
Dog dog2=new Dog(2,"ab","green");
dog2.show();
}
}
class Dog{
int age;
String name;
String color;
public Dog(int age,String name,String color){
this.age=age;
this.name=name;
this.color=color;
}
public void show(){
System.out.println("age= "+age+" name= "+name+" color="+color);
}
}
输出下列结果(10,9,10)
public class PTest258{//公有类
int count=9;//属性
public void count1(){
count=10;//就近原则,将count=9改成10
System.out.println("count1="+count);//10
}
public void count2(){
System.out.println("count1=" + count++);//9先赋值再自增得到10,此时t1=10;
}
//PTest258类的main方法,任何一个类,都可能有main
public static void main(String args[]){
//1.new Test()是匿名对象(使用后就不能用了)
2.new.Test().count1()调用属性
new PTest258().count1();
PTest258 t1= new PTest258();//t1的count=9
t1.count2();//输出9,此时t1=10;
t1.count2();//再调用t1.count2,输出10,此时再自增
}
}
题目9定义Music类,里面有name,时长times,播放功能play 返回本身属性信息的方法getInfo()
public class Tets255Homework04 {
public static void main (String[] args) {
Music m=new Music("武林外传",20);
System.out.println(m.getInfor());
}
}
class Music{
String name;
int times;
public Music(String name,int times){
this.name=name;
this.times=times;
}
public void Play(){
System.out.println("音乐name"+name+"播放时常"+times+"秒");
}
public String getInfor(){
return "音乐name"+name+"播放时间"+times;
}
}
题目10找出如下代码的输出
class Demo{
int i=100;
public void m(){
int j=i++;
System.out.println("i="+i);//输出101
System.out.println("j="+j);//输出100
}
}
class Test{
public static void main(String [] args){
Demo d1 =new Demo();
Demo d2=d1;//引用传递
d2.m();//调用赋值
System.out.println(d1.i);//输出101
System.out.println(d2.i);//输出101
}
}
题目11,调用method方法,语句如下 System.out.println(method(method(10.0,20.0),100);
写出它的方法定义
class M{
double n1;
double n2;
public M(double n1,double n2){
this.n1=n1;
this.n2=n2;
}
public double Method(){
return n1+n2;
}
}
题目12建Employee类,属性name,gender,age,posiotion salary,3个构造方法,
可以初始化(1)name,gender,age,posiotion salary,(2)name,gender,age(3)posiotion salary,
要用复合符合构造器
class Employee{
String name;
char gender;
int age;
String position;
double salary;
public Employee(String name,char gender,int age){
this.name=name;
this.gender=gender;
this.age=age;
}
public Employee(String position,double salary){
this.position =position;
this.salary=salary;
}
public Employee(String name,char gender,int age,String position,double salary){
this.(name,gender,age)//this调用构造器只能在第一行语句
this.position =position;
this.salary=salary;
}
题目13,将对象作为参数传递给方法
(1)定义一个Circle类,包含double型和属性radius半圆,findArea()方法返回圆的面积
(2)定义一个PassObject类,在类中定义一个方法printAreas(),该方法定义如下:public void printAreas(Cicle c,int times)
(3)在printAreas()方法中打印输出1到times只见的每个整数的半径值,以及对应面积,例如times=5,输出半径1,2,3,4,5以及对应的圆面积
(4)在main 方法中调用printAreas()方法,调用完毕后输出当前半径值,Radius Area (运用Math.PI)
public class P261Test{
public static void main (String[] args) {
Circle c1=new Circle();
Pt p = new Pt();
p.printAreas(c1,5);
}
}
class Pt{
public void printAreas(Circle c,int times){
System.out.println("Radius"+"\t"+"\t"+"Area");
for(double i=1;i <= times;i++){
c.setRadius(i);//我直接用c.radius=i,这样每次都new了一个对象
System.out.println(i+"\t"+"\t"+c.findArea());
}
}
}
class Circle{
double radius;
public double findArea(){
return Math.PI*radius*radius;
}
public double setRadius(double radius){//设置一个方法,每次修改半径
return this.radius=radius;
}
}
题目14Tom与电脑猜拳(0表示石头,1代表剪刀,2代表步),计算Tom获胜次数
自己写的代码,0、1、2的概率有点问题
public class P261Test{
public static void main (String[] args) {
Game g =new Game();
Tom t = new Tom();
int count=0;
for(int i=0;i<3;i++){//Tom获胜情况count,暂定执行3次
if(g.game()== 0 && t.game()==2){
count++;
}else if(g.game() == 1 && t.game()==0){
count++;
}else if(g.game()==2 && t.game()==1){
count++;
}else{
}
}
System.out.print(count);
}
}
class Game{//电脑出拳
public int game(){
return (int) (Math.random()+ Math.random());
}
}
class Tom//Tom出拳
public int game(){
return (int) (Math.random()+ Math.random());
}
}