Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the inheritance and polymorphic realization of Java Farmers' River Crossing problem

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I would like to talk to you about how to understand the inheritance and polymorphic implementation of the problem of Java farmers crossing the river. Many people may not know much about it. In order to make you understand better, the editor summed up the following content for you. I hope you can get something from this article.

Topic description:

A farmer takes a wolf, a sheep and a cabbage to cross the river, there is only one boat, and the farmer can only take one animal or object to cross the river at a time, and when the farmer is away, the wolf will eat sheep, the sheep will eat cabbage. Please design a program, the user will choose the role to cross the river, and the system will automatically judge the outcome of the game: when a creature is eaten, the game fails, all the characters go to the other side of the river, and the game succeeds. Don't say much, just look at the code:

Package Test1;import java.util.Scanner;public class Client {static Farmer farmer = new Farmer (); public static void menu () {System.out.println ("= Please choose operation="); System.out.println ("\ t=1:Cross the river alone="); System.out.println ("\ t=2:Cross the river with (" + farmer.wolf.name+ ")" + "="); System.out.println ("\ t=3:Cross the river with (" + farmer.sheep.name+ ")" + "=") System.out.println ("\ t=4:Cross the river with (" + farmer.cabbage.name+ ")" + "="); System.out.println ("\ twee0System.out.println Quit ="); System.out.println ("= ="); System.out.println ("Input the number (04th):");} public static void show () / * output status (survival, location) of farmers, various animals and articles * / {System.out.println ("River Crossing status:") System.out.println (farmer.sheep.name+ ": whether on the left river:" + farmer.sheep.is_across_left+ "on the right river" + farmer.sheep.is_across_right+ "whether to survive:" + farmer.sheep.is_alive) / / if the sheep cross the river successfully, display false on the left side of the river and display true System.out.println on the right side of the river (farmer.cabbage.name+ ": whether the river is on the left: whether" + farmer.cabbage.is_across_left+ "is on the right river" + farmer.cabbage.is_across_right+ "whether to survive:" + farmer.cabbage.is_alive) / / if the cabbage crosses the river successfully, show false on the left and show true System.out.println on the right (farmer.wolf.name+ ": whether the river is on the left: whether" + farmer.wolf.is_across_left+ "is on the right river" + farmer.wolf.is_across_right+ "whether to survive:" + farmer.wolf.is_alive) / / if the wolf crosses the river successfully, display false on the left side of the river and display true System.out.println on the right side of the river ("farmer: whether the river is on the left:" + farmer.is_across_left+ "on the right river" + farmer.is_across_right) } public static void is_alive () {/ / judge whether sheep and cabbage are eaten by if (farmer.sheep.is_across_left==farmer.wolf.is_across_left&&farmer.sheep.is_across_right==farmer.wolf.is_across_right & & farmer.is_across_left==farmer.sheep.is_across_right&&farmer.is_across_right==farmer.sheep.is_across_left & & farmer.is_across_left==farmer.wolf.is_across_right&&farmer.is_) Across_right==farmer.wolf.is_across_left) {/ / if the sheep and the wolf are on the same side and the farmer is on the other side, the sheep will be eaten farmer.sheep.is_alive=false } if (farmer.sheep.is_across_left==farmer.cabbage.is_across_left&&farmer.sheep.is_across_right==farmer.cabbage.is_across_right & & farmer.is_across_left==farmer.cabbage.is_across_right&&farmer.is_across_right==farmer.cabbage.is_across_left & & farmer.is_across_left==farmer.sheep.is_across_right&&farmer.is_across_right==farmer.sheep.is_across_left) {/ / if sheep and cabbage On the same side and the farmer on the other side, the cabbage will be eaten farmer.cabbage.is_alive=false. }} public static int is_win () {/ / determine whether to cross the river successfully if (farmer.sheep.is_alive==false | | farmer.cabbage.is_alive==false) {return 0 / / if the sheep or cabbage is eaten, return 0 to exit the game directly and fail} if (farmer.is_across_right==true&&farmer.sheep.is_across_right==true&&farmer.wolf.is_across_right&&farmer.cabbage.is_across_right==true) {/ / if the farmer, sheep, wolf and cabbage are all on the right side of the river, return 1 game success return 1;} return 2 / / in other cases, continue} public static void main (String [] args) {/ / TODO Auto-generated method stub Scanner input = new Scanner (System.in); int choice = 0; int massif 2; / / set the initial value of m to 2 to indicate the situation in progress boolean gamevoer=false,win=false; while (! gamevoer) {if (masking 1 | | masking 0) {/ / if massif 0 or 1, directly exit and display the game result break;} menu (); choice = input.nextInt () System.out.println ("\ n"); switch (choice) {case 0: gamevoer=true; break; case 1: {farmer.cross_alone (); / * the treatment of the farmer crossing the river alone * / / the judgment of the farmer's position is_alive (); show (); m=is_win (); / / m is used to record the return value of the method 0jin1 if 2 if (mechin1) / / if the river crossing is successful {win=true;// direct output game success} break } / / the following situations are similar to case 2: {farmer.cross_wolf (); / * treatment of farmers with wolves * / is_alive (); show (); m=is_win (); if (farmer.cross_sheep ()) {win=true;} break;} case 3: {farmer.cross_sheep (); / * treatment of farmers with sheep * / is_alive (); show (); m=is_win (); if (farmers with wolves) {win=true;} break } case 4: {farmer.cross_cabbage (); / * treatment of farmers with cabbage * / is_alive (); show (); m=is_win (); if (win=true;} break;} if (win) {System.out.println ("game over: you win!");} else {System.out.println ("game over: you lose!");} input.close ();}}}

Package Test1;public class Cabbage extends wuti {public Cabbage () {super.name= "cabbage";}}

Package Test1;public class Farmer {boolean is_across_left = true; / / the left side of the default river is the starting side boolean is_across_right = false; Sheep sheep = new Sheep (); Wolf wolf = new Wolf (); Cabbage cabbage = new Cabbage () Public void cross_cabbage () {if (cabbage.is_across_left==is_across_left | | cabbage.is_across_right==is_across_right) {/ / if the cabbage farmer is on one side if (cabbage.is_across_left==false) {/ / cabbage right to left cabbage.is_across_left=true; cabbage.is_across_right=false;} else {/ / cabbage left to right cabbage.is_across_left=false; cabbage.is_across_right=true } if (is_across_left==false) {/ / Farmer right to left is_across_left=true; is_across_right=false;} else {/ / Farmer left to right is_across_left=false; is_across_right=true;} else {/ / if the cabbage farmer is not on one side, the cabbage cannot cross the river System.out.println (cabbage.name+ "no longer on the farmer's side") }} public void cross_sheep () {if (sheep.is_across_left==is_across_left | | sheep.is_across_right==is_across_right) {/ / if the sheep farmer is on one side if (sheep.is_across_left==false) {/ / sheep right to left sheep.is_across_left=true; sheep.is_across_right=false;} else {/ / sheep left to right sheep.is_across_left=false; sheep.is_across_right=true } if (is_across_left==false) {/ / Farmer right to left is_across_left=true; is_across_right=false;} else {/ / Farmer left to right is_across_left=false; is_across_right=true;} else {/ / if the sheep farmer is not on one side, the sheep cannot cross the river System.out.println (sheep.name+ "not on the farmer's side") }} public void cross_wolf () {if (wolf.is_across_left==is_across_left | | wolf.is_across_right==is_across_right) {/ / if the wolf farmer is on one side if (wolf.is_across_left==false) {/ / Wolf right to left wolf.is_across_left=true; wolf.is_across_right=false;} else {/ / Wolf left to right wolf.is_across_left=false; wolf.is_across_right=true } if (is_across_left==false) {/ / Farmer right to left is_across_left=true; is_across_right=false;} else {/ / Farmer left to right is_across_left=false; is_across_right=true;} else {/ / if the wolf farmer is not on one side, the wolf cannot cross the river System.out.println (wolf.name+ "no longer on the farmer's side") } public void cross_alone () {if (is_across_left==false) {/ / Farmer right to left is_across_left=true; is_across_right=false;} else {/ / Farmer left to right is_across_left=false; is_across_right=true;}

Package Test1;public class Sheep extends wuti {public Sheep () {super.name= "sheep";}}

Package Test1;public class Wolf extends wuti {public Wolf () {super.name= "wolf";}}

Package Test1;public class wuti {String name; boolean is_across_left = true; boolean is_across_right = false; boolean is_alive = true;}

First, create an object class wuti.java, which includes name to indicate the objects to cross the river. Is_across_left indicates that it is on the left side of the river, default is true, is_across_right indicates that it is on the right side of the river, default is false, is_alive indicates that the object is not eaten, this class is the parent class, and there will be three classes inherited from this class, which are Cabbage,Sheep,Wolf classes, but there are only constructions that represent names respectively. Although simple, it is good for game changes, such as changing wolves, sheep and cabbage to foxes, rabbits and carrots, so you only need to change super.name to the name of the object you need. Then create a Farmer class, including cross_cabbage () method, cross_sheep () method, cross_wolf () method, these three methods are used to represent the farmer and cabbage, sheep, wolf crossing the river, and the farmer must be on the side of the object, otherwise the output will indicate that the object is different from the farmer's position can not cross the river. Finally, create a Client class that includes the menu () method, the show () method, the is_alive () method, the is_win () method, and the menu () method to display a menu-like selection that allows you to choose if you want to cross the river. The show () method outputs the status (survival, location) of farmers, various animals, and objects. If the object crosses the river successfully, the left side of the river (is_across_left) shows false and the right side of the river (is_across_right) shows true. The is_alive () method is used to judge whether the sheep and cabbage are eaten. If the sheep and the wolf are on the same side and the farmer is on the other side, the sheep will be eaten. If the sheep and cabbage are on the same side and the farmer is on the other side, the cabbage will be eaten. Is_win () method to determine whether to successfully cross the river, if the sheep or cabbage is eaten, return 0 directly exit the game failure, if the farmer sheep wolf cabbage are all on the right side of the river, then return 1 game success. In the main method, the initial value of m is set to 2 to indicate the situation in progress. In the while loop, if m is 0 or 1, the game result is displayed directly. In the switch statement, case0,1,2,3,4 selects the object crossing the river respectively. Similar methods such as farmer.cross_alone () in case indicate the position of the farmer and the object crossing the river and whether the crossing is successful. The is_alive () method indicates to judge whether the object has been eaten or not. The show () method outputs the status (survival, location) of farmers, all kinds of animals and objects, and the is_win () method determines whether to cross the river successfully. Skillfully use the value of m to judge whether crossing the river is successful or not.

After reading the above, do you have any further understanding of how to understand the inheritance and polymorphic implementation of the Java farmer crossing problem? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report