Read Positive Integer N And Plot 3 Triangles Of Size N

Oct 10, 2014

Write a Java program that reads positive integer n, 1 ≤ n ≤ 13, and plots 3 triangles of size n as shown below.

For n = 4, for instance, the program should plot: (works for n = 5 and n = 6 etc.)

triangle 1

1
2 3
4 5 6
7 8 9 10

triangle 2

_ _ _ 1
_ _ 3 2
_ 6 5 4
10 9 8 7

triangle 3

_ _ _ 1
_ _ 3 3 3
_ 5 5 5 5 5
7 7 7 7 7 7 7

import java.util.Scanner;
class Loops { 
void plotTriangle1(int n) {
int t = 1;
for (int i = 1; i <= n; i++) {
System.out.println(" ");
for (int j = 1; j <= i; j++) {
System.out.printf("%3d", t++);

[Code] ....

Currently compiles to this: Enter n(1-13):4
1
2 3
4 5 6
7 8 9 10

Format of the triangles, I couldn't get the spacing to work so I used underscores to replicate the blank space....

View Replies


ADVERTISEMENT

Calculate The Factors Of A Positive Integer Number

Mar 16, 2014

lines 7, 8, &12 "primes" are underline in red (prime cannot be resolved) is what pops up when i hover over the x's.

i don't get why that is.

package assignment7;
public class Exercise3
{
public static void main(String[] args)
{
Prime.setSize(1000);
for (int p = Primes.next(); p < 30; p = Primes.next())

[Code]...

View Replies View Related

Calculate Factors Of Positive Integer Number

Mar 17, 2014

Lines 7, 8, &12 "primes" are underline in red (prime cannot be resolved) is what pops up when i hover over the x's.

I don't get why that is.

Java Code :

public class Exercise3
{
public static void main(String[] args) {
Prime.setSize(1000);
for (int p = Primes.next(); p < 30; p = Primes.next()) {
int n = (int)Math.round(Math.pow(2,p)) - 1;
System.out.printf("%d 2^%d-1%d", p, p, n);
if (Primes.isPrime(n))

[Code] ....

View Replies View Related

Program That Accept Input Of Positive Integer And Sum All Digits

Jan 28, 2015

I am attempting to write a program that accepts input of a positive integer, reports each digit and then the sum of all digits. This is what I have so far:

*/
 package numbersum;
import java.util.*; 
public class Week4NumberSum {
static Scanner console = new Scanner(System.in); 
/**
* @param args the command line arguments

[Code]...

It works if I enter 1234567891 (10 digits)
Enter a positive integer: 1234567891
digit: 1
digit: 2
digit: 3
digit: 4
digit: 5
digit: 6
digit: 7
digit: 8
digit: 9
digit: 1

The sum of the digits is 46

But if I enter 11 digits, it fails:

Enter a positive integer: 123456789123 (12)
Exception in thread "main" java.util.InputMismatchException: For input string: "123456789123"
at java.util.Scanner.nextInt(Scanner.java:2123)
at java.util.Scanner.nextInt(Scanner.java:2076)
at week4numbersum.Week4NumberSum.main(Week4NumberSum. java:26)

java - Scanner error with nextInt() - Stack Overflow gives me some infor for InputMismatchException - which I find described as In order to deal with this exception you must verify that the input data of your application meet its specification. When this error is thrown, the format of the input data is incorrect and thus, you must fix it, in order for your application to proceed its execution.

I don't understand why the 10 digit integer is OK but the 11 or > digit integer is a mismatch.

View Replies View Related

Input Validation - Where Condition Is A Word From Set And Value Is A Positive Integer

Mar 18, 2015

I am trying to create a program that first asks the user for an input in the form: condition = value, where condition is a word from the set {limit, deficient, abundant, perfect, prime}, and value is a positive integer. Then it verifies the input. If the input is invalid it prints a message indicating that and terminates. If the input is valid it prints a table with the number of abundant, deficient, perfect and prime numbers less than or equal to N, where N = 1, 2, 3, ..., limit.

My problem is with the input validation. i want it to read the value as a string and verify if it's integer.

Here is my code

import java.util.Scanner;
 public class Factors
{
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
int n, f, fsum, p=0, a=0, d=0,pe=0,limit=0,abundant=0, deficient=0;
System.out.print("Enter stoping condition (condition = value): ");
String cond = scan.next();

[Code] ....

View Replies View Related

Write A Java Program That Reads A Positive / Non-zero Integer As Input

Aug 1, 2011

Write a Java program that reads a positive, non-zero integer as input and checks if the integer is deficient, perfect, or abundant.

A positive, non-zero integer, N, is said to be perfect if the sum of its positive proper divisors (i.e., the positive integers, other than N itself, that divide N exactly) is equal to the number itself. If this sum is less than N, the number is said to be deficient. If the sum is greater than N, the number is said to be abundant.For example, the number 6 is perfect, since 6 = 1 + 2 + 3, the number 8 is deficient, since 8 > 1 + 2 + 4, while the number 12 is abundant, since 12 < 1 + 2 + 3 + 4 + 6.

SAMPLE OUTPUT:

1.)Input N: 5

5 is deficient.

2.)Input N: 6

6 is perfect.

3.)Input N: 18

18 is abundant.

View Replies View Related

Determine Positive And Negative Values Have Been Read

Feb 4, 2015

The program work somehow, but it can't count the first input when user key in.

import java.util.Scanner;
public class DetermineValues {
public static void main( String[] args ) {
int sum;
int minus;
int data;

[code]....

View Replies View Related

Write A Program To Create Integer Array Of Size 20

Aug 2, 2014

Write a program to create an integer array of size 20. Then, the program should generate and insert random integers between 1 and 5, inclusive into the array. Next, the program should print the array as output.

A tremor is defined as a point of movement to and fro. To simulate it, the program will generate a random number between 0 and 19, which represents the location in the array (i.e. index number). Then, the 3 numbers to the left and right of this location should be reset to the value 0. If there isn't 3 numbers to the left and right you may assume a lesser number depending on the boundaries of the array.

Then, the final array should be printed as output. There is no user input for this program.Your program must include, at least, the following methods:

-insertNumbers, which will take as input one integer array and store the random numbers in it.
-createTremor, which will generate the random number as the location and return it.

A sample run of the program is shown below:

Sample output #1:
Array:1 2 2 3 1 5 4 2 3 4 4 2 1 1 3 2 1 4 3 2 1
Random position: 5
Final Array:1 2 0 0 0 5 0 0 0 4 4 2 1 1 3 2 1 4 3 2 1

View Replies View Related

Writing A While Loop To Read Positive Integers From User Until User Enters EOF Character

Feb 24, 2014

import java.util.Scanner;
public class Project_5
{
public static void main (String[] args)
{
Scanner input= new Scanner (System.in);

[code]....

So I'm attempting to have this program take the users input of integers, pressing enter in between each one. If they enter a negative number it lets them know that its invalid. At the end of the program it takes all of the valid integers entered and must add them and average them. I've put the final println in there as a placeholder. If the user types in "6" then presses enter and types in "3" , the output is:

There were 3 valid numbers entered.
The sum of the valid numbers was --- and the average was ---.
There were 0 invalid numbers entered.

It then continues on allowing the user to enter more values. Here is the example output for correct code"

Enter a positive value (EOF to quit): 4
Enter a positive value (EOF to quit): 7
Enter a positive value (EOF to quit): 8
Enter a positive value (EOF to quit): 2
Enter a positive value (EOF to quit): -1
The number "-1" is invalid.

Enter a positive value (EOF to quit): 8
Enter a positive value (EOF to quit): 0
Enter a positive value (EOF to quit): -4
The number "-4" is invalid.

Enter a positive value (EOF to quit): CTRL-D

There were 6 valid numbers entered.
The sum of the valid numbers was 29 and the average was 4.83.
There were 2 invalid numbers.

View Replies View Related

How To Read Integer Attribute Using DOM Element

May 13, 2014

I don't know how to read the attribute maxLenth. The way in the image I have done drive me to the error below.!

Piece of XSL:

<?xml version="1.0" encoding="UTF-8"?>
<c:message xmlns:c="ictt"><c:de format="B" lengthField="0" name="BIT MAP, PRIMARY" number="000"/>
<c:de format="B" lengthField="0" maxLength="008" minLength="008" name="BIT MAP, SECONDARY" number="001" subFields="00"/>

Piece of Java Code:

int length = Integer.parseInt(spec.getAttribute("maxLength"));
[5/13/14 14:52:19:497 CDT] 00000028 SystemErr R java.lang.NumberFormatException: For input string: ""
[5/13/14 14:52:19:497 CDT] 00000028 SystemErr R at java.lang.NumberFormatException.forInputString(Num berFormatException.java:63)
[5/13/14 14:52:19:497 CDT] 00000028 SystemErr R at java.lang.Integer.parseInt(Integer.java:502)
[5/13/14 14:52:19:497 CDT] 00000028 SystemErr R at java.lang.Integer.parseInt(Integer.java:531)

maxlenth.jpg

View Replies View Related

How To Read Integer And Then String From Java

Feb 28, 2015

I am solving a problem in which first my program will ask a number N and then N numbers form the user

suppose:
5
4 3 4 5 6

another
6
3 2 7 8 9 3

and I am using this code

inputValues=new LinkedHashMap<Integer, Integer>();
Scanner in=new Scanner(System.in);
int N=in.nextInt();
String inputString=in.nextLine();

[Code] .....

But its not working as i want . Where is fault?

View Replies View Related

Read String To List (Integer) And Back

Feb 14, 2014

I am looking for a good and reliable library to read a string to construct a list of Integers, Doubles, Booleans, etc. This library should be robust enough to handle faulty input from an inexperienced user.

Example:

input: "1, 2, 3, 4"
output List<Integer> [1, 2, 3, 4]

input: "1, 2, 3.6, 4"
output List<Double> [1.0, 2.0, 3.6, 4.0]

input: "true, true, false"
output List<Boolean> [true, true, false]

input: "[1, 2, 3]"
output List<Integer> [1, 2, 3]

input: "(1, 2, 3)"
output List<Integer> [1, 2, 3]

It would be really nice if such a library would already exist.

View Replies View Related

Write A Program That Read Integer And Display Diamond

Oct 14, 2014

Write a program that reads an integer and displays, using asterisks, a filled diamond of the given side length. For example, if the side length is 4 the program should display.

*
***
*****
*******
*****
***
*

I have it where it displays the top half of the diamond. But i cannot figure out how to get it to draw the bottom half.

import java.util.*;
public class E616 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("Enter number of rows. ");
int N=input.nextInt();

[code]...

View Replies View Related

Read Integer From Keyboard Until Negative Number Is Entered

Apr 8, 2014

Basically i have a question ask me to write a program that read integer from keyboard until a negative number is entered, and then print out the max and min out of the number i have entered.

This is what i have done already which works, but i need to ignore the negative number when print out max and min...which i dont know how to do so. when i compile this program now it will always give me the negative number i enter at the end as minimum. which i need to ignore

Also if negative number is entered at once the program should print a error message say max and min are undefined.

public static void main(String[] args) {

Scanner in = new Scanner(System.in);
int large = Integer.MIN_VALUE;
int small = Integer.MAX_VALUE;
while(true) {
System.out.print("Enter a integer: ");
int n = in.nextInt();

[Code] ....

View Replies View Related

Read From User Input (integer) And Reduce It By Multiplying Its Non-zero Digits

Nov 15, 2014

Write a program that reads from the user an integer and reduce it by multiplying its non-zero digits. The result of the multiplication is a number which is to be reduced as the initial one. This process continues until an integer of one single digit is obtained. For example:

64734502 (6 * 4 * 7 * 3 * 4 * 5 * 2) 20160 (2 * 1 * 6) 12 (1 * 2) 2

Your program should display the number obtained in every iteration.

Sample run1
Enter an integer: 64734502
After iteration 1: 20160
After iteration 2: 12
After iteration 3: 2

Sample run2
Enter an integer: 97737999
After iteration 1: 6751269
After iteration 2: 22680
After iteration 3: 192
After iteration 4: 18
After iteration 5: 8

View Replies View Related

Swing/AWT/SWT :: Draw 3 Triangles On Top Of Each Other To Give Appearance Of A Tree

Nov 22, 2014

I'm having trouble in 2 areas.

I'm trying to draw 3 triangles 'on top' of each other to give appearance of a tree. My code is below and struggling with how to set my x/y cords correctly.

Also I've drawn a rectangle under a new class and that should then be appearing in the south location of my border layout (south) - but it's not.

import javax.swing.*;
import java.awt.*;
public class Christmas1 extends JFrame {
JPanel titlePanel;
JLabel title;

[Code] .....

View Replies View Related

Fractal Triangle - Recursion To Make Children Triangles

Oct 7, 2014

I have a FractalGUI and a FractalTriangle class. The FractalTriangle class is what does a lot of the work, it uses ATriangle(s) to make a triangle, and it uses recursion to draw three children triangles along each parent triangle. Fractal Triangle has a method "makeTriangle" private ATriangle makeTriangle( Point p0, Point p1 ) which takes the ATriangles and sets them the right size and everything, but for some reason I can only get one level of children triangles, and they are also already their upon running which they shouldn't be, they should appear when the depth is 2. When the depth is three it should add three triangles each onto the three added at depth two. At depth 4 it should add three each onto the three previously made triangles, and so on until depth reaches 6, the max depth. Here is some code of my FractalTriangle.

The Atriangle class given to us ("starter code") does not extend or implement anything, and has basic methods (setLocation, setSize, setThickness, setColor, etc. )

//---------------- class variables ------------------------------
//---- recursive generation parameters
public static double sizeRatio = 0.5; // integer represent %
public static double offset = 0.5; // offset/100 = parametric value
// child positioning offset
public static double p2projection = 0.5;
// parametric value of projection of
// vertex p2 onto the base; can be < 0
public static boolean outside = true;

[code]....

the way it looks without the recursion looks much more right, but I am supposed to have recursion in that constructor but I can't figure out how to get it right with the recursion. It's so weird how with the recursion the big red triangle ( base triangle ) disappears, and you can see the children are really really small.

View Replies View Related

Find Area Of Four Triangles Split By Two Intersecting Lines

May 4, 2014

Im doing a problem where i have to find the area of four triangles split by two intersecting lines, All i have are the points for x1, y1, x2, y2, x3, y3, x4, y4. My question is how do i find where these two lines intersect?

View Replies View Related

How To Plot Graph

Nov 13, 2014

I am new to java. I wana create a java program that can plot a graph for that I've already written this:

/ x axis would be the ,,elemszam" from 0 to 599;
y axis would be ,,resz_xy"
double sum_xy=0;
double g=10000.0;
double z_xy;
double xy=0;

[Code]...

--- Update ---

Actually i'm using netbeans...

--- Update ---

and the datas are coming from a text file...

View Replies View Related

Plot Functions Using Abstract Methods

May 1, 2014

Assignment 13 – Plot functions using abstract methods (Plot functions using abstract methods) Write an abstract class that draws the diagram for a function. The class is defined as follows:

Java Code:

public abstract class AbstractDrawFunction extends JPanel {
/** Polygon to hold the points */
private Polygon p = new Polygon();
protected AbstractDrawFunction() {
drawFunction();

[code].....

For each function, create a class that extends the AbstractDrawFunction class and implement the f method. Figure below displays the drawing for the first three functions.how to make the jframe to work so that I displays the panel

Java Code:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.LayoutManager;
import java.awt.Polygon;

[code]....

View Replies View Related

How To Plot Geographic Coordinates In Java

Apr 3, 2014

-7984749.593824852,5368675.690126911 -7984392.035620423,5367570.791302501 -7983220.843257783,5366848.500158152 -7982343.534350842,5364796.5795687605 -7980293.251969412,5363408.488192292 -7980621.978425723,5361760.555707421 -7979175.604281852,5359233.088002191 -7977402.284793513,5358332.850725802 -7976513.843937492,5357665.108234091 -7976472.321767421,5357241.582554582 -7974834.812057852,5357842.933691362 -7973351.925120993,5354420.819505452 -7973743.435770122,5354147.992005592 -7973309.512395002,5352911.428583422 -7973639.9086436825,5352740.575552852 -7972698.034432082,5350359.050518452 -7972255.539456172,5350294.883494262 -7971909.001881332,5349519.720176511 -7970907.683061652,5349775.605440161 -7970575.617020612,5348986.585139751 -7970013.342272613,5349482.445545332 -7969898.683197102,5350177.703443532 -7968337.649977702,5350328.647515352 -7967111.243147633,5351652.264468962 -7966171.150047882,5351373.1018466605 -7966125.286417683,5352115.720678982 -7964626.258154663,5351707.120082232 -7955765.449326492,5345109.379313592 -7954159.443032822,5343966.704795052 -7942364.363746833,5335522.893249432 -7929990.868386692,5326092.063381992

View Replies View Related

Plot Functions Using Abstract Methods

Apr 29, 2014

Assignment 13 – Plot functions using abstract methods

Write an abstract class that draws the diagram for a function. The class is defined as follows:

public abstract class AbstractDrawFunction extends JPanel {
/** Polygon to hold the points */
private Polygon p = new Polygon();
protected AbstractDrawFunction() {
drawFunction();

[Code] ...

Test the class with the following functions:

a.f(x) = x2;
b.f(x) = sin(x);
c.f(x) = cos(x);
d.f(x) = tan(x);
e.f(x) = cos(x) + 5sin(x);
f.f(x) = 5cos(x) + sin(x);
g.f(x) = log(x) + x2;

For each function, create a class that extends the AbstractDrawFunction class and implement the f method. Figure below displays the drawing for the first three functions.

View Replies View Related

Two Threads - Generate Data And Plot It

Nov 17, 2014

I have two threads, one generates data based on some decision process (A) and another use the data to plot it (B). The problem occurs when there is no data to plot. What is the best way to synchronize these two threads? Using sleep, or wait/notify?

View Replies View Related

Basic Program To Plot Motion Of A Pendulum

Oct 22, 2014

Here is the outline of what the program should do: [URL] .....

So far I've got the very basics in place, I can't really figure out ptplot despite trying to research it online. The only other thing I know is that arrays are not to be used (initially I intended on using them). Here's my code:

//The next line imports the java.io package
import java.io.*;
//Imports math language
import java.lang.Math;
//import ptolemy.plot.*;
//Sets up class
public class cp5 {
//Declares class method called 'main'.

Code] ....

View Replies View Related

Creating Array And Putting Into Steam And Leaf Plot

Feb 12, 2015

I just want to know how I would go about creating an array of 100 random ints and putting it into a stem and leaf plot...

View Replies View Related

Can't Seem To Get Value For Integer 1 And Integer 2 To Pass With SetValue Method

Aug 10, 2014

public class MyInteger {
private int value;
public MyInteger(int number){
value = number;
System.out.println("Constructor created with value of " + value);

[code]....

I can't seem to get the value for integer1 and integer2 to pass with the setValue method. I get a runtime error stating the I need to have an int value for these two integers.

View Replies View Related







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