① 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();
}
}