实验⼀Java编程基础
⾯向对象——Java实验报告
实验⼀: Java基础编程
实验⼀ Java编程基础
<center>
<strong>姓名:</strong>
<u>XXX</u>
<strong>班级:</strong>
<u>XXXXX</u>
<strong>学号:</strong>
<u>XXXXXXXXXXXX</u></center>
⼀、实验⽬的
在集成开发环境下,依照Java语⾔的基本语法编译和执⾏简单Java的程序,通过本次实践环节理解java程序的运⾏机制,掌握 java 语⾔的基本结构和程序设计⽅法。
⼆、实验⽬标
(1)能够运⽤集成开发⼯具开发简单的Java程序,理解java语⾔的语法规则;
(2)能够利⽤Java语⾔解决实际问题,进⼀步熟悉和掌握java语⾔的程序设计的⽅法。
三、实验内容
3.1 实验环境
IntelliJ IDEA Community Edition 2021.1 x64 + openjdk-16.0.1
3.2 实验内容
⼩明带了20元来商店买⼀本书,剩余的钱还可以买哪种商品?可以购买⼏件?剩余多少钱?
商品价格表
商品名称价格
⽜奶5元
书10元
橡⽪1元
尺⼦2元
定义⼀个计算器类,该类中包含加、减、乘、除4个静态⽅法,并定义⼀个测试类,调⽤对应的⽅法进⾏四则运算。
打印100-999以内的⽔仙花数(⽔仙花数即每个位数上数字的3次幂之和等于数字它本⾝,如153=1*1*1+5*5*5+3*3*3)。
在类中定义⼀个静态⽅法printStar打印⼀颗树,该⽅法没有返回值,需要⼀个整形参数number定义树
的⾼度,在主⽅法中调⽤这个⽅法,number值为5和10时得到下图的结果。
四、实验过程分析
4.1 实验步骤
1.⼩明带了20元来商店买⼀本书,剩余的钱还可以买哪种商品?可以购买⼏件?剩余多少钱?
实验代码
public class experimentOne {
public static void main(String[] args){
int money =20-10;
int count =0;
for(int i =0; i <= money/1; i++){
for(int j =0; j <= money /2; j++){
for(int k =0; k <= money /5; k++){
for(int g =0; g <= money /10; g++){
int sum = i *1+ j *2+ k *5+ g *10;
if(sum <= money){
System.out.printf("⽅案%2d,买橡⽪%2d个、买尺⼦%2d把、买⽜奶%2d瓶、买书%2d本,剩余%2d元\n",++count, i, j, k, g, money - sum);
}else break;
}
}
}
}
System.out.printf("总共%d种⽅案.\n", count);
}
}
结果输出
C:\Users\a\.jdks\openjdk-16.0.1\ "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.1\lib\idea_rt.jar=64278:C:\Progra m Files\JetBrains\IntelliJ IDEA Community Edition 2021.1\b in" -ding=UTF-8 -classpath "C:\Users\a\Documents\Tencent Files\lession\out\producti on\lession" experimentOnenextint()方法
⽅案1,买橡⽪0个、买尺⼦0把、买⽜奶0瓶、买书0本,剩余10元
⽅案2,买橡⽪0个、买尺⼦0把、买⽜奶0瓶、买书1本,剩余0元
… …
⽅案49,买橡⽪9个、买尺⼦0把、买⽜奶0瓶、买书0本,剩余1元
⽅案50,买橡⽪10个、买尺⼦0把、买⽜奶0瓶、买书0本,剩余0元
总共50种⽅案.
Process finished with exit code 0
实验代码
import Scanner;
public class experimentTwo {
public static void main(String[] args){
Scanner cin =new Scanner(System.in);
int score = Int();
score /=10;
if(score >10|| score <0)
return;
switch(score){
case10:
case9:
System.out.println("优秀");
break;
case8:
System.out.println("良好");
break;
case7:
System.out.println("中");
break;
case6:
System.out.println("及格");
break;
default:
System.out.println("不及格");
break;
}
}
}
结果输出
C:\Users\a\.jdks\openjdk-16.0.1\ "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.1\lib\idea_rt.jar=59140:C:\Progra m Files\JetBrains\IntelliJ IDEA Community Edition 2021.1\b in" -ding=UTF-8 -classpath "C:\Users\a\Documents\Tencent Files\lession\out\producti on\lession" experimentTwo
60
及格
Process finished with exit code 0
C:\Users\a\.jdks\openjdk-16.0.1\ "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.1\lib\idea_rt.jar=62215:C:\Progra m Files\JetBrains\IntelliJ IDEA Community Edition 2021.1\b in" -ding=UTF-8 -classpath "C:\Users\a\Documents\Tencent Files\lession\out\producti on\lession" experimentTwo
75
中
Process finished with exit code 0
3.定义⼀个计算器类,该类中包含加、减、乘、除4个静态⽅法,并定义⼀个测试类,调⽤对应的⽅法进⾏四则运算。
实验代码
import Scanner;
class Calculator {
public Calculator(){}
public static int add(int n,int m){return n + m;}
public static int sub(int n,int m){return n - m;}
public static int mult(int n,int m){return n * m;}
public static int div(int n,int m){
try{return n / m;}
catch(ArithmeticException e){
System.out.println("/ by zero.");
return0;
}
}
}
class test {
public static void testCa(){
Calculator cA =new Calculator();
Scanner cin =new Scanner(System.in);
int n = Int();
int m = Int();
System.out.println("add:"+cA.add(n, m));
System.out.println("mult:"+cA.mult(n, m));
System.out.println("sub:"+cA.sub(n, m));
System.out.println("div:"+cA.div(n, m));
}
}
public class experimentThree {
public static void main(String[] args){ stCa();}
}
结果输出
C:\Users\a\.jdks\openjdk-16.0.1\ "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.1\lib\idea_rt.jar=55362:C:\Progra m Files\JetBrains\IntelliJ IDEA Community Edition 2021.1\b in" -ding=UTF-8 -classpath "C:\Users\a\Documents\Tencent Files\lession\out\producti on\lession" experimentThree
1520
add:35
mult:300
sub:-5
div:0
Process finished with exit code 0
C:\Users\a\.jdks\openjdk-16.0.1\ "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.1\lib\idea_rt.jar=61776:C:\Progra m Files\JetBrains\IntelliJ IDEA Community Edition 2021.1\b in" -ding=UTF-8 -classpath "C:\Users\a\Documents\Tencent Files\lession\out\producti on\lession" experimentThree
150
add:15
mult:0
sub:15
/ by zero.
div:0
Process finished with exit code 0
4.打印100-999以内的⽔仙花数(⽔仙花数即每个位数上数字的3次幂之和等于数字它本⾝,如153=1*1*1+5*5*5+3*3*3)。
实验代码
public class experimentFour {
public static void main(String[] args){
for(int i =100; i <=999; i++){
if(flower.isFlower(i))
System.out.println(i);
}
}
}
class flower {
public flower(){}
public static boolean isFlower(int n){
int sum =0;
int N = n;
while(N >0){
int temp = N %10;
sum = sum + temp * temp * temp;
N /=10;
}
return sum == n;
}
}
结果输出
C:\Users\a\.jdks\openjdk-16.0.1\ "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.1\lib\idea_rt.jar=60409:C:\Progra m Files\JetBrains\IntelliJ IDEA Community Edition 2021.1\b in" -ding=UTF-8 -classpath "C:\Users\a\Documents\Tencent Files\lession\out\producti on\lession" experimentFour
153
370
371
407
Process finished with exit code 0
5.在类中定义⼀个静态⽅法printStar打印⼀颗树,该⽅法没有返回值,需要⼀个整形参数number定义树的⾼度,在主⽅法中调⽤这个⽅法,number
值为5和10时得到下图的结果。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论