6.输出圆形和矩形的面积
//图形类
class Shape{
public void Calculated_area(){
}
}
//圆形类
class circle extends Shape{
//计算圆的面积
public double Calculated_area(double r){
return (3.1415926)*(r*r);
}
}
//矩形类
class rectangle extends Shape{
//计算矩形面积
public double Calculated_area(double wide,double hetght){
return wide*hetght;
}
}
public class Main_6 {
public static void main(String[] args) {
circle c=new circle();
System.out.println("圆的面积:"+c.Calculated_area(1.5));
rectangle r=new rectangle();
System.out.println("矩形的面积:"+r.Calculated_area(2, 5.5));
}
}
7.定义人类的介绍方式
class human{
String name;
int age;
public human(String name, int age) {
this.name = name;
this.age = age;
}
public human() {
}
@Override
public String toString(){
// 判断年龄是否大于等于18岁,是则返回成年,否则返回未成年人
return "我"+this.age+"岁,我是"+(this.age >= 18 ? "成年人" : "未成年人");
}
}
public class Main_7 {
public static void main(String[] args) {
human h_1=new human("小明",18);
System.out.println(h_1.toString());
}
}
8.编写登入方法
class user{
String name;
String password;
public user(String id, String password) {
this.name = id;
this.password = password;
}
public user() {
}
}
public class Main_8 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("请输入用户名:");
String name=in.next();
System.out.println("请输入密码:");
String passwo=in.next();
System.out.println("------------------");
if(true == logIn(name,passwo)){
System.out.println("登录成功");
}else{
System.out.println("登录失败");
}
}
//登录方法
public static boolean logIn(String Name,String PassWord){
//代表正确的用户名与密码
user ur=new user("张三","123456");
int nLen=Name.length();
int pLen=PassWord.length();
//如果用户名密码长度与正确的长度不相同
if(nLen != ur.name.length() || pLen != ur.password.length() ){
return false;
}
//验证用户名
for (int i = 0; i < nLen; i++) {
//判断用户名是否相同
// charAt(index) 为获取字符串指定下标的元素
if(Name.charAt(i) != ur.name.charAt(i)){
return false;
}
}
//验证密码
for (int i = 0; i < pLen; i++) {
//判断密码是否相同
if(PassWord.charAt(i) != ur.password.charAt(i)){
return false;
}
}
//如果所以都符合的话,那么返回true,表示登录成功
return true;
}
}
9.人工包装的水果与普通水果的价格
//普通水果
class fruit{
String name; //水果名称
double price; //水果价格
double weight; //重量 / 千克
public fruit(String name, double price, double weight) {
this.name = name;
this.price = price;
this.weight = weight;
}
public fruit() {
}
@Override
public String toString(){
return this.name+"\t\t"+this.price+"\t\t\t\t\t"+this.weight+"\t\t\t\t\t"+"0.0\t\t\t"+"";
}
}
//精装水果
class PackagedFruit extends fruit{
double PackingCharge; //包装费
public PackagedFruit(String name, double price, double weight, double packingCharge) {
super(name, price, weight);
this.PackingCharge=packingCharge;
}
public PackagedFruit() {
}
@Override
public String toString(){
return this.name+"\t\t"+this.price+"\t\t\t\t\t"+this.weight+"\t\t\t\t\t"+this.PackingCharge+"\t\t\t"+"";
}
}
public class Main_9 {
public static void main(String[] args) {
//给普通水果赋值
fruit apple=new fruit("苹果",1.98,5.0);
//给精装水果赋值
PackagedFruit PackageApple=new PackagedFruit("苹果",1.98,5.0,1);
System.out.println("水果名称 \t 水果价格(元/千克) \t 水果重量(千克) \t 包装费(元/千克) \t 总价");
System.out.println("-----------------------------------------------------------------------------------");
//总价格等于 价格*重量
double sum1=apple.price*apple.weight;
System.out.println(apple.toString()+sum1); //输出数据与总价
//总价格等于 水果价格包装费*重量
double sum2=(PackageApple.price+PackageApple.PackingCharge)*PackageApple.weight;
System.out.println(PackageApple.toString()+sum2);
System.out.println("-----------------------------------------------------------------------------------");
//差价等于两个总和的差值
double difference=Math.abs(sum2-sum1);
System.out.println("差价\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"+(difference));
}
}
10.模拟上课场景
代码:
public interface student {
void answer();// 回答的方法
void note();// 笔记的方法
}
public interface teacher {//定义一个接口
void greeting();// 问候的方法
void attend();// 上课的方法
}
public class Qi implements teacher, student {// 继承IFather接口和IMather接口
@Override
public void answer() {
System.out.println("老师好");
}
@Override
public void note() {
System.out.println("同学开始记笔记");
}
@Override
public void greeting() {
System.out.println("同学们好");
}
@Override
public void attend() {
System.out.println("老师开始上课");
}
public static void main(String[] args) {
student peter = new Qi();
System.out.print("peter:");
peter.answer();
teacher mike = new Qi();
System.out.print("mike:");
mike.greeting();
System.out.print("mike:");
mike.attend();
System.out.print("peter:");
peter.note();
}
}
11.儿子喜欢做的事
代码:
public interface Book {
void fish();
void smoke();
}public interface Bresk {
void cook();
void wath();
}public class Qi implements Book,Bresk {
@Override
public void cook() {
// TODO Auto-generated method stub
System.out.println("做饭");
}
@Override
public void wath() {
// TODO Auto-generated method stub
System.out.println("看电视");
}
@Override
public void fish() {
// TODO Auto-generated method stub
System.out.println("钓鱼");
}
@Override
public void smoke() {
// TODO Auto-generated method stub
System.out.println("抽烟");
}
public static void main(String[] args) {
Bresk fa = new Qi();// 通过子类创fa建接口对象
System.out.println("儿子喜欢做的事有:");
fa.wath();
fa.cook();
Book mo =new Qi();// 通过子类创建mo接口对象
mo.smoke();
mo.fish();
}
}
运行结果;