Exercisesin theJavaprogramming environment, create aJavaproject, the project name istest+number, and then complete the following programming questions to create a package for each programming question , with the package namedtest+problem number, for example, the first question is a packagetest1, and then complete the question under the packagerequirements of the project.
1. define an integer array, calculate the number of elements and the maximum and minimum values of the array elements, and output the results.
public class test{
public static void Main (string[] args) {
int x[] = new INT[10];
Scanner read = new Scanner (system.in);
System.out.println (" Please enter ten integers ");
for (int i=0;i<10;i++) {
X[i] = Read.nextint ();
}
Sort (x);
for (int i=0;i<10;i++)
{
System.out.print (X[i]);
System.out.print ("");
}
System.out.println ();
System.out.println ("The maximum value of the array is " +x[9]);
System.out.println ("The minimum value of the array is " +x[0]);
}
}
2. define an integer array and find the odd number and even number of them.
public class test{
public static void Main (string[] args) {
int x[] = new INT[10];
int js=0;
int os=0;
Scanner read = new Scanner (system.in);
System.out.println (" Please enter A total of more than 0 integers ");
for (int i=0;i<10;i++)
{
X[i] = Read.nextint ();
}
for (int i=0;i<10;i++)
{
if (x[i]%2==0) os++;
if (x[i]%2==1) js++;
}
System.out.println ("The number of odd numbers is " +js);
System.out.println ("The number of even numbers is " +os);
}
}
3. define an integer array, complete the descending sort and output each element.
public class test{
public static void Main (string[] args) {
int x[] = new INT[10];
Scanner read = new Scanner (system.in);
System.out.println (" Please enter ten integers ");
for (int i=0;i<10;i++)
{
X[i] = Read.nextint ();
}
Sort (x);
for (int i=9;i>=0;i--)
{
System.out.print (X[i]);
System.out.print ("");
}
}
}
4. design a Myinteger class, which includes:
(1) an int property value named value that stores the int value represented by the object , with a default value of 1;
(2) one for value Value Creation Myinteger the method of constructive and non-parametric construction ;
(3) Properties value of the Set and the Get Method ;
(4) if value is even, odd, and prime, then IsEven () , isodd () and the IsPrime () method will return true;
(5) if the specified value is even, odd, and prime, then isEven (int) , isodd (int) and the isprime (int) method will return true; implement this class and write test class tests.
Class Myinteger {
int value=1;
public Myinteger (int value)
{
This.value = value;
}
void SetValue (int value)
{
This.value = value;
}
void Get ()
{
System.out.print (value);
}
Boolean IsEven () {return isevent (value);
}
Boolean isodd () {return isodd (value);
}
Boolean IsPrime () {return isprime (value);
}
Boolean isevent (int value) {return value%2==0;
}
Boolean isodd (int value) {return value%2>0;
}
Boolean isprime (int value)
{
for (int i = 2;i<value;i++) {
if (value%i!=0) return false;
return true;
}
}
public class Test {
public static void Main (string[] args)
{
Myinteger mi = new Myinteger (2);
System.out.println (Mi.iseven ());
System.out.println (Mi.isodd ());
System.out.println (Mi.isprime ());
} }
5. Define the class of a vehicle (Vehicle) , properties: Velocity (speed), Volume (size) , etc. method has movement ( Move ()), set the velocity (setspeed (int speed)),accelerate speedUp (), decelerate Speeddown () and so on, with the method of constructive and non-parametric construction. Write test class testvehicle test.
Class Vehicle {
int speed;
String size;
public void Move () {
System.out.println ("I ' M moving");
}
public static void Main (string[] args) {
Bike B = new Bike ();
Bus U = new bus ();
B.start (0);
B.stop (0);
U.start (0);
U.stop (0); } }
9. create a class named comparablecircle that extends the Circle class and implements the Compare interface, where:
(1) Compare interface includes a comparison method compareto (), method head: public int CompareTo (object o) o o
(2) Circle radius 1 radius property set get method; A named getaera () method returns the area of the circle; a named getperimeter () method returns the perimeter of the circle class; A named tostring () method returns the string description of this round half price;
(3) The Comparablecircle class implements the interface CompareTo method, can compare two circle size, requires two circles according to the area to compare the size; write a test program to find out the largest of the two instances of the Comparablecircle class.
public interface compare{public int compareTo (Circle o); Class circle{private double Radius=1; Circle () {} circle (int radius) {This.radius = radius; } public void Setradius (double radius) {This.radius = radius; } public double Getradius () {
return radius; } public double Getaera () {return radius*radius*3.14; } public double Getperimeter () {return radius*2*3.14; } public void Rtostring () {System.out.println ("the radius of the circle is:"+radius); }} class Comparablecircle extends circle implements compare{public int compareTo (Circle o) {Double A = (int) (t His.getradius ()-o.getradius ()); int b = 0; if (a>0) b=1; if (a<0) b=-1; System.out.println (b); return 0; }} public class Test {public static void main (string[] args) {Circle Ci = new Circle (5); Comparablecircle C = new Comparablecircle (); C.compareto (Ci); } }
- take advantage of file byte stream (FileInputStream and fileoutputstream) complete the following requirements:
1) Create a file "Test.txt"in the current directory with fileoutputstreamand output "HelloWorld" to the file Appended to the contents of the original file if the file already exists.
2) Read the test.txt file with FileInputStream and print it out on the console test.txt Content in the.
3) requires the try-catch-finally to handle the exception, and the closing stream should be placed in the finally block. public class Test {public static void main (String args[]) {
FileOutputStream Out=null;
FileInputStream Fin=null;
try {
Out=new FileOutputStream ("Test.txt", true);
Fin = new FileInputStream ("test.txt");
Out.write ("HelloWorld". GetBytes ());
int c=0;
while ((C=fin.read ())!=-1) {
System.out.print ((char) c);
}
} catch (IOException e) {
}finally{
try {
Fin.close ();
Out.close ();
} catch (IOException e2) {}}}}
Java Basics Exercises