Java after-school exercise 6

Source: Internet
Author: User
Tags float double mul stub

Hands on Brain 1: Read the Qipan.java sample program to learn how to draw a Gobang disk with two-dimensional arrays and circular statements

1) Source code:
Import java.io.*;

public class Qipan
{
Define a two-dimensional array to act as a chessboard
Private string[][] board;
Define the size of the board
private static int board_size = 15;
public void Initboard ()
{
Initializing an array of checkers
board = new String[board_size][board_size];
Assign each element "╋" to draw a chessboard on the console
for (int i = 0; i < board_size; i++)
{
for (int j = 0; J < Board_size; J + +)
{
BOARD[I][J] = "╋";
}
}
}
How to output a checkerboard in the console
public void Printboard ()
{
Print each array element
for (int i = 0; i < board_size; i++)
{
for (int j = 0; J < Board_size; J + +)
{
Print array elements without wrapping
System.out.print (Board[i][j]);
}
Outputs a newline character after each line of array elements is printed
System.out.print ("\ n");
}
}
public static void Main (string[] args) throws Exception
{
Qipan GB = new Qipan ();
Gb.initboard ();
Gb.printboard ();
This is the method for getting keyboard input
BufferedReader br = new BufferedReader (new InputStreamReader (system.in));
String inputstr = null;
System.out.println ("Please enter the coordinates of your chess, should be in X, y format:");
Br.readline (): Whenever you enter a line on the keyboard press ENTER, the content you just entered will be read by BR.
while ((Inputstr = Br.readline ()) = null)
{
Separates the user-entered string into 2 strings with a comma (,) as a delimiter
string[] Posstrarr = Inputstr.split (",");
Converts 2 strings to the coordinates of a user's chess
int xPos = Integer.parseint (posstrarr[0]);
int yPos = Integer.parseint (posstrarr[1]);
Assign the corresponding array element as "".
GB.BOARD[XPOS-1][YPOS-1] = "";
/*
The computer randomly generates 2 integers, which are assigned to the board array as the coordinates of a computer's chess game.
Also involves
1. The validity of the coordinates, can only be a number, not beyond the Board range
2. If the point of chess under, can not repeat chess.
3. After each play, you need to scan who wins.
*/
Gb.printboard ();
System.out.println ("Please enter the coordinates of your chess, should be in X, y format:");
}
}
}

2) Output Result:

Hands on Brain 2: Write a program to convert an integer to a kanji reading string. For example, "1123" is converted to "1123".
1) Source code:

Package Ceshi;


public class Bai
{
 private string[] Hanarr = {"0", "one", "II", "three", "the", "WU", "Lu", "Qi", "ba", "JIU" };
 private string[] Unitarr = {"Ten", "Hundred", "thousand", "Million", "100,000", "Million"};
 /**
  * Converts a four-bit numeric string into a kanji string
  * * @param numstr four-bit numeric string that needs to be converted
  * * @return four-bit numeric string is transferred The character string to replace.
  * */
 private string Tohanstr (String numstr)
 {
  string result = "";
  int Numlen = Numstr.length ();
  //iterates through each digit of the numeric string
  for (int i = 0; i < Numlen; i++)
  {
  & nbsp;//converts the char number to the int number, because their ASCII value is exactly the same as the
   //so the char number minus 48 gets the int type number, for example ' 4 ' is converted to 4.

int num = Numstr.charat (i)-48;
If it is not the last digit, and the number is not 0, you need to add units (thousand, Hundred, Ten)

if (i! = numLen-1 && num! = 0)
{
Result + = Hanarr[num] + unitarr[numlen-2-i];
}
Otherwise, do not add units
Else
{

Whether the previous number is "0" and not "0" when added
if (Result.length () >0 && hanarr[num].equals ("0") && Result.charat (Result.length ()-1) = = ' 0 ')
Continue
Result + = Hanarr[num];
}
}
Only single digit, direct return
if (Result.length () ==1)
return result;

int Index=result.length ()-1;
while (Result.charat (index) = = ' 0 ') {
index--;
}
if (Index!=result.length ()-1)
Return result.substring (0,index+1);
else {
return result;
}
}
public static void Main (string[] args)
{
Bai nr = new Bai ();
Test to turn a four-bit numeric string into a Chinese character string
System.out.println (Nr.tohanstr ("1123"));

}
}

2) Output Result:

Hands on Brain 3:

The previous introduction of the JDK provided by the BigInteger can complete the large number of calculations, if not use it, directly using the array to express large numbers, you can achieve the same function?
Requirements:
(1) Add and subtract two functions with your large number of classes
(2) Read the BigInteger class source code, figuring out what algorithm it uses to achieve subtraction four kinds of operations?
(3) through the Internet to find the large number of relevant data, to your large number of classes to add multiply, divide, and other functions to seek factorial.

1) Introduction to BigInteger history
In Java, there are many kinds of data types, such as byte short char int float double long, and BigInteger belongs to one of the more special data types, which is also the focus of this tutorial. BigInteger already exists in the JDK1.1, belonging to the Java.math package class. By name, BigInteger represents a larger range of values than an integer. The basic structure of the BigInteger class is as follows:
Java.lang.Object
|_java.lang.number
|_java.math.biginteger
BigInteger implemented interfaces: Serializable, comparable<biginteger>

2) BigInteger is an integer with immutable arbitrary precision. In all operations, BigInteger (such as the basic integer type of Java) are represented in twos complement form. BigInteger provides a counterpart to all Java's basic integer operators and provides all the relevant methods of Java.lang.Math. In addition, BigInteger provides the following operations: modulo arithmetic, GCD calculation, prime number test, prime generation, bit manipulation, and some other operations.

3) BigInteger Attribute analysis
Here's a look at the key attributes of BigInteger, the main ones are the following three:
1.final int Signum
The Signum attribute is designed to differentiate between positive and negative and 0 flags, which are clearly stated in the JDK notes:
The signum of this BigInteger:-1 for negative, 0 for zero, or 1 for positive. Note that the BigInteger zero must has a signum of 0. This was necessary to ensures that there is exactly one representation for each BigInteger value.
2.final int[] Mag
Mag is an abbreviated form of magnitude, the mag array is stored BigInteger numeric size, in the order of Big-endian, that is, the high-level bytes into the low address, low-bit bytes into the high address, in order to arrange the way. The original JDK notes are as follows:
The magnitude of this BigInteger, in Big-endian order:the zeroth element of this array is the most-significant int of the Magnitude. The magnitude must is "minimal" in the and the Most-significant Int (mag[0]) must be non-zero. This was necessary to ensure that there is exactly one representation for each BigInteger value. Note that this implies, the BigInteger Zero has a zero-length mag array.
3.final static Long long_mask = 0xffffffffL;
This mask was used to obtain the value of an int as if it were unsigned.

Source:

Package Ceshi;


Import Java.math.BigInteger;
public class Bai
{
public static void Main (String[]args)
{
TODO auto-generated Method Stub
BigInteger aa=new BigInteger ("100");
BigInteger bb=new BigInteger ("25");
BigInteger sub=aa.subtract (BB);///large integer minus
BigInteger Add=aa.add (BB);//The addition of large integers
BigInteger mul=aa.multiply (BB);//multiplication of large integers
BigInteger div=aa.divide (BB);//The addition of large integers
System.out.println ("Minus of large integers:" +sub.tostring ());
System.out.println ("Plus of large integers:" +add.tostring ());
System.out.println ("Multiplication of large integers:" +mul.tostring ());
SYSTEM.OUT.PRINTLN ("Except for large integers:" +div.tostring ());
}
}

Output Result:

After-school assignment 1: Randomly generate 10 numbers, populate an array, then display the array contents with a message box, and then calculate the array elements and the results are also displayed in the message box.

Design idea: First use the random structure to generate 10 random numbers, and then 10 number of input array, then calculate 10 number of the and the last output 10 numbers and

Source:

Package Ceshi;
Import javax.swing.*;

public class Bai {

public static void Main (string[] args) {
TODO auto-generated Method Stub

String output= "10 random numbers: \ n";
int sum=0;
int a []=new int [10];
for (int i = 0;i<10;i++)
{
a[i]= (int) (Math.random () *10);
Output + = "" +a[i];
Sum + = A[i];
}

Output + = "\ n 10 number of the and is:" +sum;

Joptionpane.showmessagedialog (Null,output, "results",
Joptionpane.plain_message);
}
}

Output Result:

Programming Summary: The use of random structure can generate stochastic number and then use Joptionpane.showmessagedialog (Null,output, "results",
joptionpane.plain_message); output number.

Java after-school exercise 6

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.