① java面向对象,编程题,谢谢!
第一种:
publicstaticvoidmain(Stringargs[]){
Scannerscn=newScanner(System.in);
System.out.print("第一个数:");
inta=scn.nextInt();
System.out.print("操作(+,-,*,/):");
Stringoption=scn.next();
System.out.print("第二个数:");
intb=scn.nextInt();
intresult=0;
if("+".equals(option)){
result=a+b;
}
if("-".equals(option)){
result=a-b;
}
if("*".equals(option)){
result=a*b;
}
if("/".equals(option)){
result=a/b;
}
System.out.println("结果:"+result);
}
第二种:
publicstaticvoidmain(Stringargs[]){
Scannerscn=newScanner(System.in);
System.out.print("请输入第一个数:");
intoNumFirst=scn.nextInt();
System.out.print("请输入操作方式(+,-,*,/):");
Stringoption=scn.next();
System.out.print("请输入第二个数:");
intoNumSecond=scn.nextInt();
intresult=0;
switch(option){
case"+":
result=oNumFirst+oNumSecond;
break;
case"-":
result=oNumFirst-oNumSecond;
break;
case"*":
result=oNumFirst*oNumSecond;
break;
case"/":
try{
result=oNumFirst/oNumSecond;
}catch(ArithmeticExceptione){
e.printStackTrace();
System.out.println("除数不能为0");
}
break;
}
System.out.println("结果为:"+result);
}
第三种:
/**
*计算器类
*CreatedbyHDLon2016/11/30.
*/
publicclassOptioner{
privateintoNumFirst;//第一个数
privateintoNumSecond;//第二个数
publicintgetoNumFirst(){
returnoNumFirst;
}
publicvoidsetoNumFirst(intoNumFirst){
this.oNumFirst=oNumFirst;
}
publicintgetoNumSecond(){
returnoNumSecond;
}
publicvoidsetoNumSecond(intoNumSecond){
this.oNumSecond=oNumSecond;
}
publicOptioner(){
}
publicOptioner(intoNumFirst,intoNumSecond){
this.oNumFirst=oNumFirst;
this.oNumSecond=oNumSecond;
}
publicintgetReuslt(){
return0;
}
}1617181920212223242526272829
/**
*加法运算
*CreatedbyHDLon2016/11/30.
*/
{
@Override
publicintgetReuslt(){
returngetoNumFirst()+getoNumSecond();
}
}
/**
*减法运算
*CreatedbyHDLon2016/11/30.
*/
{
@Override
publicintgetReuslt(){
returngetoNumFirst()-getoNumSecond();
}
}
/**
*乘法运算
*CreatedbyHDLon2016/11/30.
*/
{
@Override
publicintgetReuslt(){
returngetoNumFirst()*getoNumSecond();
}
}
/**
*除法运算
*CreatedbyHDLon2016/11/30.
*/
{
@Override
publicintgetReuslt(){
if(getoNumSecond()==0){
System.out.println("除数不能为0");
return-1;
}else{
returngetoNumFirst()/getoNumSecond();
}
}
}
测试
publicstaticvoidmain(Stringargs[]){
Scannerscn=newScanner(System.in);
System.out.print("请输入第一个数:");
intoNumFirst=scn.nextInt();
System.out.print("请输入操作方式(+,-,*,/):");
Stringopt=scn.next();
System.out.print("请输入第二个数:");
intoNumSecond=scn.nextInt();
Optioneroptioner=null;
switch(opt){
case"+":
optioner=newAddOptioner();
break;
case"-":
optioner=newSubOptioner();
break;
case"*":
optioner=newMulOptioner();
break;
case"/":
optioner=newDivOptioner();
break;
}
optioner.setoNumFirst(oNumFirst);
optioner.setoNumSecond(oNumSecond);
System.out.println("结果为:"+optioner.getReuslt());
}
② java基础有关面向对象,类的,2题
1.
public class School{
private double scoreLine; // 录取分数线
public School() {
}
public School(double scoreLine) {
this.scoreLine = scoreLine;
}
public double getScoreLine() {
return scoreLine;
}
public void setScoreLine(double scoreLine) {
this.scoreLine = scoreLine;
}
}
2.
/**
* 学生类
*/
public class Student {
private int id; // 考号
private String name; // 考生姓名
private double score; // 综合成绩
private double sports; // 体育成绩
/**
* 无参构造方法
*/
public Student() {
}
/**
*
* 构造方法说明:有参构造方法<br/>
* <br/>
*
* @param id 考号
* @param name 考生姓名
* @param score 综合成绩
* @param sports 体育成绩
*/
public Student(int id,String name, double score, double sports){
this.id = id;
this.name = name;
this.score = score;
this.sports = sports;
}
/**
*
* 方法说明:获取综合成绩<br/>
* <br/>
*
* @return 综合成绩
*/
public double getScore() {
return score;
}
/**
*
* 方法说明:获取体育成绩<br/>
* <br/>
*
* @return 体育成绩
*/
public double getSports() {
return sports;
}
}
③ (Java类和对象练习).
importjava.util.Arrays;
importjava.util.Comparator;
classStudent
{
publicStringstuNo;
publicStringstuName;
publicintstuAge;
publicStudent(StringstuNo,StringstuName,intstuAge)
{
.stuNo=stuNo;
this.stuName=stuName;
this.stuAge=stuAge;
}
publicvoidaddAge()
{
stuAge++;
}
publicStringtoString()
{
return"No:"+stuNo+"Name:"+stuName+"Age:"+stuAge;
}
}
<Student>
{
@Override
publicintcompare(Studento1,Studento2)
{
//TODOAuto-generatedmethodstub
returno1.stuNo.compareTo(o2.stuNo);
}
}
publicclassTest3
{
publicstaticvoidmain(String[]args)
{
Student[]students=newStudent[5];
students[0]=newStudent("1003","a",12);
students[1]=newStudent("1002","b",13);
students[2]=newStudent("1004","c",14);
students[3]=newStudent("1001","d",15);
students[4]=newStudent("1005","e",16);
Arrays.sort(students,newStudentOrder());
intcount=0;
for(inti=0;i<5;i++)
{
System.out.println(students[i]);
students[i].addAge();
if(students[i].stuAge>20)
count++;
}
System.out.println("大于20岁的人数:"+count);
}
}
④ Java面向对象程序设计考试题目 类的定义 继承 创建对象 构造方法
public class Geometry {
public Geometry(int w, int h) {
width = w;
height = h;
}
public int area() {
return width * height;
}
private int width, height;
}
public class Cube extends Geometry {
public Cube(int w, int h) {
super(w, h);
}
public Cube(int a, int b, int c) {
super(a, b);
height = c;
}
public void setHeight(int h) {
height = h;
}
public int volumn() {
return area() * height;
}
private int height;
}
public class User {
public static void main(String []args) {
Cube cube1 = new Cube(1,2,3);
Cube cube2 = new Cube(4, 5);
cube2.setHeight((int) (Math.random() * 10) + 1);//avoid zero height
System.out.println("Cube 1 area: " + cube1.area() + " volumn: " + cube1.volumn());
System.out.println("Cube 2 area: " + cube2.area() + " volumn: " + cube2.volumn());
}
}
⑤ java面向对象练习题,急求答案~ 【练习题】25.抽象类、继承、接口综合 设计一个系
abstract 水{
public void 水();
}
interface 过滤{}
interface 缓冲{}
interface 加热{}
interface 放糖{}
class 纯净水1 extends 水 imps 过滤,缓冲{}
class 纯净水2 extends 水 imps 缓冲{}
class 纯净水2 extends 水 imps 过滤{}
。。。。
abstract class Water
{
public abstract void Water();
}
interface Filter
{
public abstract void Filter();
}
interface Buffer
{
public abstract void Buffer();
}
interface Heat
{
public abstract void Heat();
}
interface Sugar
{
public abstract void Sugar();
}
class Water1 extends Water implements Filter,Buffer
{
public void Water(){
System.out.println("水1的制造过程:\n");
}
public void Filter(){
System.out.println("水已经过滤\n");
}
public void Buffer(){
System.out.println("水已经缓冲\n");
}
}
class Water2 extends Water implements Buffer
{
public void Water(){
System.out.println("水2的制造过程:\n");
}
public void Buffer(){
System.out.println("水已经缓冲\n");
}
}
class Water3 extends Water implements Filter
{
public void Water(){
System.out.println("水3的制造过程:\n");
}
public void Filter(){
System.out.println("水已经过滤\n");
}
}
public class b01
{
public static void main(String args[]){
Water1 a = new Water1();
Water2 b = new Water2();
Water3 c = new Water3();
a.Water();
a.Buffer();
a.Filter();
b.Water();
b.Buffer();
c.Water();
c.Filter();
}
}