本文目录一览:
- 1、编写Java程序,用来描述动物园里的猴子。要求如下 (1) 创建一个Monkey(猴子)类,其属性包括颜色和性别
- 2、Java程序编写宠物类并封装
- 3、java编程几个类,可以实现动物园中几个科下动物的增删改查
- 4、编写动物乐园java,满意会加分的!!!
- 5、如何用Java程序编程,最好讲解一下。题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对
- 6、有一个java的编程题。编写一个DOG类,它有名字(name)颜色(color)和年龄(age)三个属性
编写Java程序,用来描述动物园里的猴子。要求如下 (1) 创建一个Monkey(猴子)类,其属性包括颜色和性别
public class Monkey {
private String color;
private String sex;
public Monkey(String color,String sex) {
this.color = color;
this.sex = sex;
}
public Monkey(String color) {
this.color = color;
this.sex = "公";
}
public Monkey(){}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int add(int p1,int p2,int p3) {
return p1+p2+p3;
}
public int add(int p1,int p2) {
return p1+p2;
}
}
public class Zoo {
public static void main(String[] args) {
Monkey g = new Monkey("黑");
Monkey l = new Monkey("红","母");
int p1 = g.add(1, 2);
int p2 = l.add(1, 2, 3);
System.out.println(g.getSex()+":"+p1);
System.out.println(l.getSex()+":"+p2);
}
}
Java程序编写宠物类并封装
public class Pet {
//pet的属性
private String name;
private int age;
private char gengder;
//提供无参构造方法
public Pet() {
super();
}
//提供有参构造方法
public Pet(String name, int age, char gengder) {
super();
this.name = name;
this.age = age;
this.gengder = gengder;
}
//提供get/set方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public char getGengder() {
return gengder;
}
public void setGengder(char gengder) {
this.gengder = gengder;
}
//重写tostring方法
@Override
public String toString() {
return "Pet [name=" + name + ", age=" + age + ", gengder=" + gengder + "]";
}
}
封装的基本语法:
属性私有化
提供无参和有参构造方法
提供get/set方法
重写tostring方法
java编程几个类,可以实现动物园中几个科下动物的增删改查
import java.util.ArrayList;
import java.util.List;
class Animal {
public String name;
public String color;
public String toString() {
return " Name: " + this.name + " Color: " + this.color;
}
/**
* 动物吃东西
*
* @param foodName
* 食物名称
*/
public void eat(String foodName) {
}
}
class Dog extends Animal {
public Dog() {
}
public Dog(String name, String color) {
this.name = name;
this.color = color;
}
public void eat(String foodName) {
System.out.println("Dog: " + this.name + " like to eat: " + foodName);
}
}
class Cat extends Animal {
public Cat() {
}
public Cat(String name, String color) {
this.name = name;
this.color = color;
}
public void eat(String foodName) {
System.out.println("Cat: " + this.name + " like to eat: " + foodName);
}
}
class Tiger extends Animal {
public Tiger() {
}
public Tiger(String name, String color) {
this.name = name;
this.color = color;
}
public void eat(String foodName) {
System.out.println("Tiger: " + this.name + " like to eat: " + foodName);
}
}
class Lion extends Animal {
public Lion() {
}
public Lion(String name, String color) {
this.name = name;
this.color = color;
}
public void eat(String foodName) {
System.out.println("Tiger: " + this.name + " like to eat: " + foodName);
}
}
public class Zoo {
public static ListAnimal zooList = new ArrayListAnimal();
/**
* 添加Animal对象
*
* @param obj
*/
public void addAnimal(Animal obj) {
zooList.add(obj);
}
/**
* 根据animal的名字删除对象
*
* @param obj
* Animal对象
*/
public void deleteAnimal(Animal obj) {
boolean deleteFlag = false;
for (int index = 0; null != zooList index zooList.size(); index++) {
if (zooList.get(index).name.equals(obj.name)) {
zooList.remove(index);
deleteFlag = true;
System.out.println("删除: " + obj + " 成功");
}
}
if (!deleteFlag) {
System.out.println("找不到该动物: " + obj);
}
}
/**
* 更新Animal对象信息
*
* @param obj
* Animal对象
*/
public void updateAnimal(Animal obj) {
boolean flag = false;
for (int index = 0; null != zooList index zooList.size(); index++) {
if (zooList.get(index).name.equals(obj.name)) {
zooList.get(index).name = obj.name;
zooList.get(index).color = obj.color;
flag = true;
System.out.println("\n修改成功");
}
}
if (!flag) {
System.out.println("找不到该动物: " + obj);
}
}
/**
* 根据动物名字查询
*
* @param animalName
* 动物名字
*/
public void checkAniaml(String animalName) {
boolean flag = false;
for (int index = 0; null != zooList index zooList.size(); index++) {
if (zooList.get(index).name.equals(animalName)) {
System.out.println(zooList.get(index));
flag =true;
}
}
if (!flag) {
System.out.println("找不到该动物: " + animalName);
}
}
public void display(ListAnimal list) {
for (int index = 0; null != list index list.size(); index++) {
System.out.println("\n" + list.get(index));
}
}
public static void main(String[] args) {
Zoo zoo = new Zoo();
zoo.addAnimal(new Dog("dog1", "black"));
zoo.addAnimal(new Dog("dog2", "black"));
zoo.addAnimal(new Lion("Lion1", "black"));
zoo.addAnimal(new Lion("Lion2", "black"));
zoo.addAnimal(new Cat("cat1", "yellow"));
zoo.addAnimal(new Cat("cat2", "yellow"));
zoo.addAnimal(new Tiger("Tiger1", "yellow"));
zoo.addAnimal(new Tiger("Tiger2", "yellow"));
zoo.updateAnimal(new Dog("dog1","somethingselse"));
zoo.display(zooList);
zoo.deleteAnimal(new Dog("dog2",""));
zoo.display(zooList);
zoo.checkAniaml("Lion1");
}
}
编写动物乐园java,满意会加分的!!!
public class Animal
{
public String name;
public int legs;
public String sound;
public Animal(String name, int legs, String sound){
this.name = name;
this.legs = legs;
this.sound = sound;
}
public String toString(){
return name + "\t" + legs + "\t" + sound;
}
public static void main(String[]args)
{
//init
Animal[] animals = new Animal[3];
animals[0] = new Animal("加菲猫",4, "喵喵喵");
animals[1] = new Animal("唐小鸭",2, "嘎嘎嘎");
animals[2] = new Animal("海豚奇奇",0, "海豚音");
byte[] b = new byte[1024];
String[] names = {"猫", "鸭子", "海豚"};
int[] legs = {4,2,0};
String name;
int leg;
String sound;
while(true){
try{
System.out.println("动物名称\t腿的条数\t动物叫");
for (int i = 0; i animals.length; i++) {
System.out.println(animals[i].toString());
}
System.out.println("0-修改,其他数字-退出");
int n = System.in.read(b);
String s = new String(b,0,n);
int oper = Integer.parseInt(s.trim());
if (oper != 0) {
System.out.println("退出!");
return;
}
for (int i = 0; i animals.length; i++) {
System.out.println("请输入"+names[i]+"的名字");
n = System.in.read(b);
s = new String(b,0,n);
name = s.trim();
if (name.length() == 0) {
throw new Exception(names[i]+"必须有名字");
}
System.out.println("请输入"+names[i]+"的腿数");
n = System.in.read(b);
s = new String(b,0,n);
leg = Integer.parseInt(s.trim());
if (leg != legs[i]) {
throw new Exception(names[i]+"必须有"+legs[i]+"条腿");
}
System.out.println("请输入"+names[i]+"的叫声");
n = System.in.read(b);
s = new String(b,0,n);
sound = s.trim();
if (sound.length() == 0) {
throw new Exception(names[i]+"必须有声音");
}
animals[i].name = name;
animals[i].legs = leg;
animals[i].sound = sound;
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
}
凑合写的....
instanceof 不难吧...就是验证类是否为类或者父类的实例...
我看你ppt要求的是自定义异常抛出,接口继承应该很简单,抛出异常参考我的代码即可..
如何用Java程序编程,最好讲解一下。题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对
这道题目考察的是运用递归(数列)的思路去解决问题。
假设到第24个月,示例代码如下:
public class woo {
public static void main(String args[]) {
System.out.println(fib(24));
}
private static int fib(int n) {
if (n == 1 || n == 2) {
return 1;
} else {
return fib(n - 1) + fib(n - 2);
}
}
}
扩展资料:
斐波那契数列(Fibonacci sequence),又称黄金分割数列、因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为“兔子数列”,指的是这样一个数列:1、1、2、3、5、8、13、21、34、……。
在数学上,斐波纳契数列以如下被以递推的方法定义:F(1)=1,F(2)=1, F(n)=F(n-1)+F(n-2)在现代物理、准晶体结构、化学等领域,斐波纳契数列都有直接的应用,为此,美国数学会从1963年起出版了以《斐波纳契数列季刊》为名的一份数学杂志,用于专门刊载这方面的研究成果。
参考资料:
百度百科:斐波那契数列
百度百科:递归函数
有一个java的编程题。编写一个DOG类,它有名字(name)颜色(color)和年龄(age)三个属性
Dog.java
public class Dog {
private String name;
private String color;
private int age;
public Dog() {
}
public Dog(String name, String color, int age) {
this.name = name;
this.color = color;
this.age = age;
}
public void show() {
System.out.println("name:" + this.name);
System.out.println("color:" + this.color);
System.out.println("age:" + this.age);
}
}
DogTest.java
public class DogTest {
public static void main(String[] args) {
Dog dog1 = new Dog("大黑", "黑", 2);
Dog dog2 = new Dog("小白", "白", 1);
dog1.show();
dog2.show();
}
}
// try...