Trying to cut down on my java code

Okay, so I've completed 99% of an assignment for university, however, I want to know how I can cram a lot into one method, I want to know how I can nicely return a lot of values from one method, so it can be processed in another.

So I currently have many methods that make shapes, but each one for each individual shape, however all shapes share similar variables, they only differ in terms of height/width, and they do differ in and order too, etc.

i.e.:

// shape 1 constructor
shape1(var1, var2, var3, var 4, var 5);
shape2( var4, var5, var6, var1, var2, var3);

An example would be something like :

private static Shape1 createShape1(Scanner in){
     var1 = in.nextInt();
     var2 = in.nextInt();
     var3 = in.nextInt();
     var4 = in.nextInt();
     var5 = in.nextInt();
     // you get the picture
     Shape1 myShape1 = new Shape1 (var1, var2, var3, var4, var5);
     return myShape1 ;
}

Well I've done this many times, in many different methods, however I can't help but feel this is not a nice way to do things. So I was wondering, can I put all this in one method, that way this method can be called from any other method, I need an individual method for each individual shape, it's the way my lecturer has set things up. I was thinking on something more like allowing the above shape1 method to call this other method. Then pass the method it has called into a shape1 constructor, as I have another class that allows you to create a custom hexagon. But the constructor in the hex class requires the values that's being passed into the scanner. But I also want to be able to pass this other method into shape2's method and constructor?

The important thing to remember here, is that you are dealing with objects. It is not uncommon to have multiple classes and methods for even the most simple of problems.

What I would recommend for you is creating a parent class, to contain all the methods that your objects will share in common (similar methods only; you do not want to store all this object data in one method. Not secure blah blah proper programming blah blah).

Have you gone over inheritance and polymorphism?

Ever so briefly, and to be fair, we're not actually allowed to do that, kinda, or at least if we did, it would break the way in which they want us to make it work. They're VERY strict about how the code works/runs in my uni, I lost 50% of the marks in my first programming assignment because I used the word "number" instead of "amount". So you can see how they'd react if I started making my own classes and stuff, they didn't say I can't make my own methods, if anything, they said to do so.

The worst part was that this was just in plain text output, like so:

System.out.println("number");

Normally I would do exactly what you said, or at least I'd handle things in a much better/neater way, this isn't how you'd do it in reality, I can assure you that, there's a lot of duplicated code, for seriously no reason, I'm not sure why, I assume they'll explain why they've done it like this further down the line. I'm only in my first year to be fair, but I do want to try and make things as neat as possible.

fucking christ, what are they? Imbeciles

are you a part of the coding Discord? Might be easier to talk there.

I don't know, I don't think so, they do have PhD's to be fair, so idk about that, but I do think they're a little bit too strict with the marking criteria...

For the second post, unfortunately I am not, there's no such thing in my university, as far as I'm aware of.

no for tek syndicate

fear not, well help ya out @Atomic_Charge, invite this guy plz

2 Likes

Oh waw, my bad, maybe I'm the Imbecil, but no I am not...

You really need a parent class 'base shape' or shape which store all the shape specific code.
Also need a coordinate class to hold X,Y,Z values (float or double).
The constructor parameter is an array of coordinates. The shape simply store this array.
BOOM you have a 2d Shape, or 3d object.

The child class extend this by check the array size, and coordinate related rules. [is square or quadrilateral, etc]

The input processing canbe a different class.
dummy code:

array coordinates = MyShapeReader(scanner);
switch (coordinates.length)
case 3: new Triangle(coordinates)
case 4: new Quadrilateral(coordinates)
....

I agree, but like I've said above, the lecturers are rather strict about how we'd do this, they only want another method, not another class, I mean they deducted me of 50% of the marks for my first assignment for outputting the string value of number rather than amount.

Like so:

System.out.println("number"); 
// rather than: 
System.out.println("amount");

So making another class would probably lose me a substantial amount of marks, sadly, and they also don't want us to use arrays for this assignment. They said if we do use arrays we will get 0 marks, I really don't have a lot of options open to me, sadly.

Hmm okay. What are the rules and the goal? No Class too? No Array. Need all (read, process, output) in main method? What the shapes do?

So something like this?

import java.io.*;
import java.util.*;

public class ShaperApplication1 {

	static Scanner in = new Scanner(System.in);
	static PrintStream out = System.out;

	static void notInteger() {
		out.println("Oops!! Please enter only integral numbers");
	}

	public static void main(String[] args) {

		int coordinates = 0;

		while (true) {
			out.print("How many coordinates you have? ");

			try {
				coordinates = in.nextInt();
			} catch (InputMismatchException e) {
				notInteger();
			}

			if (coordinates > 2) {
				break;
			}

			out.println("Sorry you need minimum 3 coordinates for a shape");
		}

		out.println("Set coordinates (" + coordinates * 2 + "): ");

		switch (coordinates) {
			case 3:
				setTriangle();
				break;
			case 4:
				break;
		}

	}

	static void setTriangle() {
		int x1 = 0;
		int y1 = 0;

		try {
			out.print("x1: ");
			x1 = in.nextInt();

			out.print("y1: ");
			y1 = in.nextInt();

			out.println("c1: [" + x1 + "," + y1 + "]");

			// ....
		} catch (InputMismatchException e) {
			notInteger();
		}

	}

}

This is the sorta thing that we're currently doing, where you just have a main method with a switch statement, and then another method for each individual shape, only rather than using a 'coordinates' variable we go by shape name and read data in from a txt file. Simple enough, it's just disgusting seeing all the code duplication.

ahaa, okay then use again a scanner :)
read file, build a string, and read the string. really stupid but working if you can't use array....

import java.io.*;
import java.util.*;

public class ShaperApplication1 {

	static Scanner in = new Scanner(System.in);
	static PrintStream out = System.out;

	static void notInteger() {
		out.println("Oops!! Please enter only integral numbers");
	}

	public static void main(String[] args) {

		int coordinates = 0;

		while (true) {
			out.print("How many coordinates you have? ");

			try {
				coordinates = in.nextInt();
			} catch (InputMismatchException e) {
				notInteger();
			}

			if (coordinates > 2) {
				break;
			}

			out.println("Sorry you need minimum 3 coordinates for a shape");
		}

		out.println("Set coordinates (" + coordinates * 2 + "): ");

		switch (coordinates) {
			case 3:
				setTriangle(in); // in is your file new Scanner(file)
				break;
			case 4:
				break;
		}

	}

	static String delimiter = ",";

	static String getCoordinates(Scanner in, int amount) {
		String coordinates = "";

		try {
			coordinates += in.nextInt();
			amount--;

			while (amount > 0 && in.hasNextInt()) {
				coordinates += delimiter + in.nextInt();
				amount--;
			}
		} catch (InputMismatchException e) {
			notInteger();
		}

		return coordinates;
	}

	static void setTriangle(Scanner input) {
		String coordinates = getCoordinates(input, 6);

		Scanner s = new Scanner(coordinates);
		s.useDelimiter(delimiter);

		while (s.hasNext()) {
			out.println("c1: [" + s.next() + "," + s.next() + "]");
		}

		s.close();

	}

}