I'm trying to transpose a note, but the result it returns is not what it should be. Could you please check my code and tell me where I am wrong?
public int changeTone(String chord) {
int changeTone = 0;
switch(chord) {
case "I":
changeTone = 0;
break;
case "II":
changeTone = 1;
break;
case "III":
changeTone = 2;
break;
case "IV":
changeTone = 3;
break;
case "V":
changeTone = 4;
break;
case "VI":
changeTone = 5;
break;
case "VII":
changeTone = 6;
break;
case "i":
changeTone = 0;
break;
case "ii":
changeTone = 2;
break;
case "iii":
changeTone = 4;
break;
case "iv":
changeTone = 5;
break;
case "v":
canviDeTo = 7;
break;
case "vi":
changeTone = 9;
break;
case "vii":
changeTone = 11;
break;
default:
System.out.println("Vaya");
break;
}
return changeTone ;
}
public String getChord(int interval) {
String chord = "";
switch(interval) {
case "0":
chord = "C";
break;
case "2":
chord = "D";
break;
case "4":
chord = "E";
break;
case "5":
chord = "F";
break;
case "7":
chord = "G";
break;
case "9":
chord = "A";
break;
case "11":
chord = "B";
}
return chord;
}
public String WriteChord(String chords, String tone) {
String finalChord;
String[] chordArray = chords.split("-");
for(int i=0; i < chordArray.length; i++){
String chord = chordArray[i];
int interval = changeTone(chord);
chord = getChord(interval);
Note note = new Note(chord);
finalChord += note.changeValue(interval).toString() += "-";
}
return finalChord;
}
OK, so what this tries to do is to change the value of a chord given a chord progession with intervals. Like I-III-IV-iv. The user would choose a tone (the tonical, I note) and the chord would be changed taking the note as a reference. So, for example, running the code should do the following:
The user chooses a tone, say "E".
The code generates a chord progression, say "I-III-IV-iv".
The code gets the interval between I and III, I and IV, and I and iv.
The initial note, "E", changes its value with the interval.
The expected output is:
E-G#-A#-A#
The actual output is:
C-G#-G#-Bb
Why doesn't this work? I have simplified my code, so if you need a bit more let me know! Thanks in advance.
Edit: I have corrected the code and added the expected/gotten output.
JFugue already has support for intervals and chord progressions. I hope the following code satisfies your needs:
import org.jfugue.theory.ChordProgression;
public static void main(String[] args) {
ChordProgression cp = new ChordProgression("I-III-IV-iv").setKey("E");
System.out.println(cp);
}
The output from this code is:
E4MAJ G#4MAJ A4MAJ A4MIN
You can play this directly in a Player:
Player player = new Player(); // Don't forget to import org.jfugue.player.Player
player.play(cp);
If that's playing a little too fast for you because of the default duration of a quarter note, you can play each chord as something longer, say a whole note:
player.play(cp.eachChordAs("$!w"));
If you just want the roots, which in this case are [E, G#, A, A] (if you see an error with my music theory, please let me know), you can use:
cp.eachChordAs("$0");
There are several Chord Progression examples at http://www.jfugue.org/examples.html
Related
I am trying to convert a roman numeral entered by the user into the correct value it represents.
My code:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Please enter the a roman numberal (I, V, X, L, C, D, M): ");
String romanNumeral = in.nextLine();
romanNumeralToInt(romanNumeral);
System.out.println(romanNumeral);
}
public static int romanNumeralToInt(String romanNumeral) {
switch (romanNumeral) {
case "I":
romanNumeral = 1;
break;
case "V":
romanNumeral = 5;
break;
case "X":
romanNumeral = 10;
break;
case "L":
romanNumeral = 50;
break;
case "C":
romanNumeral = 100;
break;
case "D":
romanNumeral = 500;
break;
case "M":
romanNumeral = 1000;
break;
}
}
I have tried numerous different ways of converting this. I tried to use char instead of string but the same problem came up.
I have also tried to use the line:
public static int romanNumeralToInt(String romanNumeral, int romanDecimal) {
and then using:
case "I":
romanDecimal = 1;
break;
Which didn't work.
I have tried printing out just System.out.println(romanNumeral) which also didn't work.
I am kinda stuck and don't really understand how I go about returning a value as an integer when it was inputted as a string in a method
The current code you have pasted here does not seem right. No return written for the method, even though it clearly has an int return type. Was it compiled correctly?
Anyhow, change your method to have a return;
public static int romanNumeralToInt(String romanNumeral) {
int intNumeral = 0;
switch (romanNumeral) {
case "I":
intNumeral = 1;
break;
case "V":
intNumeral = 5;
break;
case "X":
intNumeral = 10;
break;
case "L":
intNumeral = 50;
break;
case "C":
intNumeral = 100;
break;
case "D":
intNumeral = 500;
break;
case "M":
intNumeral = 1000;
break;
}
return intNumeral;
}
Then in the main method let the methods return variable be stored in an int type variable;
int intNumericConverted = romanNumeralToInt(romanNumeral);
Welcome to stackoverflow!
My first thought is that you're not using your return value from your function, to do this, add a return statement instead of a break statement.
it should read
case "I":
return 1;
then return from it:
System.out.println(romanNumeralToInt("I"));
Or , simply make sure youre assigning strings instead of integers inside your function :
case "I":
romanNumeral = "1";
break;
but to accomplish this, you'll need to change the scope of romanNumeral to global if im not mistaken and this wouldnt be the best practice, in my opinion
You don't need to convert it you can simply return integer values like this:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Please enter the a roman numberal (I, V, X, L, C, D, M): ");
String romanNumeral = in.nextLine();
romanNumeralToInt(romanNumeral);
System.out.println(romanNumeral);
}
public static int romanNumeralToInt(String romanNumeral) {
switch (romanNumeral) {
case "I":
return 1;
case "V":
return 5;
case "X":
return 10;
case "L":
return 50;
case "C":
return 100;
case "D":
return 500;
case "M":
return 1000;
}
}
Integer class has static method toString() - you can use it:
int i = 1234;
String str = Integer.toString(i);
Returns a String object representing the specified integer. The argument is converted to signed decimal representation and returned as a string, exactly as if the argument and radix 10 were given as arguments to the toString(int, int) method.
I have to create a scrabble word search for my data structures class. I haven't reached the actual search yet. First, I need to create a bag of scrabble tiles. However, I keep getting errors when trying to add ScrabbleTile objects to my bag.
I have four classes: ScrabbleTile, ScrabbleBag, ScrabbleHand, and WordFinder.
Here is ScrabbleTile:
public class ScrabbleTile {
private char letter;
private int points;
ScrabbleTile (char letter)
{
this.letter = letter;
switch (letter)
{
case '_':
points = 0;
case 'e':
case 'a':
case 'i':
case 'o':
case 'n':
case 'r':
case 't':
case 'l':
case 's':
case 'u':
points = 1; break;
case 'd':
case 'g':
points = 2; break;
case 'b':
case 'c':
case 'm':
case 'p':
points = 3; break;
case 'f':
case 'h':
case 'v':
case 'w':
case 'y':
points = 4; break;
case 'k':
points = 5; break;
case 'j':
case 'x':
points = 8; break;
case 'q':
case 'z':
points = 10; break;
default: System.out.println("Incorrect character. Please enter a lowercase letter, a-z.");
break;
}
}
public char getLetter()
{
return letter;
}
public int getPoints()
{
return points;
}
}
Here is my ScrabbleBag class:
import DSLib.*;
public class ScrabbleBag {
private BagADT<ScrabbleTile> letterBag;
ScrabbleBag()
{
letterBag = new Bag<>();
for (int i = 0; i < 12; i++) {letterBag.add(ScrabbleTile('e'));}
}
}
In the ScrabbleBag constructor, I'm trying to add the correct number of each letter tile, starting with "e". Netbeans had a few suggestions which I tried, but then it was telling me the line syntax was wrong, after creating a few more instance variables in the ScrabbleBag class. How can I properly add ScrabbleTile objects to the bag?
My professor was very clear that we cannot use methods ahead of what we've gone over in class. Thanks in advance!
I think you missed new when create ScrabbleTitle object
for (int i = 0; i < 12; i++) {
letterBag.add(new ScrabbleTile('e'));
}
To create a ScrabbleTile you need the new keyword:
for (int i = 0; i < 12; i++) {
ScrabbleTile tile = new ScrabbleTile('e');
letterBag.add(tile);
}
I am working on a rather unique coding situation in Java. The purpose of the program I am trying to write is to take an Amateur Radio call sign, convert the letters in the call sign into numbers as defined by a list or other structure, treat the number as its face integer value, and run these numbers through several mathematical operations to output a unique "User Code" at the end. The length of characters, as well as the number itself, will vary from user to user based on their call sign, which is fine. The biggest obstacle I have encountered is that I do not want the letters to be assigned values in a 1-26 or 0-25 type pattern. I will post my code in a moment to show you. For the moment, the end use of this User Code is unimportant to this example, but suffice it to say that since I will be the only one using this particular code example I am not very concerned with doing validity checks or the like as I will ensure the integrity of the input data manually. With this being said, I do have a working solution which I will post here, but my question is not "it doesn't work" because it does work, my problem is that it is, in my opinion, bloated, and something tells me it could be cut down considerably. Here is the code, and following are some alternatives I considered but rejected:
import java.util.*;
import java.io.*;
public class UserCode
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int baseNumber = 0;
int finalNumber;
String callSign;
System.out.println("Enter CallSign for Code Generation: ");
callSign = in.nextLine();
String s = callSign.toUpperCase();
for (int i = 0; i < s.length(); i++)
{
char c = s.charAt(i);
if (Character.isDigit(c))
{
int l = Character.getNumericValue(c);
baseNumber = baseNumber + l;
}
else if (Character.isLetter(c))
{
int letNum = 0;
switch (c)
{
case 'A':
letNum = 23;
break;
case 'B':
letNum = 17;
break;
case 'C':
letNum = 5;
break;
case 'D':
letNum = 11;
break;
case 'E':
letNum = 34;
break;
case 'F':
letNum = 18;
break;
case 'G':
letNum = 13;
break;
case 'H':
letNum = 31;
break;
case 'I':
letNum = 27;
break;
case 'J':
letNum = 25;
break;
case 'K':
letNum = 7;
break;
case 'L':
letNum = 25;
break;
case 'M':
letNum = 33;
break;
case 'N':
letNum = 26;
break;
case 'O':
letNum = 28;
break;
case 'P':
letNum = 16;
break;
case 'Q':
letNum = 14;
break;
case 'R':
letNum = 2;
break;
case 'S':
letNum = 4;
break;
case 'T':
letNum = 6;
break;
case 'U':
letNum = 8;
break;
case 'V':
letNum = 10;
break;
case 'W':
letNum = 37;
break;
case 'X':
letNum = 12;
break;
case 'Y':
letNum = 3;
break;
case 'Z':
letNum = 1;
break;
default:
System.out.println("Call Contains a bad character. Try again. \n");
}
baseNumber = baseNumber + letNum;
}
}
System.out.println("\n");
String baseStr = Integer.toString(baseNumber);
System.out.println("The Base number is: " + baseStr + "\n");
int sMod = baseNumber%7;
String sModStr = Integer.toString(sMod);
System.out.println("The Check Digit is: " + sModStr + "\n");
String combine = baseStr + sModStr;
int nextOp = Integer.parseInt(combine);
finalNumber = nextOp * nextOp;
String finalStr = Integer.toString(finalNumber);
System.out.println("The User Code is: " + finalStr + "\n");
}
}
Okay, as I said this code works, but it is long. I had considered a few alternatives, none of which will really work. The first was Enum, but that obviously is outside my parameters as it produces a 1-26 incremented pattern. There are several variations of this using various for{} loops but the result there is the same. I even considered a new HashMap and map.put statements, but that's only marginally shorter and it would seem to me that creating a hashmap would, in the end, actually use more memory than my current solution. As you can see, I did not .split or use .toCharArray() since these created separate entities in memory that I did not need. All the information I needed was already contained in the string itself, except for my chosen numerical values. Finally, I could have created another class file with this assignment code, but the result is the same: the length itself hasn't been changed it's been spread over two files (and actually increased in calling the method).
Having put this out here, can anyone see any way I can shorten this code, particularly in the area of the switch{} block and still retain the same result with the same numerical values? I would be highly interested in any suggestions that can be made in this regard. BTW I didn't mention this but this is not a school assignment, this is a personal project, though my level of Java knowledge is about at that of a beginner taking my first Java class LOL. Thanks
if you want to stick with this logic then use a HashMap<Character, Integer> and setup only once the values like: map.put('Z', 1); and so on, then u don't need the switch. You can get the values by using: map.get('Z'); which will return every time 1
it is easy to implement and easy to change the code if needed
Create an array of corresponding integers:
int[] letnums = {23, 17, 5, ... };
char c = s.charAt(i);
if (Character.isLetter(c)) {
int letnum = letnums(c);
...
}
public static String octalEquivalent(String binaryInput) {
String octalOutput =" ";
for(int counter=binaryInput.length(); counter<3; counter++)
{
binaryInput= "0"+binaryInput;
}
for(int counter=binaryInput.length(); counter%3==1; counter++)
{
binaryInput= "0"+binaryInput;
}
for (int counter1=0, counter2=3; counter2<=binaryInput.length(); counter1+= 3, counter2+=3)
{
String temp=binaryInput.substring(counter1,counter2);
switch (temp){
case "000": octalOutput = octalOutput+"0";
break;
case "001": octalOutput = octalOutput+"1";
break;
case "010": octalOutput = octalOutput+"2";
break;
case "011": octalOutput = octalOutput+"3";
break;
case "100": octalOutput = octalOutput+"4";
break;
case "101": octalOutput = octalOutput+"5";
break;
case "110": octalOutput = octalOutput+"6";
break;
case "111": octalOutput = octalOutput+"7";
break;
}
}
return(octalOutput);
}
in the first two for loops I added a space after the 0 which made it not match up to the switch statement's cases. example binaryInput= "0 "+binaryInput;
instead of binaryInput= "0"+binaryInput;
it still doesn't return the right answer but at least it is returning something.
Unless there's something I'm missing, I'd recommend Integer.parseInt(String, int) to convert the binary input into a number, and then Integer.toOctalString(int) to convert that to an octal String. Something like
public static String octalEquivalent(String binaryInput) {
return Integer.toOctalString(Integer.parseInt(binaryInput, 2));
}
Is counter2 ever greater than binaryInput.Length? If no, then there is your answer. That for loop is never getting hit, so your string is never changing.
Also, it looks like all you really want to do is substring the binaryInput between 0 and 3, which wouldn't require a for-loop at all. Try something like this, perhaps:
String temp="";
temp=binaryInput.substring(0,3);
switch (temp){
case "000": octalOutput = octalOutput+"0";
break;
case "001": octalOutput = octalOutput+"1";
break;
case "010": octalOutput = octalOutput+"2";
break;
case "011": octalOutput = octalOutput+"3";
break;
case "100": octalOutput = octalOutput+"4";
break;
case "101": octalOutput = octalOutput+"5";
break;
case "110": octalOutput = octalOutput+"6";
break;
case "111": octalOutput = octalOutput+"7";
break;
}
public static String octalEquivalent(String binaryInput) {
String octalOutput =" ";
for(int counter=binaryInput.length(); counter<3; counter++)
{
binaryInput= "0"+binaryInput;
}
while(binaryInput.length()%3 != 0)
{
binaryInput= "0"+binaryInput;
}
for (int counter1=0, counter2=3; counter2<=binaryInput.length(); counter1+= 3, counter2+=3)
{
String temp=binaryInput.substring(counter1,counter2);
switch (temp){
case "000": octalOutput = octalOutput+"0";
break;
case "001": octalOutput = octalOutput+"1";
break;
case "010": octalOutput = octalOutput+"2";
break;
case "011": octalOutput = octalOutput+"3";
break;
case "100": octalOutput = octalOutput+"4";
break;
case "101": octalOutput = octalOutput+"5";
break;
case "110": octalOutput = octalOutput+"6";
break;
case "111": octalOutput = octalOutput+"7";
break;
}
}
return(octalOutput);
}
I did it!
I'm working with an application right now for our project in school. My application is about Resistor Color Code calculation. My codes are working, but in displaying the values, I used the value as string. My problem is I want to make my result value as 1.2K ohms, 1.5M ohm or 5.4M ohms, just like that. Because in my codes the result will display 1200 ohms, 1500K ohms or 5400K ohms. Help me Please. Thanks in advance for the help.
This is my code for a, b, c, d and value is the display in EditText.
calcu is a button.
calcu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//for first band
if (a=="Black")
a = " ";
if (a=="Brown")
a = "1";
if (a=="Red")
a = "2";
if (a=="Orange")
a = "3";
if (a=="Yellow")
a = "4";
if (a=="Green")
a = "5";
if (a=="Blue")
a = "6";
if (a=="Violet")
a = "7";
if (a=="Gray")
a = "8";
if (a=="White")
a = "9";
//for second band
if (b=="Black")
b = "0";
if (b=="Brown")
b = "1";
if (b=="Red")
b = "2";
if (b=="Orange")
b = "3";
if (b=="Yellow")
b = "4";
if (b=="Green")
b = "5";
if (b=="Blue")
b = "6";
if (b=="Violet")
b = "7";
if (b=="Gray")
b = "8";
if (b=="White")
b = "9";
//for multiplier
if (c=="Black")
c = " ";
if (c=="Brown")
c = "0";
if (c=="Red")
c = "00";
if (c=="Orange")
c = "000";
if (c=="Yellow")
c = "0000";
if (c=="Green")
c = "00000";
if (c=="Blue")
c = "000000";
if (c=="Violet")
c = "0000000";
if (c=="Gray")
c = "00000000";
if (c=="White")
c = "000000000";
//for Tolerance
if (d=="Brown")
d = "1";
if (d=="Red")
d = "2";
if (d=="Green")
d = "0.5";
if (d=="Blue")
d = "0.25";
if (d=="Violet")
d = "0.1";
if (d=="Gray")
d = "0.05";
if (d=="Gold")
d = "5";
if (d=="Silver")
d = "10";
Value.setText(a + b + c + "\u2126" + " " + "\u00B1" + d + "%" + " Tolerance");
int result = getTheResult();
String Result = "";
if(result > 0 && result < 1000) Result = "" + result + " Ohms";
else if(result >= 1000 && result < 1000000) Result = "" + (result / 1000) + "K Ohms";
else if (result >= 1000000) Result = "" + (result / 1000000) + "M Ohms";
else Result = "Invalid Value";
import javax.swing.JOptionPane;
public class Resistance {
String digit_band1_color;
String digit_band2_color;
String multiplier_band3_color;
String tolerance_band4_color;
int temp1,temp2,temp3;
double temp4;
double result;
public Resistance(String a,String b,String c,String d){
digit_band1_color=a;
digit_band2_color=b;
multiplier_band3_color=c;
tolerance_band4_color=d;
switch (digit_band1_color){
case "Black":
temp1=0;
break;
case "Brown":
temp1=1;
break;
case "Red":
temp1=2;
break;
case "Orange":
temp1=3;
break;
case "Yellow":
temp1=4;
break;
case "Green":
temp1=5;
break;
case "Blue":
temp1=6;
break;
case "Voilet":
temp1=7;
break;
case "Grey":
temp1=8;
break;
case "White":
temp1=9;
break;
}
switch (digit_band2_color){
case "Black":
temp2=0;
break;
case "Brown":
temp2=1;
break;
case "Red":
temp2=2;
break;
case "Orange":
temp2=3;
break;
case "Yellow":
temp2=4;
break;
case "Green":
temp2=5;
break;
case "Blue":
temp2=6;
break;
case "Voilet":
temp2=7;
break;
case "Grey":
temp2=8;
break;
case "White":
temp2=9;
break;
}
switch (multiplier_band3_color){
case "Black":
temp3=0;
break;
case "Brown":
temp3=1;
break;
case "Red":
temp3=2;
break;
case "Orange":
temp3=3;
break;
case "Yellow":
temp3=4;
break;
case "Green":
temp3=5;
break;
case "Blue":
temp3=6;
break;
case "Voilet":
temp3=7;
break;
case "Grey":
temp3=8;
break;
case "White":
temp3=9;
break;
}
switch (tolerance_band4_color){
case "Brown":
temp4=1;
break;
case "Red":
temp3=2;
break;
case "Orange":
temp4=0.05;
break;
case "Yellow":
temp4=0.02;
break;
case "Green":
temp4=0.5;
break;
case "Blue":
temp4=0.25;
break;
case "Voilet":
temp4=0.1;
break;
case "Grey":
temp4=0.01;
break;
case "Gold":
temp4=5;
break;
case "Silver":
temp4=10;
break;
}
result=Math.pow(10,temp3);
System.out.println("Resistance = "+temp1+temp2+result+"+-"+temp4+"%");
}
public static void main(String[] args) {
String a=JOptionPane.showInputDialog(null,"Please Enter Color in Proper Format like this Black,Brown etc");
String b=JOptionPane.showInputDialog(null,"Please Enter Color in Proper Format like this Black,Brown etc");
String c=JOptionPane.showInputDialog(null,"Please Enter Color in Proper Format like this Black,Brown etc");
String d=JOptionPane.showInputDialog(null,"Please Enter Color in Proper Format like this Black,Brown etc");
Resistance calculator=new Resistance(a,b,c,d);
}
}