Java数组,构造方法,this关键字例题全解--经典问题

1.int[][]  arr = {{5, 7, 9},{12, 14, 16, 18},{23, 25, 36, 47},{22, 54, 65, 15},{22, 34}}; 求该数组元素之和
class Demo6{
   public static void main(String[] args){
   int[][]  arr = {{5,7,9},{12,14,16,18},{23,25,36,47},{22,54,65,15},{22,34}};
     int sum=0;
    System.out.print("二维数组和sum=");
for (int i=0;i<arr.length ;i++ )
     {
for (int j=0; j<arr[i].length ;j++ )
{
 sum+=arr[i][j];
}
     }
System.out.println(sum+";");
    }
  }




2.使用二维数组存储班上五个学生三门功课的考试成绩,要求输出每一个学生的总分、平均分、最高分、最低分。


要求:
①学生个数动态获取。
②学生考试的门数动态获取。
③每个学生各门功课考试的成绩动态获取。
【动态获取是指由键盘输入】


import java.util.Scanner;
class Demo7{
   public static void main(String[] args){
   Scanner sc=new Scanner(System.in);
   Scanner input = new Scanner(System.in);
   System.out.print("请录入班级学生的个数:");
int stuNums = input.nextInt();
int[][] scores = new int[stuNums][];
for (int i = 0; i < scores.length; i++) {
System.out.print("\n请录入第【" + (char)(i +65) + "】个学生考试的功课门数:");
int courseNum = input.nextInt();
scores[i] = new int[courseNum];
int max = 0;
int min = 0;
double totalScore = 0;
double avgScore = 0;
for (int j = 0; j < scores[i].length; j++) {
System.out.print("请录入该学生第【" +  (char)(j +65) + "】功课的成绩:");
scores[i][j] = input.nextInt();
// b)求最高分
if (scores[i][max] < scores[i][j]) {
max = j;
}
// c)最低分
if (scores[i][min] > scores[i][j]) {
min = j;
}
// d)总成绩
totalScore += scores[i][j];
}
avgScore = totalScore / scores[i].length;
System.out.println("该学生成绩信息如下:总分--》" + totalScore + ",平均分--》"
+ avgScore + ",最高分---》" + scores[i][max] + ",最低分---》"
+ scores[i][min]);
}
   }
}




3.编程打印一个二维数组中所有元素的和,并打印最大值,最小值(以及它们所在的行号和列号)
void printResult(int a[][]){......}
输出结果格式:
二维数组中所有元素的和是:123
最大值是:15,行号:3,列号:1
最小值是:1,行号:2,列号:4


class Demo7 
{
public static void main(String[] args){
int[][] arr = {{2,3,4},{1,2},{5,6,7}};
printResult(arr);
printMax(arr);
}
public static void printResult(int arr[][]){
int sum = 0;
int max = 0;
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
sum +=arr[i][j];
}
}
System.out.println("二维数组中所有元素的和是"+sum);
}
public static void printMax(int arr[][]){
int max = arr[0][0];//存储最大值
int x = 0;//行号
int y = 0;//列号
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
if(arr[i][j] > max){
max = arr[i][j];
x = i;
y = j;
}
}
}
System.out.println("最大值是:"+max+",行号"+x+",列号"+y);
}
}






利用面向对象的思想写下面的程序


1.小美在朝阳公园溜旺财【注:旺财是狗】


分析:
Dog:
特性:名称
Park:
特性:名称
Person
特性:姓名
行为:溜(Park p,Dog d)


class Park{
String name;
}
class Dog{
String name;
}
public class Demo7{
String name;
void slip(Park p, Dog d){
System.out.println(name + "正在【"+p.name+"】溜【"+d.name+"】");
}
public static void main(String[] args){
Park p = new Park();
p.name = "朝阳公园";
Dog d = new Dog();
d.name = "旺财";
Demo7 pe = new Demo7();
pe.name = "小美";
pe.slip(p, d);
}
}


2.小明穿着白色的特步运动鞋在奥林匹克公园跑步


/*分析:
Park:
特性:名字
Shoes:
特性:颜色,品牌;分类
Person:
特性:姓名, 鞋
行为:
跑(在哪跑)*/


class Park{

String name;
}


class Shoes{
String color;
String brand;
String classify;
}


public class Person{

String name;
// 人有一双鞋
Shoes s;

void run(Park p){
System.out.println(name + "穿着【"+s.color+"】的【"+s.brand+"】"+s.classify+"在【"+p.name+"】跑步");
}

public static void main(String[] args){
Park p = new Park();
p.name = "奥林匹克公园";

Shoes s = new Shoes();
s.color = "白色";
s.brand = "特步";
s.classify = "运动鞋";

Person pe = new Person();
pe.name = "小明";
pe.s = s;
pe.run(p);
}

}








3.赵老师在讲台上讲课,小刚认真的听课做笔记

/*
 * 
  分析:
Teacher:
特性:姓名
行为:
讲课
Student:
特性:姓名
行为:
听课
做笔记
*/


class Teacher{

String name;

void teach(){

System.out.println(name + "正在讲台上讲课");
}
}


class Student{
String name;

void listen(){

System.out.print(name + "正在认真的听课");
}

void write(){

System.out.print("记笔记");
}
}


public class Demo {

public static void main(String[] args){
Teacher t = new Teacher();
t.name = "赵老师";
t.teach();

Student stu = new Student();
stu.name = "小刚";
stu.listen();
stu.write();
}
}








4.张阿姨和李阿姨在物美超市买红富士


/*
  分析:
Person:
特性:姓名
行为:
买(位置,东西)
SuperMarket:
特性:姓名


Apple
特性:品牌
*/


class SuperMarket{

String name;
}


class Apple{
String brand;
}


public class Person{
String name;

void buy(Person p,SuperMarket s, Apple a){

System.out.println(name + "和【"+p.name+"】在【"+s.name+"】买【"+a.brand+"】");
}

public static void main(String[] args){
SuperMarket s = new SuperMarket();
s.name = "物美";

Apple a = new Apple();
a.brand = "红富士";

Person p1 = new Person();
p1.name = "赵阿姨";

Person p2 = new Person();
p2.name = "张阿姨";
p2.buy(p1, s, a);

}
}






构造方法和this关键字的使用


1.定义一“圆”(Circle)类,圆心为“点”Point类,构造一圆,求圆的周长和面积,并判断某点与圆的关系


分析:
点 - (x, y)
圆 - 半径和圆心
人 
计算


方式一:
/*
点类
*/
class Point
{
//特征
double x;
double y;


Point(double x, double y){
this.x = x;
this.y = y;
}
}


/*
圆类
*/
class Circle
{
// 特征
Point p;  // 圆心点
double r;


Circle(Point p, double r){
this.p = p;
this.r = r;
}
}
class Person
{


// 名字
String name;
// 行为 


// 1. 圆的面积
double getCircleArea(Circle c){

return Math.PI * Math.pow(c.r , 2);
}


// 2.圆的周长
double getCircleLength(Circle c){
return 2*Math.PI*c.r;
}


// 3.点和圆的关系  a:圆内   b:圆上   c:圆外
String relation(Point p, Circle c){
//  d = (x1-x2)^2 + (y1-y2)^2  再开根号    d > r 圆外   d==r 圆上   d < r 圆内


// 求给出的点和圆心之间的距离


// 横轴之间的距离差的平方
double x = Math.pow( p.x - c.p.x  , 2);
// 纵轴之间的距离差的平方
double y = Math.pow( p.y - c.p.y  , 2);
// 球 x + y 开平方
double d = Math.sqrt( x + y);


if(d > c.r){
return "圆外";
}else if(d == c.r){
return "圆上";
}else {
return "圆内";
}

}


public static void main(String[] args) 
{
System.out.println("Hello World!");


// 实例化一个点类
Point point = new Point(1, 1);


// 实例化圆类
Circle circle = new Circle(point, 1);

// 作业
Person work = new Person ();


// 1.圆的面积
double area = work.getCircleArea(circle);
System.out.println("面积:" + area);




// 2.圆的周长
double length = work.getCircleLength(circle);
System.out.println("周长:" + length);


// 3.点和圆的关系
Point newPoint = new Point(2, 2);


String relation = work.relation(newPoint, circle);
System.out.println("关系:" + relation);



}
}


方式二
class  work3_1
{
public static void main(String[] args) 
{
Point p = new Point(0,0);
Circle c = new Circle(p,2);


System.out.println("面积:"+c.getS());
System.out.println("周长:"+c.getC());


Point p1 = new Point(3,3);
c.getP(p1);
}
}


class Circle
{
private Point p;
private double r;

Circle(Point p,double r){
this.p = p;
this.r = r;
}

public double getC(){
return 2*3.14*r;
}


public double getS(){
return 3.14*r*r;
}


public void getP(Point p){
double x = (p.getX() - this.p.getX()) * (p.getX() - this.p.getX()) + (p.getY() - this.p.getY()) * (p.getY() - this.p.getY());
if (x>r*r)
{
System.out.println("该点在园外");
}else if (x==r*r)
{
System.out.println("该点在园上");
}else{
System.out.println("该点在园内");
}
}
}


class Point
{
private double x;
private double y;


Point (double x,double y){
this.x = x;
this.y = y;
}


public void setX(double x){
this.x = x;
}
public double getX(){
return x;
}


public void setY(double y){
this.y = y;
}
public double getY(){
return y;
}
}








2.李晓在家里开party,向朋友介绍家中的黄色的宠物狗【彩彩】具有两条腿走路的特异功能。


/*
  分析:
Person:
特性:姓名
行为:
开party
介绍宠物狗
Dog:
特性:
姓名
特异功能
颜色


*/




class Dog{
String name;
String color;
String func;
Dog(){}
Dog(String name, String color, String func){

this.name = name;
this.color = color;
this.func = func;
}
}


public class Person{
String name;

Person(){}
Person(String name){

this.name = name;
}


void makeParty(){

System.out.println(name + "开party");
}

void introduce(Dog d){

System.out.println(name + "介绍家中【"+d.color+"】宠物狗【"+d.name+"】具有【"+d.func+"】的特异功能");
}

public static void main(String[] args){


Dog d = new Dog("彩彩","黄色","两条腿独立行走");

Person p1 = new Person("李晓");
p1.makeParty();
p1.introduce(d);

}
}






3.王梅家的荷兰宠物猪【笨笨】跑丢了,她哭着贴寻猪启示。


/*
  分析:
Person:
特性:姓名
行为:

贴启示
Pig:
特性:
品种
行为:



*/




class Pig{
String brand;
String name;

Pig(){}
Pig(String brand, String name){

this.brand = brand;
this.name = name;
}

void run(){

System.out.println(name + "跑丢了");
}
}


public class Person{
String name;

Person(){}
Person(String name){

this.name = name;
}
void cry(){
System.out.println(name + "哭了");
}
void paste(){

System.out.println("贴寻猪启示");
}

public static void main(String[] args){

Pig d = new Pig("荷兰猪","笨笨");
d.run();

Person p1 = new Person("王梅");
p1.cry();
p1.paste();
}
}




4.富二代张三向女朋友李四介绍自己的新跑车:白色的宾利
/*
  分析:
Person:
特性:姓名
性别
行为:
介绍车
Car:
特性:
品牌
颜色




*/




class Car{
String brand;
String color;

Car(){}
Car(String brand, String color){
this.brand = brand;
this.color = color;
}
}
public class Person{
String name;
Person(){}
Person(String name){
this.name = name;
}
void introduce(Person d, Car c){
System.out.println(name + "向【"+d.name+"】介绍自己的新车【"+c.color+"】的【"+c.brand+"】");
       }
public static void main(String[] args){

Car c = new Car("宾利","白色");

Person p1 = new Person("李四");

Person p2 = new Person("张三");
p2.introduce(p1, c);

}
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值