这两道题代码怎么写java? java面试代码题
求做两道java题目
6.class Animal {
public double weight = 0;
public int age = 0;
public Animal(){
}
public void eat(){
System.out.println("Animal eat!");
}
public void sleep(){
System.out.println("Animal sleep!");
}
public void breath(){
System.out.println("Animal breath!");
}
}
public class Fish extends Animal{
public Fish() {
}
public Fish(double weight, int age) {
this.weight = weight;
this.age = age;
}
public void breath(){
System.out.println("Fish breath!");
}
public static void main(String[] args) {
Fish fish = new Fish();
fish.eat();
fish.breath();
fish.sleep();
Fish fish2 = new Fish(2.85, 2);
fish.eat();
fish.breath();
fish.sleep();
}
}
结果:
Animal eat!
Fish breath!
Animal sleep!
Animal eat!
Fish breath!
Animal sleep!
7.
abstract class Animal {
abstract public void sleep();
public void goSleep(){
sleep();
}
}
class Bird extends Animal {
public void sleep() {
System.out.println("a bird sleeps in tree");
}
}
class Person1 extends Animal {
public void sleep() {
System.out.println("a bird sleeps in bed");
}
}
public class Fish extends Animal{
public void sleep() {
System.out.println("a bird sleeps in water");
}
public static void main(String[] args) {
Bird a = new Bird();
a.goSleep();
Fish fish = new Fish();
fish.goSleep();
Person1 c = new Person1();
c.goSleep();
}
}
结果:
a bird sleeps in tree
a bird sleeps in water
a bird sleeps in bed
java中这道题是这样的:随机输入一个姓名,然后拆分出名和姓再分别输出? 谢谢指导
代码如下:
import java.util.Scanner;
public class Welcome {
/**
* @param args
*/
public static void main(String[] args) {
Scanner sin = new Scanner(System.in);
System.out.println("请输入姓名:");
String s = sin.next();
char ch[]= s.toCharArray();
System.out.print("您的姓为:"+ch[0]+"\n您的名为:"+ch[1]);
}
}
LZ平时多练点吧,这个其实还是挺简单的。
满意请采纳,谢谢!
求下题代码,java题目。
Java程序:
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* 汽车类
*/
class Car {
/**
* 汽车编号
*/
protected int id = 0;
/**
* 汽车款式
*/
protected String type = null;
/**
* 构造汽车对象
*/
public Car() {
}
/**
* 构造汽车对象
* @param id 汽车编号
* @param type 汽车款式
*/
public Car(int id, String type) {
this.id = id;
this.type = type;
}
/**
* 获得汽车编号
* @return 汽车编号
*/
public int getId() {
return this.id;
}
/**
* 获得汽车款式
* @return 汽车款式
*/
public String getType() {
return this.type;
}
}
/**
* 汽车销售人员类
*/
class Saler {
/**
* 姓名
*/
protected String name = null;
public List
/**
* 构造销售汽车人员对象
*/
public Saler() {
}
/**
* 构造汽车销售人员对象
* @param name 姓名
*/
public Saler(String name) {
this.name = name;
}
/**
* 获得姓名
* @return 姓名
*/
public String getName() {
return this.name;
}
}
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
List
allCar.add(new Car(1001, "凯越"));
allCar.add(new Car(1002, "凯越"));
allCar.add(new Car(1003, "凯越"));
allCar.add(new Car(1004, "凯越"));
allCar.add(new Car(2001, "君威"));
allCar.add(new Car(2002, "君威"));
allCar.add(new Car(2003, "君威"));
allCar.add(new Car(2004, "君威"));
allCar.add(new Car(2005, "君威"));
Saler saler = new Saler("张三其");
int choice = 0;
int type;//销售车型
int num;//销售数量
while(true) {
System.out.println("请选择销售方式");
System.out.println("按车辆销售:\t1");
System.out.println("按车型销售:\t2");
System.out.println("查看销售情况:\t3");
System.out.println("退出:\t\t0");
System.out.print("您的选择:");
choice = scan.nextInt();
switch(choice) {
case 0://退出系统
System.out.println("退出系统");
System.exit(0);
break;
case 1://按车辆销售
for(Car car : allCar) {
if(! exists(saler.cars, car)) {
saler.cars.add(car);
System.out.printf("\t售出 %s 1 辆\n", car.getType());
break;
}
}
break;
case 2://按车型销售
System.out.print("车型(凯越 0/君威 1):");
type = scan.nextInt();
System.out.print("销售数量:");
num = scan.nextInt();
int c = 0;//实际销售数量
for(Car car : allCar) {
if(c >= num) {
break;
}
if(car.getType().equals(type == 0 ? "凯越" : "君威") && ! exists(saler.cars, car)) {
saler.cars.add(car);
c++;
}
}
if(c < num) {
System.out.printf("\t库存不足,实际售出 %s %d 辆\n", type == 0 ? "凯越" : "君威", c);
}
else {
System.out.printf("\t售出 %s %d 辆\n", type == 0 ? "凯越" : "君威", num);
}
break;
case 3://查看销售情况
System.out.println("\t当前销售情况一览");
System.out.printf("\t%10s%10s\n", "汽车款式", "汽车编号");
for(Car car : saler.cars) {
System.out.printf("\t%10s%10d\n", car.getType(), car.getId());
}
System.out.println("---------------------------");
System.out.printf("\t小计:\t%d 辆\n", saler.cars.size());
break;
default:
break;
}
try {
System.in.read();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//判断car在cars中是否存在
public static boolean exists(List
for(Car c : cars) {
if(c.getId() == car.getId()) {
return true;
}
}
return false;
}
}
运行测试:
请选择销售方式
按车辆销售:1
按车型销售:2
查看销售情况:3
退出:0
您的选择:1
售出 凯越 1 辆
请选择销售方式
按车辆销售:1
按车型销售:2
查看销售情况:3
退出:0
您的选择:2
车型(凯越 0/君威 1):0
销售数量:3
售出 凯越 3 辆
请选择销售方式
按车辆销售:1
按车型销售:2
查看销售情况:3
退出:0
您的选择:3
当前销售情况一览
汽车款式 汽车编号
凯越 1001
凯越 1002
凯越 1003
凯越 1004
---------------------------
小计:4 辆
请选择销售方式
按车辆销售:1
按车型销售:2
查看销售情况:3
退出:0
您的选择:0
退出系统
java中怎么编写多项选择题代码
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Choice;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JFrame;
public class Choose extends JFrame implements ItemListener {
/**
*
*/
private static final long serialVersionUID = 1L;
Label l1, l2;
TextField t1, t2;
CheckboxGroup checkboxGroup = new CheckboxGroup();
Checkbox checkbox1 = new Checkbox("QQ", checkboxGroup, false);
Checkbox checkbox2 = new Checkbox("MSN", checkboxGroup, false);
Checkbox checkbox3 = new Checkbox("ICQ", checkboxGroup, false);
Choice c;
public Choose() {
super("简单小程序");
this.setLayout(new FlowLayout());
l1 = new Label("选择你常用的软件:");
l2 = new Label("选择你喜欢的水果:");
checkbox1.addItemListener(this);
checkbox2.addItemListener(this);
checkbox3.addItemListener(this);
t1 = new TextField(20);
t2 = new TextField(20);
c = new Choice();
c.addItemListener(this);
c.add("苹果");
c.add("橘子");
c.add("香蕉");
c.add("梨子");
this.add(l1);
this.add(checkbox1);
this.add(checkbox2);
this.add(checkbox3);
this.add(t1);
this.add(l2);
this.add(c);
this.add(t2);
this.setSize(450, 200);
this.setVisible(true);
}
public static void main(String[] args) {
new Choose();
}
public void itemStateChanged(ItemEvent e) {
if (e.getSource() == checkbox1) {
t1.setText("你常用的软件是:" + checkbox1.getLabel());
}
if (e.getSource() == checkbox2) {
t1.setText("你常用的软件是:" + checkbox2.getLabel());
}
if (e.getSource() == checkbox3) {
t1.setText("你常用的软件是:" + checkbox3.getLabel());
}
t2.setText("你喜欢的水果是:" + c.getSelectedItem());// 得到选中的下拉列表值
}
}