面向对象程序设计之试题

类和对象

  1. 定义一个菜谱类Recipe用来存储某种菜的制作过程等相关信息,其属性包括:菜谱名称、菜系(如:川菜、湘菜等)、烹饪时长(分钟)、所需的多种食材(字符串数组类型,假设每种菜所需食材不会超过10种)、操作步骤,在main方法中定义一个数组存储5个菜谱的信息。

    要求:

(1)按名称模糊查询并输出符合条件的菜谱的所有信息,要求实现查找包含特定名称的菜谱信息的方法:searchRecipesContainName(Recipe[] recipes, String name),例如:希望找到所有名称中包含牛肉的菜谱,可以使用字符串类String类的contains方法,来判断某个字符串中是否包含某个子串。

输出格式:

(2)按菜系查询并输出符合条件的菜谱的所有信息,要求能够实现查找某个菜系的菜谱信息的方法:searchRecipes(Recipe[] recipes, String style),例如:希望查找所有湘菜的菜谱(可以使用字符串类String类的equals方法,来判断某个字符串是否与指定字符串相同),输出格式同上。

(3)查询烹饪时间小于某个时长的菜谱并输出,要求实现查找方法:searchRecipeLessThan(Recipe[] recipes, int time),例如:希望查找烹饪时长小于30分钟的菜谱,输出格式同上。

(4)查询包含某种食材的菜谱信息并输出,要求实现查找方法:searchRecipeContainsFood(Recipe[] recipes, String food),例如:希望查找包含西红柿的菜谱,输出格式同上。

提示:

(1)定义的菜谱类:包括属性、属性的getter、setter方法。

(2)类中其他方法可自由发挥,比如打印菜谱信息的方法(如print()或toString())

(3)自己定义包含main方法的类,类中定义4个static方法,分别用来实现要求中规定的4个方法

package www.wy.ch01;
import java.util.Arrays;
public class Recipe {private String name;//名称private String style;//菜系private int time;//时长private String[] materials;//食材private String step;//步骤public String getName() {return name;}public void setName(String name) {this.name = name;}public String getStyle() {return style;}public void setStyle(String style) {this.style = style;}public int getTime() {return time;}public void setTime(int time) {this.time = time;}public String[] getMaterials() {return materials;}public void setMaterials(String[] materials) {this.materials = materials;}public String getStep() {return step;}public void setStep(String step) {this.step = step;}public void print() {System.out.println("菜谱名称:" + name);System.out.println("菜系:" + style);System.out.println("时长:" + time);System.out.print("所需食材:");for(String material : materials) {System.out.print(material + " ");}System.out.println();System.out.println("操作步骤:" + step);}}
package www.wy.ch01;
public class Test {public static void main(String[] args) {Recipe[] recipes = new Recipe[5];Recipe recipe1 = new Recipe();recipe1.setName("酱牛肉");recipe1.setStyle("家常菜");recipe1.setTime(120);String[] materials1 = {"牛腱子","黄豆酱油","黄酒","冰糖"};recipe1.setMaterials(materials1);recipe1.setStep("1.准备好主要食材;2.加入食材慢炖两至三小时");recipes[0] = recipe1;Recipe recipe2 = new Recipe();recipe2.setName("红烧牛肉");recipe2.setStyle("家常菜");recipe2.setTime(120);String[] materials2 = {"牛腩","牛筋","生抽","冰糖"};recipe2.setMaterials(materials2);recipe2.setStep("1.准备好主要食材;2.加入食材慢炖两至三小时");recipes[1] = recipe2;Recipe recipe3 = new Recipe();recipe3.setName("农家小炒肉");recipe3.setStyle("湘菜");recipe3.setTime(10);String[] materials3 = {"青椒","红椒","酱油肉","葱"};recipe3.setMaterials(materials3);recipe3.setStep("1.准备好主要食材并切块;2.锅内倒少许的油后放入葱花爆香,然后加入食材翻炒");recipes[2] = recipe3;Recipe recipe4 = new Recipe();recipe4.setName("西红柿炒鸡蛋");recipe4.setStyle("家常菜");recipe4.setTime(10);String[] materials4 = {"西红柿","鸡蛋"};recipe4.setMaterials(materials4);recipe4.setStep("1.西红柿切块,鸡蛋打散;2.先炒鸡蛋,再加入西红柿");recipes[3] = recipe4;Recipe recipe5 = new Recipe();recipe5.setName("水煮鱼");recipe5.setStyle("川菜");recipe5.setTime(30);String[] materials5 = {"鲤鱼","黄豆芽"};recipe5.setMaterials(materials5);recipe5.setStep("1.处理鲤鱼;2.加入食材炖5分钟即可");recipes[4] = recipe5;searchRecipesContainName(recipes, "鱼");//根据名称模糊查询
//      searchRecipes(recipes, "湘菜");//根据菜系查询
//      searchRecipeLessThan(recipes, 30);//根据做饭的时间查询
//      searchRecipeContainsFood(recipes,"西红柿");//根据做饭所需食材查询}public static void searchRecipesContainName(Recipe[] recipes,String name) {for(int i = 0; i < recipes.length; i++) {if(recipes[i].getName().contains(name)) {recipes[i].print();}}}public static void searchRecipes(Recipe[] recipes, String style) {for(int i = 0; i < recipes.length; i++) {if(recipes[i].getStyle().equals(style)) {recipes[i].print();}}}public static void searchRecipeLessThan(Recipe[] recipes, int time) {for(int i = 0; i < recipes.length; i++) {if(recipes[i].getTime() < time) {recipes[i].print();}}}public static void searchRecipeContainsFood(Recipe[] recipes, String food) {for(int i = 0; i < recipes.length; i++) {String[] foods = recipes[i].getMaterials();for(String f : foods) {if(f.equals(food)) {recipes[i].print();}}}}
}
  1. 定义一个复数类Complex类,该类需要满足以下条件:realPart表示复数的实数部分,imaginaryPart表示复数的虚数部分。

在该类中除了属性的getter、setter方法之外,还需要实现以下方法:

(1)一个Complex add(Complex anotherComplex)方法,将当前复数对象与形参复数对象相加,所得的结果仍然是一个复数值。

(2)一个Complex addAnther(int anotherReal, int anotherImaginary)方法,将当前复数对象与形参的实数和虚数部分表示的复数对象相加,所得结果仍然是一个复数。

(3)编写一个测试类,在main方法中创建两个复数1+2i和3+4i,并显示它们的和。

package com.homework0202;public class Complex {private int realPart;private int imaginaryPart;public int getRealPart() {return realPart;}public void setRealPart(int realPart) {this.realPart = realPart;}public int getImaginaryPart() {return imaginaryPart;}public void setImaginaryPart(int imaginaryPart) {this.imaginaryPart = imaginaryPart;}public Complex add(Complex a) {int r = realPart + a.getRealPart();int i = imaginaryPart + a.getImaginaryPart();Complex c = new Complex();c.setRealPart(r);c.setImaginaryPart(i);return c; }public Complex addAnther(int anotherReal, int anotherImaginary) {int r = realPart + anotherReal;int i = imaginaryPart + anotherImaginary;Complex c = new Complex();c.setRealPart(r);c.setImaginaryPart(i);return c; }public String print() {return realPart + "+" + imaginaryPart + "i";}
}
package com.homework0202;public class Test {public static void main(String[] args) {Complex c1 = new Complex();c1.setRealPart(1);c1.setImaginaryPart(2);Complex c2 = new Complex();c2.setRealPart(3);c2.setImaginaryPart(4);Complex c3 = c1.add(c2);System.out.println(c1.print() + "加上" + c2.print());System.out.println("结果是:" + c3.print());Complex c4 = c1.addAnther(4, 6);System.out.println(c4.print());}
}

面向对象程序设计之类和对象初级试题相关推荐

  1. java面向对象期末考试试题_《面向对象程序设计——java》期末考试试题2008a卷.doc...

    <面向对象程序设计--java>期末考试试题2008a卷.doc 还剩 6页未读, 继续阅读 下载文档到电脑,马上远离加班熬夜! 亲,喜欢就下载吧,价低环保! 内容要点: 第 7 页 共 ...

  2. 【渝粤教育】国家开放大学2018年春季 7405-21T面向对象程序设计(本) 参考试题

    科目编号:7405 座位号 2018-2019学年度第二学期期末考试 面向对象程序设计(本) 试题 2018年 7 月 一.单选题(本大题共10小题,每小题3分,共计30分) (★请考生务必将答案填入 ...

  3. mooc程序设计与算法(三)C++面向对象程序设计 类和对象提高 编程作业 5:魔兽世界之一:备战

    5:魔兽世界之一:备战 这道题是典型的,看了题目不想做题系列... 题目太长,看了,看不懂....后来,上网找了某博客中据说是老师的代码,研究了一下,觉得真的,c++面向对象编程还是很深奥的....下 ...

  4. 面向对象程序设计——类与对象的应用2

    一.实验目的: 掌握类和对象的概念.定义和使用方法. 掌握静态数据成员和const修饰的成员函数的用法. 掌握c++程序的一般结构. 二.实验内容: 在个人的活期储蓄账户类SavingsAccount ...

  5. java 麻将的发牌与洗牌_JAVA程序设计(11)-----面对对象初级设计 麻将 创建麻将牌 然后洗牌 发牌~ 恩 就这样...

    zzzzZZZZ 1.开始还想贴图的 实在太懒了-- 这是一张麻将 package com.lovo; import java.awt.Graphics; import java.awt.Image; ...

  6. 面向对象程序设计基本概念

    面向对象程序设计基本概念 重点: C++中的 多态 和 指针间接赋值 1): 多态的三个条件         a: 子类继承父类         b: 虚函数(虚表及虚表指针)         c: ...

  7. OOP_面向对象程序设计概述

    李际军老师"面向对象程序设计"课程第一课笔记整理 面向对象程序设计概述 20世纪90年代以来面向对象程序设计(Object Oriented Programming, 简称OOP) ...

  8. JavaSE-Adventure(VII) Java OOP 面向对象程序设计

    JavaSE-Adventure(VII) Java & OOP 面向对象程序设计 CONTENTS JavaSE-Adventure(VII) Java & OOP 面向对象程序设计 ...

  9. java实验Java面向对象编程_java 实验三 面向对象程序设计(无脑实验系列)

    实验7 运算符重载 (1)定义日期类,重载 "++"和"--"运算符,使之能处理两个日期类对象自增和自减运算,并且自增和自减又分为前缀和后缀运算.(可继续完善. ...

最新文章

  1. 5.java.lang.IndexOutOfBoundsException(数组下标越界异常)
  2. NLP技术中的Tokenization
  3. Optical_Flow(4)
  4. 使用Three.js的材质
  5. linux netstat java,Linux netstat介绍
  6. B样条曲线的一些基本性质
  7. 一键解决WinRAR的有关问题
  8. ArcGIS聚类分析
  9. 叶俊|知行合一创纪录|杭州创纪录企业管理咨询有限公司董事长简介
  10. ES07--性能调优03(全面考量)
  11. TypeScript等无法获取到歌曲播放的位置信息时,关于歌曲的续播
  12. python 基因测序_科学网-python3 计算 基因组测序结果文件 各碱基数目(个人练习)-靳泽星的博文...
  13. 使用Teamviewer实现远程控制安卓设备的实现过程记录
  14. 移动机器人室内定位技术综述
  15. eSDK 华为ICT能力开放平台
  16. mysql回表什么意思_什么是MYSQL回表查询
  17. QLabel类常用方法
  18. FCES2019第二天 | BY AI,AI技术赋能教育的N种可能
  19. 计算机类大学生竞赛经验分享
  20. QT安装段错误segmentation fault

热门文章

  1. vue 仿外卖app-数据mock部分
  2. 合成大西瓜漏洞作弊详解,教你如何生成三个大西瓜
  3. 《数据结构与算法分析》回溯算法之博弈——三连棋(tic tac toe)人机对战AI设计(αβ枝减)
  4. 【英语学习】英语语法术语表 English Grammar Terminology
  5. IMX6Q的DDR3初始化配置
  6. selenium中的三种等待方法
  7. oracle异地容灾备份 英文6,异地容灾备份的方案.doc
  8. 大学医学院有计算机专业吗,上大学时辛苦一点,将来工作轻松一点,这些专业可以做到...
  9. 命令行运行coppeliasim(vrep)出现/usr/lib/x86_64-linux-gnu/libQt5Core.so.5: version `Qt_5.12‘ not found
  10. 学python吧-Python为什么这么厉害? 不想成为专业码农? 来学习Python吧!