Answered You can hire a professional tutor to get the answer.

QUESTION

Hey all i was wondering how to run a program using CMD with two separate java files. If i need to turn them into class files do i change the...

Hey all i was wondering how to run a program using CMD with two separate java files. If i need to turn them into class files do i change the extension by simply switching .java to .class. The two codes im trying to run at the same time is:

import java.awt.* ;

import java.util.Random;

import java.lang.reflect.Field;

public class Guitar{

// datafield

private int numStrings;

private double guitarLength ;

private String guitarManufacturer ;

private Color guitarColor ;

//not arg constructor Guitar

public Guitar(){

   numStrings = 6;

guitarLength = 28.2;

guitarManufacturer = "Gibson";

     guitarColor = Color.red;

}

//constructor

public Guitar(int NumberOfStrings ,double length, String manufacturer, Color color ){

numStrings = NumberOfStrings;

guitarLength = length;

guitarManufacturer = manufacturer;

     guitarColor =color;

}

//getNumStrings

public int getNumStrings(){

return numStrings;

}

//getGuitarLength

public double getGuitarLength(){

return guitarLength;

}

//getGuitarManufacturer

public String getGuitarManufacturer(){

return guitarManufacturer;

}

//getGuitarColor

public Color getGuitarColor(){

return guitarColor;

}

//playGuitar

public String playGuitar( ) {

int first,second;

Random random = new Random();

double[] DurationValues = {0.25,0.5,1,2,4};

char[] character = {'A','B','C','D','E','F','G'};

String output = "[";

for (int i=0 ; i <= 15 ; i++) {

first = random.nextInt(7);

second = random.nextInt(5);

output += character[first] + "(" + String.valueOf(DurationValues[second]) + ")";

if (i < 15){

output = output + ",";

            }

}

return output += ']';

}

// toString

public String toString(){

String infoString = "(numStrings="+getNumStrings()+", Length="+getGuitarLength()+

", manufacturer="+getGuitarManufacturer()+", color="+getNameReflection(getGuitarColor())+")";

return infoString;

}

//getNameReflection to get name of color

public static String getNameReflection(Color colorParam) {

    try {

      Field[] field = Class.forName("java.awt.Color").getDeclaredFields();

      for (Field f : field) {

        String colorName = f.getName();

        Class<?> t = f.getType();

        if (t == java.awt.Color.class) {

          Color defined = (Color) f.get(null);

          if (defined.equals(colorParam)) {

            return colorName.toUpperCase();

          }

        }

      }

    } catch (Exception e) {

      System.out.println("Error... " + e.toString());

    }

    return " ";

  }

}

/*

filename: TestGuitar

*/

import java.awt.Color;

public class TestGuitar {

public static void main(String[] args) {

Guitar test1 = new Guitar(7, 30.2, "Fender", Color.BLACK);

Guitar test2 = new Guitar();

Guitar test3 = new Guitar(9, 50.3, "johandark", Color.cyan);

// test1

 System.out.println("***Output***");

 System.out.println("toString(): "+test1.toString());

System.out.println("getNumStrings(): "+test1.getNumStrings());

System.out.println("getGuitarLength(): "+test1.getGuitarLength());

System.out.println("getGuitarManufacturer(): "+test1.getGuitarManufacturer());

System.out.println("getGuitarColor(): "+test1.getNameReflection(test1.getGuitarColor()));

 System.out.println("playGuitar(): "+test1.playGuitar()+"nn");

// test2

 System.out.println("***Output***");

 System.out.println("toString(): "+test2.toString());

System.out.println("getNumStrings(): "+test2.getNumStrings());

System.out.println("getGuitarLength(): "+test2.getGuitarLength());

System.out.println("getGuitarManufacturer(): "+test2.getGuitarManufacturer());

System.out.println("getGuitarColor(): "+test2.getNameReflection(test2.getGuitarColor()));

 System.out.println("playGuitar(): "+test2.playGuitar()+"nn");

// test3

 System.out.println("***Output***");

 System.out.println("toString(): "+test3.toString());

System.out.println("getNumStrings(): "+test3.getNumStrings());

System.out.println("getGuitarLength(): "+test3.getGuitarLength());

System.out.println("getGuitarManufacturer(): "+test3.getGuitarManufacturer());

System.out.println("getGuitarColor(): "+test3.getNameReflection(test3.getGuitarColor()));

 System.out.println("playGuitar(): "+test3.playGuitar());

}

}

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question