java小白自学笔记——Day3

例1:编写猫类,创建三只猫的对象,并让它们一起抓老鼠

   代码如下:

package project;
public class cat {String color;public cat(String color){this.color=color;}public static void main(String[] args) {cat cat1=new cat("black");cat cat2=new cat("white");cat cat3=new cat("yellow");System.out.println(cat1.color+"、"+cat2.color+"、and"+" "+cat3.color+" "+"cat catch mouse todether!");}			
}

例2:创建工具类,类中调动pow()类,并计算4.56和3.5的四次幂

   代码如下:

package project;
public class gongju{double x;public static double pow(double x){return x*x*x*x;}public static void main(String[] args) {double y=pow(4.56);System.out.println("4.56的三次幂是:"+y);double z=pow(3.5);System.out.println("3.5的三次幂是:"+z);}			
}

例3:电影票:满18周岁100元全票,未满的享受半价。控制台按姓名、年龄、票价输出以下观众信息(张三,20;李四,16;王五,8;赵六,32)

    代码如下:

package project;
public class MoivePrice {String name;int age;int price;public MoivePrice(String name,int age){this.name=name;this.age=age;if(age>=18){this.price=100;}else if(age>=0&&age<18){this.price=50;}else{this.price=0;}}public static void main(String[] args) {MoivePrice p1=new MoivePrice("张三",20);MoivePrice p2=new MoivePrice("李四",16);MoivePrice p3=new MoivePrice("王五",8);MoivePrice p4=new MoivePrice("赵六",32);System.out.println("姓名  年龄  票价(元)");System.out.println("________________");System.out.println(p1.name+"  "+p1.age+"  "+p1.price);System.out.println(p2.name+"  "+p2.age+"  "+p2.price);System.out.println(p3.name+"  "+p3.age+"   "+p3.price);System.out.println(p4.name+"  "+p4.age+"  "+p4.price);}			
}

例4:记录4名学生的语文、数学、英语三科成绩,再计算每个人的平均分(用到成员变量和this关键字)

     代码如下:

package project;
public class ScoresAverage{String name;double chinese,math,english;double average;int number;public  ScoresAverage(int number,String name,double chinese,double math,double english){this.number=number;this.name=name;this.chinese=chinese;this.math=math;this.english=english;this.average=(math+chinese+english)/3;}public static void main(String[] args) {ScoresAverage s1=new  ScoresAverage(1,"张三",91.5,98.0,89.0);ScoresAverage s2=new  ScoresAverage(2,"李四",96.0,98.5,93.0);ScoresAverage s3=new  ScoresAverage(3,"王五",97.0,100.0,98.5);ScoresAverage s4=new  ScoresAverage(4,"赵六",77.0,83.0,81.0);System.out.println("学号  姓名  语文  数学  英语  平均分");System.out.println("____________________________");System.out.println(s1.number+"  "+s1.name+"  "+s1.chinese+"  "+s1.math+"  "+s1.english+"  "+s1.average);System.out.println(s2.number+"  "+s2.name+"  "+s2.chinese+"  "+s2.math+"  "+s2.english+"  "+s2.average);System.out.println(s3.number+"  "+s3.name+"  "+s3.chinese+"  "+s3.math+"  "+s3.english+" "+s3.average);System.out.println(s4.number+"  "+s4.name+"  "+s4.chinese+"  "+s4.math+"  "+s4.english+"  "+s4.average);}			
}

例5:编写矩形类型,并求出给定矩形的面积

    代码如下:

package project;
import java.util.Scanner;
public class SIZE {double LongSide,WideSide;double s;public SIZE(double chang,double kuan){this.LongSide=chang;this.WideSide=kuan;this.s=chang*kuan;}public static void main(String[] args) {Scanner sc=new Scanner(System.in);double x=sc.nextDouble();double y=sc.nextDouble();sc.close();SIZE ju=new pro(x,y);System.out.println(ju.s);}			
}

例6:编写两个数运算的简单计算器类

    代码如下:

package project;
import java.util.Scanner;
public class EasyCalculator {char operator;double x,y;double s;public EasyCalculator(double x,char yunsuaufu,double y){this.x=x;this.y=y;this.operator=yunsuaufu;if(yunsuaufu=='+'){this.s=x+y;}else if(yunsuaufu=='-'){this.s=x-y;}else if(yunsuaufu=='*'){this.s=x*y;}else if(yunsuaufu=='/'){this.s=x/y;}else if(yunsuaufu=='%'){this.s=x%y;}else{this.s=0;}}public static void main(String[] args) {Scanner sc=new Scanner(System.in);double x=sc.nextDouble();char ch=sc.next().charAt(0);double y=sc.nextDouble();sc.close();EasyCalculator Project=new EasyCalculator(x,ch,y);System.out.println(Project.s);}			
}

   


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部