Jump to content

reading integers separated by spaces and calculating a triangle

- - - - -

  • Please log in to reply
3 replies to this topic

#1
pindo

pindo

    Newbie

  • Members
  • PipPip
  • 15 posts
I've been given an assignment to create a program that reads three integers from the user.
this this the qusetion:

Convert the C program you wrote in Assignment 1 that decides whether three integers inputted representing a triangle are invalid, equilateral, scalene or isosceles into Java. You should now adapt the program so that it reads the data from a file, the name of which is passed as a parameter on the command line. Your program may assume that this file will always exist and need not verify this. The format of the input file should be with each of the three side lengths for each triangle written on a single line separated by spaces, for example:

10 20 30

5 5 8

5 5 5

5 8 12


Your program should process the file in batch mode without requiring interaction from the user. Your program should, by default, output the results to the screen. However, if a second parameter is passed on the program's command line then this should be treated as a filename in the current directory where the results should be written to. In this case   your program should not output anything to the screen except for potential error messages. For example:

  java triangle infile.txt outfile.txt




Your program should also be as robust as possible in terms of processing the data it receives and your solution must be modular.

here's my assignment 1:


#include<stdio.h>

#include<math.h>


int main()

{

    int s1;

    int s2;

    int s3;


    printf("enter side 1: \n");

    scanf("%d", &s1);


    printf("enter side 2: \n");

    scanf("%d", &s2);


    printf("enter side 3: \n");

    scanf("%d", &s3);



    if(((s1 + s2) > s3) || ((s1 + s3) > s2) ||((s2 + s1) > s3) || ((s2 + s3) > s1) ||((s3 + s1) > s2) || ((s3 + s2) > s1))

       {

           printf("The triangle is valid and ");


        {

        if(((s1 == s2) && (s1 == s3)) || ((s2==s3)&& (s2==s1)) || ((s3 == s1)&&(s3 == s2)))

            printf("equilateral.\n");

        else

            if(((s1 == s2)&&(s1>s3))|| ((s1==s3)&&(s1>s3)) || ((s2==s3)&&(s2>s1)) || ((s2==s1)&&(s2>s3)) || ((s3 ==s1)&&(s3 > s2)) ||((s3 == s2)&&(s3>s1)))

                printf("isosceles.\n");

            else

                if(((s1 != s2) && ( s1 != s3)) ||((s2 != s1) &&(s2 != s3))||((s3 != s1) && (s3 != s2)))

                    printf("scalene.\n");

        }

       }

    else

        printf("input error.");


}



#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
I don't understand. Do you have a specific question or problem you're having trouble with? Post the Java code you have so far and tell us where you're having trouble so we can point you in the right direction.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3
pindo

pindo

    Newbie

  • Members
  • PipPip
  • 15 posts


import B102.*;


public class Triangle {


	public static void main(String[] args) {

		Triangle T = new Triangle();

		T.print();

		T.calc();

		

	}


	public void calc() {

		String[] Side = input.split (" ");

		String[] sides= new String[3];

		

		Side [0]=input;

		Side[1]=Side[1];

		Side[2]=Side[2];

		

		 if(((Side[0] + Side[1]) > Side[2]) || ((Side[0] + Side[2]) > Side[1]) ||((Side[1] + Side[0]) > Side[2]) || ((Side[1] + Side[2]) > Side[0]) ||((Side[2] + Side[0]) > Side[1]) || ((Side[2] + Side[1]) > Side[0]))

	     {

	         Screen.out.println("The triangle is valid and ");


	      {

	      if(((Side[0] == Side[1]) && (Side[0] == Side[2])) || ((Side[1]==Side[2])&& (Side[1]==Side[0])) || ((Side[2] == Side[0])&&(Side[2] == Side[1])))

	          Screen.out.println("equilateral.\n");

	      else

	          if(((Side[0] == Side[1])&&(Side[0]>Side[2]))|| ((Side[0]==Side[2])&&(Side[0]>Side[2])) || ((Side[1]==Side[2])&&(Side[1]>Side[0])) || ((Side[1]==Side[0])&&(Side[1]>Side[2])) || ((Side[2] ==Side[0])&&(Side[2] > Side[1])) ||((Side[2] == Side[1])&&(Side[2]>Side[0])))

	              Screen.out.println("isosceles.\n");

	          else

	              if(((Side[0] != Side[1]) && ( Side[0] != Side[2])) ||((Side[1] != Side[0]) &&(Side[1] != Side[2]))||((Side[2] != Side[0]) && (Side[2] != Side[1])))

	                  Screen.out.println("scalene.\n");

	      }

	     }

	  else

	      Screen.out.println("input error.");


	}


	public void print() {

	String input;

	Screen.out.println("Please enter the three sides: ");

	input = Keybd.in.readLine();

	}

}


thats what i've got.
not sure how to go about adding the elements.

#4
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Closing this thread Decided against it, but since this question has been better phrased on the following post, and is otherwise a copy, I suggest posting there:

http://forum.codecal...s-elements.html

Edited by ZekeDragon, 04 May 2011 - 09:03 PM.

Wow I changed my sig!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users