Java笔记_this关键字_HomeWork(5 - 9 题)
第五题
/**
* @ClassName HomeWork05
* @Description TODO
* @Author Orange
* @Date 2021/4/25 10:09
* @Version 1.0
**/
/*
Notes:
定义一个圆类Circle,定义属性:半径,
提供显示圆周长功能的方法,提供显示圆面积的方法
Define a circle class Circle, define the attribute: radius,
provide a method to display the function of the circumference,
and provide a method to display the area of the circle.
*/
import java.util.Scanner;
public class HomeWork05 {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
System.out.println("请输入圆的半径:");
Double radius = myScanner.nextDouble(); //键入半径值
Circle a = new Circle(radius); //创建对象a,并利用构造器对radius赋值
System.out.println("圆的周长为: " + a.perimeter()); //调用对象a的方法perimeter并输出圆周长
System.out.println("圆的面积为: " + a.area()); //调用对象a的方法area并输出圆面积
}
}
class Circle {
double PI = 3.14;
double radius;
public Circle(double radius) { //构造器
this.radius = radius;
}
public Double perimeter () {
Double Circle_perimeter = 2 * PI * this.radius; //计算圆周长
Circle_perimeter = (double) Math.round(Circle_perimeter * 100) / 100; //将double类型转换成保留一位小数
return Circle_perimeter;
}
public Double area () {
Double Circle_area = PI * PI * this.radius; //计算圆周长
Circle_area = (double) Math.round(Circle_area * 100) / 100;
return Circle_area;
}
}
/*
Program running results:
------------------------------------
请输入圆的半径:
10
圆的周长为: 62.8
圆的面积为: 98.6
------------------------------------
*/
第六题
/**
* @ClassName HomeWork06
* @Description TODO
* @Author Orange
* @Date 2021/4/25 13:41
* @Version 1.0
**/
/*
Notes:
编程创建一个Cale计算类,在其中定义2个变量表示两个操作数,
定义四个方法实现求和、差、乘、商(要求除数为0的话,要提示)
并创建两个对象,分别测试。
Create a 'Cale' calculation class programmatically,
define two variables in it to represent two operands,
define four methods to achieve sum, difference, multiplication,
and quotient (if the divisor is required to be 0,
you need to prompt) and create two objects and test separately.
*/
public class HomeWork06 {
public static void main(String[] args) {
A06 a = new A06(4 , 9); //对象a
System.out.println("这两个数的和为:" + a.sum());
System.out.println("这两个数的差为:" + a.difference());
System.out.println("这两个数的积为:" + a.product());
System.out.println("这两个数的除数为:" + a.divisor());
A06 b = new A06(13 , 5); //对象b
System.out.println("这两个数的和为:" + b.sum());
System.out.println("这两个数的差为:" + b.difference());
System.out.println("这两个数的积为:" + b.product());
System.out.println("这两个数的除数为:" + b.divisor());
}
}
class A06 {
double num1;
double num2;
double Sum;
double Difference;
double Product;
double Divisor;
public A06(double num1, double num2) {
this.num1 = num1;
this.num2 = num2;
System.out.println();
System.out.println("num1 = " + this.num1 + ", num2 = " + this.num2);
}
public Double sum() { //求和
Sum = this.num1 + this.num2;
return Sum;
}
public Double difference() { //求差
if(this.num1 > this.num2) {
Difference = this.num1 - this.num2;
return Difference;
}else {
Difference = this.num2 - this.num1;
return Difference;
}
}
public Double product() { //求乘积
Product = this.num1 * this.num2;
return Product;
}
public Double divisor() { //求除数
if(this.num1 > this.num2) {
Divisor = this.num1 / this.num2;
}else {
Divisor = this.num2 / this.num1;
}
return Divisor;
}
}
/*
Program running results:
------------------------------------
num1 = 4.0, num2 = 9.0
这两个数的和为:13.0
这两个数的差为:5.0
这两个数的积为:36.0
这两个数的除数为:2.25
num1 = 13.0, num2 = 5.0
这两个数的和为:18.0
这两个数的差为:8.0
这两个数的积为:65.0
这两个数的除数为:2.6
------------------------------------
*/
第七题
/**
* @ClassName HomeWork07
* @Description TODO
* @Author Orange
* @Date 2021/4/25 16:34
* @Version 1.0
**/
/*
Notes:
设计一个Dog类,有名字、颜色和年龄属性,定义输出方法show()
显示其信息,并创建对象,进行测试【提示:this,属性】
Design a 'Dog' class with attributes of name, color and age,
define the output method show() to display its information,
and create an object for testing [Prompt: this, attribute]
*/
public class HomeWork07 {
public static void main(String[] args) {
Dog d = new Dog("峰哥的狗仔", "水泥灰", 3);
System.out.println("====狗的信息====");
System.out.println("名字: " + d.name );
System.out.println("颜色: " + d.color );
System.out.println("年纪: " + d.age );
}
}
class Dog {
String name;
String color;
int age;
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 + ", color = "
+ this.color + ", age = " + this.age);
}
}
/*
Program running results:
------------------------------------
====狗的信息====
名字: 峰哥的狗仔
颜色: 水泥灰
年纪: 3
------------------------------------
*/
第八题
/**
* @ClassName HomeWork08
* @Description TODO
* @Author Orange
* @Date 2021/4/26 8:44
* @Version 1.0
**/
/*
Notes:
给定一个Java程序的代码如下所示,则编译运行后,输出结果是?
public class Test {
int count = 0;
public void count1() {
count = 10;
System.out.println("count1 = " + count);
}
public void count2(){
System.out.println("count1 = " + count++);
}
public static void main(String[] args) {
new Test().count1();
Test t1 = new Test();
t1.count2();
t1.count2();
}
}
*/
public class HomeWork08 {
static int count = 0;
public void count1() {
count = 10;
System.out.println("count1 = " + count);
}
public static void count2(){
System.out.println("count1 = " + count++);
}
public static void main(String[] args) {
new HomeWork08().count1();
HomeWork08 t1 = new HomeWork08();
HomeWork08.count2();
HomeWork08.count2();
}
}
/*
Program running results:
------------------------------------
count1 = 10
count1 = 10
count1 = 11
------------------------------------
*/
Java笔记_this关键字_HomeWork(5 - 9 题)的更多相关文章
- Java笔记 —— this 关键字
Java笔记 -- this 关键字 h2{ color: #4ABCDE; } a{ color: blue; text-decoration: none; } a:hover{ color: re ...
- Java笔记:关键字
关键字 描述 abstract 抽象方法,抽象类的修饰符 assert 断言条件是否满足 boolean 布尔数据类型 break 跳出循环或者label代码段 byte 8-bit 有符号数据类型 ...
- JAVA笔记4__static关键字/对象数组/foreach/方法的可变参数
/** * static关键字:修饰属性(实质就是全局变量).方法(无需本类的对象即可调用此方法).类. * 1.static方法只能调用static方法 * 2.static方法只能访问static ...
- JAVA笔记 -- this关键字
this关键字 一. 基本作用 在当前方法内部,获得当前对象的引用.在引用中,调用方法不必使用this.method()这样的形式来说明,因为编译器会自动的添加. 必要情况: 为了将对象本身返回 ja ...
- JAVA笔记4-static关键字
1.static方法中不能使用this关键字 2.例题:static成员变量用于计数
- JAVA笔记11-Final关键字
相当于C++中的const (1)final的变量值(成员变量.局部变量(形参))不可被改变: (2)final的方法不能被重写: (3)final的类不能被继承.
- JAVA笔记3-this关键字
1. 2.例题
- Java复习笔记--java中this 关键字
Java中this关键字,this可以调用类的成员变量和成员方法,this还可以调用类中的构造方法.使用这种方式值得注意的是, 只可以在无参构造方法中的第一句使用this关键字调用有参构造方法. pu ...
- Thinking in Java 笔记
大二就买了这本书,如今再看这本书,看到了一些以前没看的细节,也有了不同的体会.本文使用第4版,整理每章的笔记心得.老外的书有个特点,他会花费大量的文字去阐述一个概念,这比堆代码强多了. 第 1 章 对 ...
- TIJ读书笔记05-this关键字
TIJ读书笔记05-this关键字 概述 this的用法 static方法 概述 两个对象同时调用一个方法,那么jvm怎么知道是哪个方法调用的呢. 我们在书写的时候会写成a.fun(1) 或者b.fu ...
随机推荐
- 教你解决虚拟机用不了USB设备的苦恼。
如果你看见了上图的效果说明我们的苦恼是一样一样的. vm10 vm11 vm player都有这个问题!!!! 对不对?哈哈 . 在虚拟机右击设备点击连接(Connect) 然后听见你的电脑发出了硬 ...
- 使用pycharm打开sqlite的问题
目录 问题:有同学在sqlite数据库文件执行数据库迁移完成前,点开了他,导致sqlite数据库被pycharm当成文本文件打开了,并且不会改了. 其实sqlite文件和电脑中的其他文件(xx.mp4 ...
- 艰难的 debug 经历,vscode 无法获取远程环境 ssh 报错,windows 11 ssh
背景介绍 要做系统结构实验,学校和华为云合作使用华为云的 aarch64 裸机,需要使用 ssh 远程开发,笔者为了追求良好的开发体验,决定使用 vscode 开发,实验环境配置过程中遇到了两个问题, ...
- P7_小程序的宿主环境
宿主环境简介 什么是宿主环境 宿主环境(host environment)指的是程序运行所必须的依赖环境.例如:Android 系统和 iOS 系统是两个不同的宿主环境.安卓版的微信 App 是不能在 ...
- vue的异步组件
异步组件 异步组件:可以在首页加载之前先加载的组件,主要是做性能优化,提高用户体验 一.基本用法 在大型项目中,我们可能需要拆分应用为更小的块,并仅在需要时再从服务器加载相关组件.Vue 提供了 de ...
- 【TS】函数和函数类型
在使用函数的时候,通常会给函数传值,或者给函数一个返回值调用,这个时候就会涉及到函数类型. 函数类型分为两个方面: 1.函数参数 2.函数返回值 语法: function 函数名( 参数 : 参数类型 ...
- vue-cli框架的下载以及框架目录介绍
目录 vue-cli框架的下载以及框架目录介绍 一.vue-cli创建项目 二.Vue项目目录介绍 vue-cli框架的下载以及框架目录介绍 一.vue-cli创建项目 在终端下载先下载cnpm # ...
- 欧拉函数和遗忘自动机 SX 的故逝
欧拉函数 \(\varphi(n)\) 定义为小于 \(n\) 与 \(n\) 互质的数字,炒个例子,\(\varphi(10) = 4\),因为 \(1,3,7,9\) 与 \(10\) 互质. 怎 ...
- 【译】.NET 7 中的性能改进(六)
原文 | Stephen Toub 翻译 | 郑子铭 矢量化 (Vectorization) SIMD,即单指令多数据 (Single Instruction Multiple Data),是一种处理 ...
- K8S Pod Sidecar 应用场景之一-加入 NGINX Sidecar 做反代和 web 服务器
Kubernetes Pod Sidecar 简介 Sidecar 是一个独立的容器,与 Kubernetes pod 中的应用容器一起运行,是一种辅助性的应用. Sidecar 的常见辅助性功能有这 ...