public class HomeWork01{
public static void main(String[] args) {
double [] arr = {56.56,45.56,4545.55,45.66};
A01 t = new A01();
System.out.println("最大值为"+t.max(arr));
}
}
class A01{
public double max(double[] arr){
double max = 0;
for (int a = 0;a<arr.length ;a++ ) {
if (arr[a]>max) {
max = arr[a];
}
}
return max;
}
}
public class HomeWork01{
public static void main(String[] args) {
A02 t = new A02();
String [] arr ={"张三","李四","王五"};
System.out.println(t.find(arr,"王五"));
}
}
class A02{
public int find(String[] arr,String a){
for (int i = 0;i<arr.length ;i++ ) {
if (a.equals(arr[i])) {
return i;
}
}
return -1 ;//如果没有for循环没有找到符合的直接返回-1
}
}
public class HomeWork01{
public static void main(String[] args) {
Book t = new Book("《演员的自我修养》",256);
Book t1 = new Book("《java核心技术卷1》",120);
}
}
class Book{
String name;
double price;
Book(String name,double price){
this.name = name;
if (price>100 && price<150) {
this.price = 100;
}else if (price > 150 ) {
this.price = 150;
}
System.out.println(this.name+"\t"+this.price);
}
}
public class HomeWork01{
public static void main(String[] args) {
A03 t = new A03();
int []arr1 = {5,4,5,4,8};
int []newArr =t.copyArr(arr1);
for (int a : newArr ) {
System.out.print(a);
}
}
}
class A03{
public int [] copyArr(int [] arr){
int [] arr2=new int[arr.length];
for (int i=0;i<arr.length ;i++ ) {
arr2[i]=arr[i];
}
return arr2;
}
}
周长公式:2Πr
面积公式:Πrr
public class HomeWork01{
public static void main(String[] args) {
Circle t = new Circle(50);
System.out.println("周长为"+t.perimeter()+"\n面积为"+t.area());
}
}
class Circle{
double radius;
Circle(double radius){
this.radius = radius;
}
public double perimeter(){
return 2*Math.PI*radius;
}
public double area(){
return Math.PI*radius*radius;
}
}
public class HomeWork01{
public static void main(String[] args) {
new T().count1();//匿名对象,只能使用一次
T t = new T();
t.count2();
t.count2();
}
}
class T{
int num = 9;
public void count1(){
num = 10;
System.out.println(num);
}
public void count2(){
System.out.println(num++);//++在后,先输出后自增
}
}
10
9
10
注意点1:注意new T().count1和t.count1是不同的两个对象,地址也不同所以变量num会重新变成9
注意点2:++在后,先输出后自增
public double method(double a,double b){
return a+b;
}
class T{
String name;
int age;
char gender;
String post;
double salary;
T(String post,double salary){
this.post = post;
this.salary = salary;
}
T(String name,char gender,int age){
this.name = name;
this.age = age;
this.gender = gender;
}
T(String name,char gender,int age,String post,double salary){
this.(name,gender,age);
this.post = post;
this.salary = salary;
}
}
练习点:构造器调用另一个构造器this(形参);。注意用this调用另一个构造器只能在第一行
public class HomeWork01{
public static void main(String[] args) {
Circle c = new Circle();
PassObject op = new PassObject();
op.printAreas(c,5);
}
}
class Circle{
double radius;
public double findAreas(){
return Math.PI*radius*radius;
}
public void setRadius(double radius){
this.radius = radius;
}
}
class PassObject{
public void printAreas(Circle c ,int times){
System.out.println("radius\tAreas");
for (int i = 1;i<=times ;i++ ) {
c.setRadius(i);
System.out.println(c.radius+"\t"+c.findAreas());
}
}
}
难点:未接触知识点Set方法 封装会接触到