And And Or Operator - Manipulate Bits In A Variable
Jun 1, 2014
I am trying to understand what ivor is saying about the and, and or operators and the mask. If I understand it correctly the & operator prevents you from changing a bit that is one when a mask is involved and changes all others to 0 and the | operator forces a bit to 1 when the mask is 1.
My question is when would i need to actually use the & ,| operators ?when will i need to manipulate the bits in a variable?
View Replies
ADVERTISEMENT
Aug 18, 2014
At first I wanted to just use an array and set each day a value, however I was told that it has to be stored as a string.
Design and implement the class Day that implements the day of the week in a program. The class Day should store the day, such as Sun for Sunday. The program should be able to perform the following operations on an object of type Day:
A. Set the day.
B. Print the day.
C. Return the day.
D. Return the next day.
E. Return the previous day.
F. Calculate and return the day by adding certain days to the current day. For example, if the current day is Monday and we add four days, the day to be returned is Friday. Similarly, if today is Tuesday and we add 13 days, the day to be returned is Monday.
G. Add the appropriate constructors.
H. Write the definitions of the methods to implement the operations for the class Day, as defined in A through G.
I. Write a program to test various operations on the class Day.
import java.util.*;
public class Day {
static Scanner readinput = new Scanner(System.in);
String day;
public Day(String day) {
[Code] ....
So right now if I run my code it allows me to type in a day, then it gives me the next and previous day, the last part is to add X days to it. Ideally I would like to be able to take the day entered, depending on the day set a numeric value, then add prompt for number of days you want to add. The program should then use modal (%7 ) to add value to the value that was given based on the day, and then translate it back into a String value (the day).
View Replies
View Related
May 4, 2014
There are a lot of variables in physics and it is easy to manipulate equations to find the unknown variable. Well, in my program I have the problem of determining which equation to solve for an unknown. You can see I have a timeF, timeI, time, distanceI, distanceF, distance...etc...variables. The way that I have written the code, it will scan for an empty() JTF and assign it as the unknown variable (not sure I can do it this way anymore). At that point, the value is passed into the correct calculation method to process the equation based on a series of case statements in each method...
I know the button arrays I have set up will process correctly in the checkJTF method...and I will need to account for that...but right now my focus is on getting the program to properly calculate the kinematics equations and display the result in the JPanel.
public class PhysicsFrame extends JFrame {
//Kinematic variables
private static float distanceI, distanceF, velocityI, velocityF,
velocityAverage, acceleration, time, timeF, timeI, result;
//Power variables
[Code] .....
View Replies
View Related
Feb 5, 2015
I just wanted to ask that while evaluating any expression,is the operator precedence fixed or it varies according to the compiler ,?
View Replies
View Related
Jun 20, 2014
I want to practice chopping my code into small objects so they can be easily reused and replaced/maintained etc. So I'm looking to separate this small program into three separate files, one main with the main function to make a window, one to make the DrawPanel for "animating", and one for the controlling with the KeyListener.
import javax.swing.*; //Graphics API
import java.awt.*; //BorderLayout API
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class KeyAnimate {
int x = 0; //X Coords
int y = 0; //Y Coords
int z = 0; //Z to Keep going forever
[code]....
View Replies
View Related
May 24, 2014
I am using something a kin to this:
for (int w=0;w<bi.getWidth();w++) {
for (int h=0;h<bi.getHeight();h++) {
int color = bi.getRGB(w, h);
color = color << 5;
bi.setRGB(w, h, color);
}
}
I am using a solid green image so
11111111000000001111111100000000
I just wanted to see how it looked so bumped it over 5 places (obviously changing the color). To my surprise, there was no added transparency. I mean moving it to the left would make the alpha:
11100000
I am thinking that setRGB() doesn't effect alpha. Is this accurate?Tested it and in fact setRGB has no effect on the alpha bits. So now the question is how can I gain access to them. I am going to look into the writable Raster API. Perhaps, I can also use a modified awt to access directly OS data.
View Replies
View Related
Mar 14, 2014
i need to show that there are 16 bits for a char variable.
i tried converting it to an int and then apply the bitCount method but that gave an output of 3.
what method should i use?
View Replies
View Related
Feb 23, 2014
I have:
Java Code: String s = "111100100111011011000010110011101"; mh_sh_highlight_all('java');
I am trying to convert that to bits/bytes.
I need to make that into a series of bits with the same exact values..."1" = a 1 bit, and "0" = a 0 bit.
Remarkably, I haven't been able to find much on this sort of conversion - possibly I am just not searching with the right keywords? As typically, stackexchange or other parts of the web have the same question that I have, asked (and answered) by many others.
How do I go about doing this?
Furthermore, how would I go about saving this to a file - and are there already good "kinds" of files to save this into. If not, how do I go about making my "own" type of "file."
CONTEXT:
I've written a compression system. To keep things simple, I've been using a string to hold the 1s and 0s, so that debugging is simpler, and overall, everything is easier to write. Now, however, my algorithm is finished - and I'm moving on to create a GUI and a working system. This is the last step that I need for the non-GUI stuff (which I'm writing through javafx by the way - is this the right thing to use? I've been told that that is where people are moving towards. Away from swing).
View Replies
View Related
Feb 7, 2006
I have to divide a text file into blocks of 128 bits. I think i must use the ByteArrayInputStream and ByteArrayOutputStream classes. is there any website showing how to user these two ByteArrayInputStream and ByteArrayOutputStream classes in detail. or it would be much better if you could show me a portion of the code.
View Replies
View Related
Feb 24, 2014
I am designing a program in-order convert Binary to Decimal values with added features:
Rejecting binary values longer than 32 bits
Prompting the user to make multiple entries after completing the binary to decimal conversion of their first entry. I was trying to code this in Nested For Loops, but I don't know if I've really done that.
Here is what i have so far.
public class BinaryToDecimal {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
String binary;
int decimal=0b10, i, rem;
boolean isBinary = true;
[Code] ....
View Replies
View Related
Sep 24, 2014
I build a select tag string and use JSTL to display this string on the JSP page.
Is there an easy way to manipulate this string to remove one of the option pairs?
View Replies
View Related
Jul 15, 2014
I was recently looking at the Tesseract module to convert handwritten text into typed text, however I was wondering is there anything out there, or any code which would allow me to do it the other way? I would like to load up a piece of typed up text (pdf, word, even notepad), and be able to turn it into an object for manipulation.
In the long run, I'd like to be able to open up a pdf document, click on some text, edit it, then use tesseract to turn it back into typed text.
View Replies
View Related
Dec 4, 2014
I have some data files that take this form:
D14
J3
N1
a26
[code]...
I also have this code that takes the file, goes through each line using a loop and counts the total of all the integers on each line, it also then loops and takes each integer on each line and divides it by the total calculated in the previous loop but I don't know what code to manipulate the strings to do these calculations. After these calculations I want to write the updated values to the text file I originally took the data in from. So as an example the total I already for this file to be '232' so the first line would be D 0.06 because 14/232 = 0.06034482758 and rounded to 2 decimal points is 0.06.
import java.io.File;
import java.io.FileReader;
import java.io.LineNumberReader;
import java.io.PrintWriter;
import java.util.Scanner;
public class Normalise {
private static Scanner scanner;
//private static PrintWriter out;
[code]....
View Replies
View Related
Nov 17, 2014
So In my program I have a String called original and I am supposed to prompt the user for a sentence and then Make a StringBuilder instance called “originalReversed” based off of original. Then I have to do is to "Manipulate originalReversed so that it contains the value of original in reverse. This is done with a single statement."
import java.util.Scanner;
public class StringBuilderPractice {
public static void main(String [] args)
{
Scanner input = new Scanner(System.in);
String original;
System.out.println("Please enter a sentence: ");
original = input.nextLine();
}
how to create the instance but not the second part where it says manipulate the value for the first part I think it might be
StringBuilder originalReversed = new StringBuilder(original);
View Replies
View Related
Oct 30, 2014
I have a little problem in my Java FX application. in a tableview, I add records whatever... And i have a button that delete the selected record in the tableview, This button delete too the related record in a database.
When I select a row in this table , I store the ID of that record in a global variable to have it available . Thus, when I click on the delete button, delete the record from the database based on the ID that I stored in the global variable.
The problem is that tableview has a curious property. If I select the last row and I click on the delete button ( removing the last record of the tableview ) , the next higher row is automatically selected ; and causes the ID value stored change .
The row is deleted in the tableview is correct but apparently , when it comes to the method deletes the record from the database, the global variable ID is already updated and delete another record.
I would like to disable that property of the table so that the row can be selected only by the mouse ... or some other solution .
...
@Override
public void initialize(URL url, ResourceBundle rb){
table.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
table.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Object>(){
[Code] .....
View Replies
View Related
Nov 17, 2014
I need to read a csv file from the disk and manipulate the data inside. My problem is that if the csv (excel format) contains "" in any of it's values the whole string line gets saved/displayed incorrectly . For example for the line in the csv file :
1,393275,57319 57321 57323 57325 57327,5_5,200000,393277,57329 57331 57333 57335 57337,3_3,200000,400000,"gi,gi",,,,,,BASS/2.4,,1
it gets read in java as :
"1,393275,57319 57321 57323 57325 57327,5_5,200000,393277,57329 57331 57333 57335 57337,3_3,200000,400000,""gi,gi"",,,,,,BASS/2.4,,1";;;;
instead as :
1,393275,57319 57321 57323 57325 57327,5_5,200000,393277,57329 57331 57333 57335 57337,3_3,200000,400000,"gi,gi",,,,,,BASS/2.4,,1
My code is :
String csvFile = "resources/testcsxs.csv" ;
String l = "";
try {
BufferedReader cz = new BufferedReader(new FileReader(csvFile));
while ((l = cz.readLine()) != null ){
System.out.println(l);
}
What am i doing wrong , how can i read a csv file (excel saved) without getting "" added incorrectly ?
View Replies
View Related
Feb 19, 2014
Write an interface Directory to manipulate entries in a telephone directory for the University. The interface must support the following operations:
1) The ability to insert new entries into the directory. Entries should be stored in alphabetical order of surname.
2) Delete entries from the directory either by name or number
3) Provide a lookup method that will find the extension no of a member of staff given his/her name. You should try to make this method run as efficiently as possible.
4) Change a person's telephone number
5) Print the telephone directory in a neatly tabulated fashion.
Write a class ArrayDirectory that implements this interface using an array to store the telephone directory information. The class must store the surname and initial for each member of staff and their telephone extension (a four digit number which may start with a zero). You may find it useful to define a class Entry to store information about individual entries. The entries should be read into the array from a text file consisting of multiple lines in the following format:
Surname<tab>Initials<tab>Telephone extension
Write a main program that reads in data from a file and then tests all the methods of your class interface. Note it is not necessary to write data back to disk (even if it has changed) in this project.
For data insertion and lookup, you should measure the performance for the average case (e.g. looking up a record in the middle of the data). To do this you can use the static method System.currentTimeMillis() which returns the time in milliseconds. To get an accurate measure of the time to perform an operation it is a good idea to perform each operation 1000 (or even 10000) times and measure the time taken. This will remove any problems due to the system clock having a coarse granularity (not ticking very often). Make sure you only time the method and not any input/output associated with it. Your documentation should include a discussion of these results.
Part 2
You decide that your solution to part 1 may be too slow to be useful when used with a real large telephone directory and make the following changes to attempt to improve the performance of your program:
1). Provide a second implementation (ListDirectory) of the Directory interface using the Java Collections List interface and LinkedList classes.
2). The changes in 1) should make adding, deleting and modifying records more efficientbut will probably reduce the time to lookup numbers. To overcome this you use a technique called hashing. Instead of storing all the records on one list you use a series of lists. The data for all people whose surname begins with "A" is stored on the first list, records for all people whose surname begins with "B" on a second list and so on. Write a third implementation (HashDirectory) of the Directory interface using this technique.
Again, you should measure the performance for the best, worst and average cases of implementations 1) and 2) above. Compare the efficiency of each of the 3 implementations in your documentation.
Part 3
Choose one of your 3 implementations from Parts 1 and 2 and embed it in a graphical user interface that allows users to perform all the methods in the Directory interface using a mouse and keyboard appropriately.
View Replies
View Related
Jan 8, 2014
I just cant seem to understand the order of precedence here.
class Test{
public static void main(String[] args){
int k = 1;
k += (k = 4) * (k + 2);
System.out.println( k );
}
}
From what I have read compound operators have the lowest order of precedence... But the above piece of code makes the assignment k = 1 + (k = 4) * (k + 2) before evaluating the rest of the statement first.
It then evaluates (k = 4) and proceeds with the remained of the statement 1 + 4 * (4 + 6)....
I dont understand why the first k is assigned 1 but the remaining ks 4. Should they not all be 1 or 4 (I would have thought 4, since += has the lost order of precedence so is evaluated last)??
View Replies
View Related
Jun 4, 2014
I know instanceof is an operator and println is a method. But what is the difference between the two? How are they different/same?
View Replies
View Related
May 23, 2015
After the executing the code below:-
class Test
{
public static void main(String [] args)
{
String s = new String("new") ;
String d = new String("new") ;
System.out.println((s==d)+ " "+s.equals(d)) ;
System.out.println(s==d + " "+ s.equals(d)) ;
}
}
OUTPUT:- false true
false
Why did the output change in the second print statement?
View Replies
View Related
Oct 6, 2014
I have an Array of objects that contains strings. I am new to Java and have read books, the Java docs, and searched the Internet for my problem to no avail. How can I get my strings to print in a list so that the user can select an object to manipulate its attributes? I have a class called Instruments and created 10 guitar objects. Here is the code:
Instrument [] guitar = new Instrument[10];
for (int i = 0; i < 10; i++) {
guitar[0] = new Instrument("Guitar 1");
guitar[1] = new Instrument("Guitar 2");
guitar[2] = new Instrument("Guitar 3");
guitar[3] = new Instrument("Guitar 4");
[Code] ....
View Replies
View Related
Feb 24, 2015
I have a string of variable tags (tag) filled with variables, and using a Scanner (scan) to manipulate and generate data dictionary add cards for each. The variables are delimited with new line character, and the variables are structured such that the actual name of the variable contains info as to its data type, size, etc. What I need to do is move the scanner to a particular point into the string then use .nextInt() or similar. I don't want to do this to the whole variable, just where I start it from. How can I do this?
E.g..
/* variables in String tag...
ANS_s20Tx12VDstr
ANS_:Tx67MSvis
ANS_zRx11MSctr
...
..
*/
Scanner scan = new Scanner( tag );
int ptr = tag.indexOf( "_" );
ptr++;
if ( Character.toLowerCase( tag.charAt( ptr ) ) == 's' )
// then start at the "_" character and grab the nextInt()
View Replies
View Related
Oct 18, 2014
why operator overloading is not supported in Java.
View Replies
View Related
Mar 23, 2014
I am new to Java and I am trying to use values that are set for cases in the switch operator.
The first menu ask for you to pick a product and each product has a price on it.
The second menu ask you to pick a state with each state having a decimal value on it.
The third is asking you to put the number of cases and each case is 12 items.
A key note to remember is that each part that a person is choosing is on a different instance!
Here is an example of what i am trying to do.
Menu 1: I picked case 1 that is Computer and it is worth 1000
Menu 2: I picked case 1 that is CT and it's tax is 7.5
Third choice: I picked case 1 and that has 12 items
I want the subtotal witch is: (1000 * 12)
Subtotal in this situation is: 120000
Next i need the total value which is based on what state they picked for the tax percent value picked from the state menu case: (12000 * 0.075 + 120000)
Total value is: 129000
I will post the code I have but based on the choices a person makes will determine the values and I need those values set in the cases to put in a math equation. The problem I am having is retrieving these numbers form the cases inside the menu options and they are on a different instance. So How can I do this in Java code?
Here is the code:
This is menu 1
Java Code:
import java.util.*; //scanners and more
class menu{
public void display_menu() {
System.out.println ("Please select your product"); //Gives user direction
System.out.println ( "1)
[Code] .....
View Replies
View Related
Jul 6, 2014
It looks like an upside down v , i cant find it on my keyboard. I assume its a keyboard combo.
View Replies
View Related
Feb 17, 2015
This program is supposed to ask for the operator (+ or -) then ask you for two numbers and do the math. When I run it it comes up- Enter operator. When I say add or sub nothing happens. Here it is:
import java.util.Scanner;
public class Echo1{
public static void main(String args[]){
Scanner userInput = new Scanner(System.in);
System.out.println("Enter Operator");
String operator = userInput.next();
[Code] ....
View Replies
View Related