第一题:修改手机默认语言
//手机类
class phone{
private String language="英文"; //默认语言为英文
public phone(){
System.out.println("智能手机的默认语言为"+this.language);
}
//创建通过有参构造将手机语言设置为中文
public phone(String language) {
this.language = language;
System.out.println("将智能手机的默认语言设置为"+this.language);
}
}
public class main {
public static void main(String[] args) {
//创建两个手机对象,演示效果
phone apple=new phone();
//在创建手机的时刻,将手机的语言改为中文
phone huawei=new phone("中文");
}
}
第二题:设置信用卡密码
class creditCard{
String id; //卡号
String password="123456"; //密码
//设置卡号,默认密码
public creditCard(String id) {
this.id=id;
System.out.println("信用卡"+this.id+"的默认密码为:"+this.password);
}
//设置初始密码与初始密码
public creditCard(String id,String Password){
this.id=id;
this.password=Password;
System.out.println("信用卡"+this.id+"的密码为:"+this.password);
}
//重置信用卡密码,
public creditCard(creditCard data,String Password){
this.id=data.id;
this.password=Password;
System.out.println("重置信用卡"+this.id+"的密码为:"+this.password);
}
}
public class Main_2 {
public static void main(String[] args) {
//创建信用卡,不设置初始密码
creditCard num1=new creditCard("40137356156462146");
//重置密码,将需要重置的对象传递过去,使用其卡号,并返回一个新对象
num1=new creditCard(num1,"168779"); //重置密码
}
}
第三题:飞速的高铁
class train{
double speed;
public train() {
}
public train(double speed) {
this.speed = speed;
System.out.printf("火车的速度为 %.1f 公里/小时\n",speed);
}
}
class high_speed_railway extends train{
public high_speed_railway(double speed) {
super(speed); //构造父类
this.speed=(super.speed*2); //高铁的速度是火车的二倍
System.out.printf("高铁的速度为 %.1f 公里/小时\n",this.speed);
}
}
public class Main_3 {
public static void main(String[] args) {
//创建高铁类,传入火车的速度
high_speed_railway h=new high_speed_railway(145.8);
}
}
第四题:计算钟表时间
class clock{
String type;
double price;
public static void getTime(){
//不确定题目要求的是获取当前时间,还是只是模拟获取时间
System.out.println("当前时间:10点10分");
}
public void show(){
System.out.printf("%s的价格为 %.2f元RMB\n",this.type,this.price);
}
public clock(String type, double price) {
this.type = type;
this.price = price;
}
}
public class Main_4 {
public static void main(String[] args) {
//创建两个钟表的对象,并赋值数据
clock clockNum1=new clock("机械钟",189.99);
clock clockNum2=new clock("石英表",69);
clockNum1.show();
clockNum1.getTime();
clockNum2.show();
clockNum2.getTime();
}
}
第五题:多功能参数
static final double PI=3.141592653589793;
public static void main(String[] args) {
//获取PI值
System.out.println(calculation());
//获取圆的面积
System.out.println(calculation(4));
//获取矩阵的面积
System.out.println(calculation(3, 4));
}
public static double calculation(double r){
return PI*(r*r); //计算圆的面积:pi*r方
}
public static double calculation(double wide,double height){
return wide*height; //计算矩形的面积:宽*高
}
public static double calculation(){
return PI; //无输入返回PI
}
}
第六题:计算圆与矩形的面积
//图形类
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));
}
}
第七题:定义人的介绍方式
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());
}
}
第八题:编写登录方法
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;
}
}
第九题:水果价格
//普通水果
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));
}
}
第十题:模拟上课场景
//问候接口
interface greet{
void Greet();
}
//工作接口
interface work{
void Work();
}
//教师类
class teacher implements greet,work{
private String name;
//实现这两个接口
@Override
public void Work(){
System.out.println(this.name+":同学们好");
}
@Override
public void Greet(){
System.out.println(this.name+":老师开始上课");
}
//有参构造
public teacher(String name) {
this.name = name;
}
//无参构造
public teacher() {
}
}
class student implements greet,work{
private String name;
//实现这两个接口
@Override
public void Greet() {
System.out.println(this.name+":老师好");
}
@Override
public void Work() {
System.out.println(this.name+":同学开始记笔记");
}
//有参构造与无参构造
public student(String name) {
this.name = name;
}
public student() {
}
}
public class Main_10 {
public static void main(String[] args) {
// 创建老师与学生的对象
teacher mike=new teacher("mike");
student peter=new student("peter");
peter.Greet(); //模拟上课场景
mike.Greet();
mike.Work();
peter.Work();
}
}
第十一题:喜欢做的事
interface DadHobby{
//抽烟
void smoke();
//钓鱼
void fishing();
}
interface MothersHobby{
//看电视
void watchTV();
//做饭
void cook();
}
class son implements DadHobby,MothersHobby{
@Override
public void smoke() {
//ps:抽烟有害健康,未成年人禁止吸烟
System.out.println("抽烟");
}
// 实现钓鱼方法
@Override
public void fishing() {
System.out.println("钓鱼");
}
@Override
public void watchTV() {
System.out.println("看电视");
}
@Override
public void cook() {
System.out.println("做饭");
}
}
public class Main_11 {
public static void main(String[] args) {
son xm=new son();
System.out.println("儿子喜欢做的事有:");
xm.watchTV();
xm.cook();
xm.smoke();
xm.fishing();
}
}