Write A Method That Prints Characters

May 7, 2014

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);
}

View Replies


ADVERTISEMENT

How To Write A Dot File That Prints Graph In Java

Dec 6, 2014

Let's say if I want to create a dot file that print this graph in Java, how do i go about it?

I'm starting with this:

PrintWriter writer = new PrintWriter("graph.dot");

View Replies View Related

Write A Program That Prompts User To Enter Two Positive Integers And Prints Their Sum

Jan 9, 2015

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();

[code]....

View Replies View Related

How To Return Array From A Method / Back Into Main Method That Prints Out Stuff

May 27, 2014

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?

View Replies View Related

Write Out Whole Sentence - Counting Characters

Sep 16, 2014

public class Setning

public static void main(String[] setning) {
skrivHeleSetningen(setning);
}
private static void skrivHeleSetningen(String[] setning) {
 
[Code] .....

What this do is:

First write out the whole sentence (WORKS)
Write out number of words (WORK)
Then I would like it to write out number of characters (DOESN'T WORK)

The arguments are given in "run configurations" using eclipse

View Replies View Related

Java Method Overloading Ask For Two Names And Prints Three Different Greetings

Feb 24, 2014

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;

[Code] ....

View Replies View Related

Creating Method That Prints The Square Root Of A Number Up To A Certain Number?

Feb 27, 2015

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++;

View Replies View Related

Using Count Element Method To Count Occurrence Of Characters In Array

Jun 30, 2014

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]]++;

View Replies View Related

String Split Method To Tokenize String Of Characters Inputted?

Sep 27, 2014

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) {

[Code] .....

View Replies View Related

Write A Method That Uses For-loop To Perform?

May 19, 2014

which when user enter a number then will display how many * in a line .Produce a line: ***** But my code only show 1 * there... what wrong with it ?

import java.util.Scanner;
public class display {
public static void main(String args[]) {
int i,n = 0 ;

[Code] ...

View Replies View Related

How To Write Inorder-method For Trees

Mar 29, 2015

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?

View Replies View Related

Write A Method Which Initializes A 2D Target Array

Apr 11, 2014

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. ***//

View Replies View Related

How To Write Conversion Method From Roman Numerals To Arabic

Nov 8, 2014

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:

public static int numberConversion(int number) {

int romanConv = 0;
char[] romanNum = {'I', 'V', 'X', 'L', 'C', 'D', 'M'};
char[] arabicNum = {1, 5, 10, 50, 100, 500, 1000};
romanNum = arabicNum;
}

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.

View Replies View Related

Write A Method That Returns Unicode Values Of A String?

Oct 25, 2014

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.

View Replies View Related

How To Write Instance Method For Rectangle Class Called Contains

May 29, 2014

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.

This is what i did so far?

public boolean contains(Rectangle other) {
Rectangle intersect = Rectangle.intersection(this, other);
if ((intersect.left == this.left) && (intersect.bottom == this.bottom) && (intersect.width == this.width)
&& (intersect.height == this.height)) {
return true;
} else {
return false;
}
}

View Replies View Related

Write A Method To Add Tag To Specific Word Regardless Of Case Or Punctuation

Mar 23, 2015

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) {

View Replies View Related

Write A Program That Will Call A Method To Calculate Function

Dec 3, 2014

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:

x f(x)
1 -15
2 -12
3 -7
4 0
5 9
6 20
7 33
8 48
9 65
10 84

I guess it also has to print this output in a table even though my professor never mentioned it. Usually

public class Lab12 {
public static int f(int x)
{
return x*x-16;

[code]....

View Replies View Related

Write A Static Method That Simulates Flip Of Weighted Coin

Apr 22, 2015

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?

View Replies View Related

How To Solve No Suitable Method Found For Write (String) Error

Sep 1, 2014

I wrote this program to prompt user to enter his choice to do a i/o operation in a file. It shows error. How to clear the error. My code is:

import java.io.*;
import java.util.*;
class Files {
public static void main(String args[]) {
String n;

[Code] ....

error:
E:java>javac Files.java
Files.java:26: error: no suitable method found for write(String)
fos.write(n);

[Code] ....

View Replies View Related

Calling Method Several Times And Trying To Write Multiple Records To A File?

Oct 27, 2014

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();
}

View Replies View Related

Write A Method Called AddToOverThirty Which Takes Array Nums3 As A Parameter

Apr 22, 2014

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};

[code]....

View Replies View Related

Prints Every Minimum In Array

Jan 20, 2015

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");

[code]....

View Replies View Related

Array Prints Out Zeros?

Nov 16, 2014

Ok, so let's say I am having a user input scores and at the end I want to print out the results so I do something like:

int[] Scores = new int[1000];
//code to ask user for input and store into array //

The user only inputs lets say 5 scores out of possible 1000. I then try to print it out by doing something like this:

for(int counter = 0; counter < Scores.length; ++counter) {
System.out.println(Scores[counter])
}

How would I go about printing out only the index's that were input, because right now it prints out the scores and then 995 0's after.

View Replies View Related

PrintWriter Only Prints Last Line?

Dec 6, 2014

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?

View Replies View Related

Program Prints 2 Values Instead Of One

Feb 4, 2015

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};

[Code] .....

View Replies View Related

Deleting From ArrayList / Goes Through Both If And Else And Prints

Dec 8, 2014

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++){

[code]....

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved