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);
}
}
Related
I have this program which can make some singular nouns plural (I know I'm missing a lot, but that's aside the point). When I enter in a word such as "man-of-war" it returns as "mans-of-war" instead of "men-of-war". How do I fix this? My code can already make man to men, just not in the case I mentioned. Also for this program every compound word will have the dash, it is simply for practice.
public class LanguageUtils {
static boolean checkException(String noun){
String[] exceptions = {"fish", "fox", "deer", "moose", "sheep", "cattle","pants","scissors"};
for(int i=0;i<exceptions.length;i++) {
if(exceptions[i].equals(noun))
return true;
}
return false;
}
static boolean EnglishConsonant(char ch) {
switch (Character.toLowerCase(ch)) {
case 'a': case 'e': case 'i': case 'o': case 'u':
return false;
default:
return true;
}
}
static String makePlural (String noun){
String pluralWord = "";
int length = noun.length();
String strippedWord = noun.substring(0, noun.length()-1);
char lastLetter = noun.charAt(noun.length()-1);
if(noun.contains("-")){
String nounsaver = noun.substring(noun.indexOf('-'), noun.length());
pluralWord = noun.substring(0,noun.indexOf('-')) + "s" + nounsaver;
}
else{
switch (lastLetter){
case 's':
case 'x':
case 'z':
if(noun.equals("ox")){
pluralWord = noun + "en";
break;
}
else{
pluralWord = noun + "es";
break;
}
case 'o':
if(EnglishConsonant(noun.charAt(noun.length()-2))){
pluralWord = strippedWord + "oes";
break;
}
case 'e':
char f = noun.charAt(noun.length()-2);
String prec = noun.substring(0, noun.length()-2);
if(f == 'f'){
pluralWord = prec + "ves";
break;
}
if(noun.equals("goose")){
pluralWord = "geese";
break;
}
else{
pluralWord = noun + "s";
break;
}
case 'h':
if ((noun.charAt(noun.length()-2)== 'c') || (noun.charAt(noun.length()-2)== 's')) {
pluralWord = noun + "es";
break;
}
case 'f':
if (EnglishConsonant(noun.charAt(noun.length()-2))) {
pluralWord = strippedWord + "ves";
break;
}
case 'y':
if (EnglishConsonant(noun.charAt(noun.length()-2))) {
pluralWord = strippedWord + "ies";
break;
}
default:
if(noun.equals("foot")){
pluralWord = "feet";
break;
}
if(noun.endsWith("man")){
pluralWord = noun.substring(0, noun.length()-3)+"men";
break;
}
else{
pluralWord = noun + "s";
break;
}
}
}
if (length == 1){
pluralWord = noun + "'s";
}
if(checkException(noun)){
pluralWord = noun;
}
return pluralWord;
}
}
When dealing with a compound word, if you need to pluralize a part, call a function designed to do just that: makePlural.
If you have - then break the words and call the makePlural for those words again.
You can use String#replace:
if (noun.contains("man")) {
pluralWord = noun.replace("man", "men");
break;
}
I've created a random word generator and it seems to work pretty well except for the fact that before the desired output, in the same line, it prints "null".
Here's my code:
import java.util.Random;
public class wordGenerator {
private static String r,s;
public static void randChar(int x) {
Random rand = new Random();
x = rand.nextInt((26 - 1) + 1) + 1;
switch(x) {
case 1: r = "a"; break;
case 2: r = "b"; break;
case 3: r = "c"; break;
case 4: r = "d"; break;
case 5: r = "e"; break;
case 6: r = "f"; break;
case 7: r = "g"; break;
case 8: r = "h"; break;
case 9: r = "i"; break;
case 10: r = "j"; break;
case 11: r = "k"; break;
case 12: r = "l"; break;
case 13: r = "m"; break;
case 14: r = "n"; break;
case 15: r = "o"; break;
case 16: r = "p"; break;
case 17: r = "q"; break;
case 18: r = "r"; break;
case 19: r = "s"; break;
case 20: r = "t"; break;
case 21: r = "u"; break;
case 22: r = "v"; break;
case 23: r = "w"; break;
case 24: r = "x"; break;
case 25: r = "y"; break;
case 26: r = "z"; break;
default: r = "|null|";
}
if(x != 1 && x != 5 && x != 9 && x != 15 && x != 21) {
int h = rand.nextInt(4 - 0);
if(h == 2) {
int k = rand.nextInt(6 - 0);
switch(k) {
case 1: r = "a"; break;
case 2: r = "e"; break;
case 3: r = "i"; break;
case 4: r = "o"; break;
case 5: r = "u"; break;
}
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Random rand = new Random();
int y = rand.nextInt((10 - 4) + 1) + 4;
for(int z = 1; z < y; z++) {
randChar(0);
s = s + r;
}
System.out.println(s);
}
To me it seems fine, but when I think it should output something like
opfruvme
it prints
nullopfruvme
What's the problem?
You didn't initialize s.
private static String r,s="";
When you start your application in the loop the statement s = s + r; takes null value before concatenating with r in the first cycle. Initializing variable with empty string will give you result that you have been expected.
You didn't initialize s, so then the first time the s = s + r; line executes the current value of s (null) will be coerced to a String, so you will assign "null" + r to s.
My instructor requires us to take the package from our code and make it a default package. The only problem is he taught us how to do that through Windows and I have a MacBook so his way isn't working. I can't figure out how to do it. I've attached the code to the bottom in case that will help.
package romannumeralcalculator;
import java.util.*;
public class RomanNumeralCalculator
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int integer;
do {
System.out.print("Please enter an interger from 1 to 5999. Enter a negative number to exit. \n ->");
integer = input.nextInt();
} while (integer >= 6000);
while (integer == 0) {
System.out.println("");
break;
}
String results = "";
int ones = integer % 10;
int tens = (integer / 10) % 10;
int hundreds = (integer / 100) % 10;
int thousands = (integer / 1000) % 1000;
switch (thousands) {
case 1:
results += "M";
break;
case 2:
results += "MM";
break;
case 3:
results += "MMM";
break;
case 4:
results += "MMMM";
break;
case 5:
results += "MMMMM";
break;
default:
System.out.println("");
}
switch (hundreds) {
case 1:
results += "C";
break;
case 2:
results += "CC";
break;
case 3:
results += "CCC";
break;
case 4:
results += "CD";
break;
case 5:
results += "D";
break;
case 6:
results += "DC";
break;
case 7:
results += "DCC";
break;
case 8:
results += "DCCC";
break;
case 9:
results += "CM";
break;
default:
System.out.println("");
}
switch (tens) {
case 1:
results += "X";
break;
case 2:
results += "XX";
break;
case 3:
results += "XXX";
break;
case 4:
results += "XL";
break;
case 5:
results += "L";
break;
case 6:
results += "LX";
break;
case 7:
results += "LXX";
break;
case 8:
results += "LXXX";
break;
case 9:
results += "XC";
break;
default:
System.out.println("");
}
switch (ones) {
case 1:
results += "I";
break;
case 2:
results += "II";
break;
case 3:
results += "III";
break;
case 4:
results += "IV";
break;
case 5:
results += "V";
break;
case 6:
results += "VI";
break;
case 7:
results += "VII";
break;
case 8:
results += "VIII";
break;
case 9:
results += "IX";
break;
default:
System.out.println("");
}
System.out.println(results);
}
}
In the projects tab of Netbeans in the top-left:
Expand the tree for your Project.
Expand the Source Packages folder.
Expand the package with your java file.
Drag the java file from under the package to the Source Packages folder.
A dialog box will pop up with the title Move Class. On that dialog click the Refactor button.
This should be the same procedure in Windows. I am not sure why your professor told you something different.
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 am trying to change this from an input box into a Scanner class, but i am having trouble doing so.
Its a program that takes words and makes them into a phone number here is the code that does so. Any help would be greatly appreciated and if there is something that i can do in return i would gladly do so.
// declare imports
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.util.*;
public class Telephone {
public static void main(String[] args) {
// ask for the phone number (in letters)
char letter;
String inputMessage = "Please enter the number in Letters " + "or enter '#' to stop the program ";
String inputString = JOptionPane.showInputDialog(inputMessage);
String outputString = "";
String outputMessage = "";
int digit = 0;
int x = 0;
for (int i = 0; i < inputString.length(); i++)
System.out.print(inputString.charAt(x)); {
while (inputString.charAt(x) != '#') {
letter = Character.toUpperCase(inputString.charAt(x));
x++;
// make sure its not a number
if (letter >= 'a' && letter <= 'z') if (letter >= 'A' && letter <= 'Z') {
digit++;
switch (letter) {
case 'A':
case 'B':
case 'C':
outputString += "2";
break;
case 'D':
case 'E':
case 'F':
outputString += "3";
break;
case 'G':
case 'H':
case 'I':
outputString += "4";
break;
case 'J':
case 'K':
case 'L':
outputString += "5";
break;
case 'M':
case 'N':
case 'O':
outputString += "6";
break;
case 'P':
case 'Q':
case 'R':
case 'S':
outputString += "7";
break;
case 'T':
case 'U':
case 'V':
outputString += "8";
break;
case 'W':
case 'X':
case 'Y':
case 'Z':
outputString += "9";
}
if (digit == 7) {
break;
}
if (digit == 3) {
outputString += "-";
}
}
inputMessage = "Enter another set of telephone letters";
}
JOptionPane.showMessageDialog(null, outputString, "Telephone Program", JOptionPane.PLAIN_MESSAGE);
}
Modify the line where you show the JOptionPane as follows,
//String inputString = JOptionPane.showInputDialog(inputMessage);
System.out.println(inputMessage);
Scanner sc = new Scanner(System.in);
String inputString = sc.nextLine();
so you could do the following,
package test;
import java.util.Scanner;
public class Telephone {
public static void main(String[] args) {
// ask for the phone number (in letters)
char letter;
String inputMessage = "Please enter the number in Letters " + "or enter '#' to stop the program ";
// String inputString = JOptionPane.showInputDialog(inputMessage);
System.out.println(inputMessage);
Scanner sc = new Scanner(System.in);
String inputString = sc.nextLine();
String outputString = "";
String outputMessage = "";
int digit = 0;
int x = 0;
for (int i = 0; i < inputString.length(); i++) {
System.out.print(inputString.charAt(x));
}
while (inputString != null && inputString.trim().length() > 0 && inputString.charAt(x) != '#') {
letter = Character.toUpperCase(inputString.charAt(x));
x++;
// make sure its not a number
// if (letter >= 'a' && letter <= 'z') {
if (x >= inputString.length()) {
x = 0;
System.out.println("\n" + outputString);
// JOptionPane.showMessageDialog(null, outputString, "Telephone Program", JOptionPane.PLAIN_MESSAGE);
// inputString = JOptionPane.showInputDialog(inputMessage);
System.out.println(inputMessage);
inputString = sc.nextLine();
} else if (letter >= 'A' && letter <= 'Z') {
digit++;
switch (letter) {
case 'A':
case 'B':
case 'C':
outputString += "2";
break;
case 'D':
case 'E':
case 'F':
outputString += "3";
break;
case 'G':
case 'H':
case 'I':
outputString += "4";
break;
case 'J':
case 'K':
case 'L':
outputString += "5";
break;
case 'M':
case 'N':
case 'O':
outputString += "6";
break;
case 'P':
case 'Q':
case 'R':
case 'S':
outputString += "7";
break;
case 'T':
case 'U':
case 'V':
outputString += "8";
break;
case 'W':
case 'X':
case 'Y':
case 'Z':
outputString += "9";
}
if (digit == 7) {
break;
}
if (digit == 3) {
outputString += "-";
}
}
// }
inputMessage = "Enter another set of telephone letters";
}
System.out.println("\n" + outputString);
// JOptionPane.showMessageDialog(null, outputString, "Telephone Program", JOptionPane.PLAIN_MESSAGE);
}
}