Write a method that prints characters using the following: public static void printChars(char ch1, char ch2, int numberPerLine). This method prints the characters between ch1 and ch2 with specific numbers per line. Characters are separated by exactly one space.Test your method with the following main method:
public static void main(String[] args)
{
printChars(‘A’,’z’,10);
printChars(‘0’,’9’,5);
}
Write a program (TwoIntegers.java) that prompts the user to enter two positive integers and prints their sum (by addition), product (by multiplication), difference (by subtraction), quotient (by division), and remainder (by modulation). When the user enters 5 and 3, the output from your program should look exactly like the following:
Enter two positive integers: 5 3 The sum is 8. The product is 15. The difference is 2. The quotient is 1. The remainder is 2.
import java.util.Scanner; public class TwoIntegers { public static void main(String[] args) { System.out.println("Enter two positive integers: "); Scanner userInput = new Scanner("System.in"); int integer1 = userInput.nextInt(); int integer2 = userInput.nextInt();
I'd like to know how to return a new array, I wrote in a method below the main method. I want to print the array but system.out.print doesn't work for arrays apparently. What structure i should use?
The class Overloading below asks for two names and prints three different greetings. Your task is to write the class methods missing from the class declaration. Methods print the greetings as shown in the example print.
Hint:The names and parameter types of the needed methods can be checked from the main method because all methods are called there. This exercise also does not require you to copy the source code below to the return field.
The method declarations will suffice.
Example output Type in the first name: John Type in the second name: Doe
********** Hi! ********** Hi, John ********** Hi, John and Doe **********
import java.util.Scanner; public class Overloading { public static void main(String[] args) { String firstName, secondName;
I am trying to create a method that prints the square root of a number up to a certain number. It needs to take a single int parameter for example "n" , and then print all of the (positive) even perfect squares less than n, each on a separate line. I want the method to be called something like this:
public void Squares(int n) { }
I need the output to look something like this:
Example: if n = 40, your code should print
4 16 36
So I have been working for a few hours now and am really stuck.
This is what I have so far:
int count = 0; int n = 4; int max = n; while(count < max) { System.out.println(n); n = n * n; count++;
I have an array with the following characters {'E', 'L','E','P','H','A','N','T','P','O'}
now, I need an array that will store the first array such that only the occurence occurs e.g {'E','L','P','H','A','N','T','O'} Notice that the characters 'E' and 'P' occur twice and as a result were not repeated the second time in the new array.
How would one go about this using the counting elements technique?
I tried this but not sure how to use the counting elements technique.
char [] arr = new char{'E', 'L','E','P','H','A','N','T','P','O'}; char[] bucket = new char[(arr[0] * arr.length)]; for (int i = 0; i < len; i++) bucket[arr[i]]++;
I am currently trying to make a calculator in Java. I want to use the String split method to tokenize the string of characters inputted. I thought I was using the String split method wrongly, because I had surrounded the characters I wanted to delimit with square brackets. However, when I removed the square brackets, the code threw an exception when I pressed the equal button. The exception was a PatternSyntaxException exception. Am I using the String split method wrongly? And why is the exception thrown? Here is my code:
import javax.swing.*;//import the packages needed for gui import java.awt.*; import java.awt.event.*; import java.util.Arrays; import java.util.List; import static java.lang.Math.*; public class CalculatorCopy { public static void main(String[] args) {
I have a question related to trees in Java. The following code is given:
public class BinarySearchTree { private Node root; public BinarySearchTree() { this.root = null; } public BinarySearchTree(Node root) {
[Code] ....
The inorder-method is the one that really imports. I have to go through a tree using the Inorder-method. I do understand how this works, however I don't understand how the code fits to the inorder process. I understand the code up to the point where it reaches the most left "root", but then I do not know how to go further. How do I reach the root above then?
So here is a method i wrote for the first part for initializing an array:The question was, "write a method which initializes a 2D target array (assuming all other classes have been imported and given), and all targets should be stationary."and this was my method i wrote:
public void initializeTargetArray(Target[][] targets) { int row = targets.length(); int col = targets[0].length;
for(int i = 0; i < row; i++) for (int j = 0; j < col; j++) targets[i][j] = new Target(Target.STATIONARY, false); }
Now my question how do I initialize an array similar to the method above for a different question?
Quote Part b Write a method which initializes an array (You can assume that the 2D array itself (but not the Target objects inside) is already initialized), where the game level stage is now set: Game level 1 - all targets are stationary, Game level 2 - For even numbered rows, targets at even numbered columns are stationary, targets at odd numbered columns are movable. For odd numbered rows, targets at odd numbered columns are stationary, targets at even numbered columns are movable. Game Level 3 - All targets are movable.
public void initializeTargetArray(Target[][] targets, int gameLevel) { //*** Finish this method. ***//
I am a student taking a Java programming class. My assignment is to write a program that converts a roman numeral input as entered by a user and converting it to it's integer value (arabic number). These are the methods that I must have in it:
1. Write a method that takes input from the user and passes it to a conversion method. 2. Write a method that yields the numeric value of each of the letters (conversion method). 3. Write a method that outputs the number the user entered and the converted number. 4. Write a main method to test the 3 methods.
I have written the first method, at least I think. Here is what I did there:
public static String romanInput(String number) { Scanner numberInput = new Scanner (System.in); System.out.print("Enter a roman numeral: "); String userInput = numberInput.next(); return userInput; }
I returned the userInput and I think that is passing it to the conversion method? Now I am working on this conversion method and to be honest I don't know where to begin. I am told how to convert a string in my assignment by the professor. I am told:
- Look at the first two characters. If the first has a larger value than the second, then simply convert the first. - Call the conversion method again for the substring starting with the second character. -Add both values. If the first one has a smaller value than the second, compute the difference and add to it the conversion of the tail.
I am also told to use a single-dimensional array. But, I don't know what I am to use a single dimensional array for? So this is what I wrote so far for this method:
I have written a character array for the roman numerals, and then one for arabic numerals, then I set them equal to each other. I also declared an integer variable set to 0 because I think that is what I will be returning at the end of the method. Now I don't know where to start for the conversion algorithm here. I know this is what I have to do, but I don't know how to do it:
1. Add the numbers together if they are in decreasing value or are equal in value. For example: VI is read as 5 + 1 = 6 XVI is read as 10 + 5 + 1 = 16 XXXVIII is 10 + 10 + 10 + 5 + 1 + 1 + 1 = 38 2. Use subtraction if a number is less than the number that follows it. For example, I is less than V, so when I is in front of V, you subtract its value. 3. For example: IV is 5 1 = 4 IX is 10 1 = 9 XL is 50 10 = 40 MCM is 1,000 + (1,000 - 100) = 1,900
I can't use hashtables or enums because I haven't learned about that yet. I have a feeling I need to use a for loop. I know I haven't done any of the actual programming work but I don't know how to begin writing this conversion method.
I must write a method that accepts a string and returns an int. The method should sum the unicode values of each character, so the string "ABC" should return 198. Also the only string class methods I'm aloud to use are length, charAt, and substring. I just don't know how the get the Unicode value.
Write an instance method, contains, that has one explicit parameter of type Rectangle. The method should return true if every point of the rectangle determined by the explicit parameter is on or within the rectangle determined by the implicit parameter. It should return false otherwise.
How do i write a method in java that will add a <b> or <em> tag to a specific word regardless of case or punctuation for example for "run forest RUN!" adding bold to run would be
<b>run<b> & <b>RUN!<b> public void addTag(String word, String tag) {
I have understood my programming class up to this point and now I have been given a lab that I can't figure out for the life of me. Here what I have to do: Write a program that will call a method (called f) to calculate the following function" f(x)=(x^2)-16...this is what the output should be:
I am supposed to write a static method that simulates a flip of a weighted coin by returning either heads or tails each time it is called. The coin is twice as likely to turn up heads as tails. My idea was to of course use a random class
Random flip = new random
and somehow initialize it so 3 numbers are called and create if statements so that when 0 and 1 are called heads returns and when 3 is called tails is.How do I put all this together?
So I am calling this method several times and trying to write multiple records to a file. Problem is that every time I call the method it overwrites the file from before and doesn't add it.
public void fileWriterMethod() throws IOException{ RandomAccessFile raf = new RandomAccessFile(filename, "rw"); raf.writeInt(id); raf.writeInt(existingMileage); raf.writeInt(gasCost); raf.writeInt(ndays); raf.writeInt(rate); raf.writeInt(totalCharge); raf.writeInt(discount); raf.writeInt(tax); raf.writeInt(netCharge); raf.writeInt(returnMileage); raf.writeBytes(carName + " "); //Closing the stream raf.close(); }
i have to "Write a method called addToOverThirty which takes the array nums3 as a parameter. It adds 1 to all numbers that have a value greater than 30 in the array.
Add a call to the addToOverThirty method, then display a message telling what the output is followed by the results For example:The nums3 array after adding 1 to all numbers greater than 30 is10 6 15 and so on (check with the values you assigned to nums3)"its pointless because we were told not to make the array have a number over 30.
import java.lang.*; import java.util.*; public class LastProject public static void main(String[] args) { int nums1[] = new int[15]; int nums2[] = new int[15]; int nums3[] = {5,2,15,8,26,22,1,29,4,23,30,11,19,6,24};
The second message dialog result is always 0 ... i want to find the minimum grade...
import javax.swing.*; import java.util.Arrays; public class Parrarrapapa{ public static void main (String[]args){ String length = JOptionPane.showInputDialog("Number of students");
I'm tyring to print the same output in console to a text file, but I can only get the last line of the console output in the text file, not sure what is wrong with my code:
while (in.hasNextLine()) { PrintWriter writer = new PrintWriter("output5.txt"); tempS = in.nextLine().toLowerCase(); System.out.println(wp.bestPages(tempS));
[code]....
What's causing only the last time to be printed in text file? Are there better ways to print console outputs into a text file than PrintWriter?
When I run this code, it is supposed to get one value from turnTimer(); and return it, just as a test. This works when I enter a valid pit. For example. If I were to input "A" when it's player one's turn, it will return 1, like it should. However, if I were to type "H" when it's player one's turn, it returns "Not a valid pit!"(like it should) but then it also returns 12. It shouldn't know that H is 12 because it's in a separate method. I'm confused as to why it's printing both values.
import java.util.*; public class Mancala { static Scanner input = new Scanner(System.in); public static int pit; public static void main(String[]args) { Mancala mancala = new Mancala(); int[] board = {0,3,3,3,3,3,3,0,3,3,3,3,3,3};
I've a ArrayList with dogs on, and I've a function that should allow me to delete a dog from the register and if the dog was found it prints "The dog was deleted" and if the dog doesn't exist in the arraylist it should print "the dog couldn't be found".
Everything works perfect until I shall delete a dog that is not first on the list. Then the program shows first "dog was not found" and on the row after "the dog was deleted" if the dog was second on the list. If it was third I will get 2 messages that the dog wasn't found and then that the dog was deleted. I've no clue why it prints both else and if!
public static void taBortHund(){ //Har tar vi bort hund fran listan //System.out.println(hundlista); System.out.print("Vilken hund vill du ta bort? "); String hunden = tangentbord.nextLine(); for (int taBort = 0; taBort<hundlista.size(); taBort++){